Thread: VB6 Question
View Single Post
  #6   Spotlight this post!  
Unread 27-05-2004, 07:32
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: VB6 Question

Quote:
Originally Posted by seanwitte
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.
Will a similar setup work with other versions of office? (and won't the 'doc.Close' make 'app.Visible = true' a little useless? Slight bug.)