Thread: VB6 Question
View Single Post
  #5   Spotlight this post!  
Unread 26-05-2004, 21:52
seanwitte seanwitte is offline
Registered User
None #0116
Team Role: Engineer
 
Join Date: Nov 2002
Location: Herndon, VA
Posts: 378
seanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant future
Send a message via AIM to seanwitte
Re: VB6 Question

First, add a reference to the "Microsoft Word 10.0 Object Library" from the Project->References menu.

This subgroutine will create a new word document and write a block of text to it:
Code:
Sub WriteWordDoc(content As String, _
                 filename As String, _
                 Optional font As String = "Verdana", _
                 Optional fontsize As Integer = 10)
                
    Dim app As Word.Application
    Dim doc As Word.Document
    
    'get an instance of the MS Word application
    Set app = CreateObject("Word.Application")
    
    'create a new document
    Set doc = app.Documents.Add
    
    'initialize the document and write the content
    With doc.content
        .font.Name = font
        .font.Size = fontsize
        .Text = content
    End With
    
    'save the document and close Word
    doc.SaveAs filename
    doc.Close
    app.Quit
    Set doc = Nothing
    Set app = Nothing
End Sub
The app.Quit line is important, otherwise it will keep Word open in the background. If you add the line "app.Visible = true" and remove "app.Quit" then you will see Word open with your new document.