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.)