Tip

Word Subroutines

You can use these routines to output data directly to Word regardless of the version they're running. We use these for a lot of reports and even to created quotes via protected Word documents. I recommend sticking them into a library then just including them whenever you like.

Sub NewWordDoc(msword As Variant, worddoc As Variant, IsVisible As Variant)
'msword and worddoc are for passing back control of the instance of word an the new word document respectively
'IsVisible is for determining whether on not to make the instance of Word visible

'Create an instance of Word
Set msword = CreateObject("Word.Application")
Set docs = msword.Documents
msword.Visible = IsVisible
'Create a new word document and set it to active
Call docs.Add
Set worddoc = docs(1)
worddoc.Activate
?Open an existing Word Document
Call msword.Documents.Open("""c:initiatives.doc""")

End Sub

Sub AttachWord(worddoc As Variant, rtitem As NotesRichTextItem, filename$)
'Saves the Ms Word document as filename$ then attaches it as an object to the rich text item. Useful for attaching the Word document to a Notes document

Dim object As NotesEmbeddedObject
Call SaveWord(worddoc, filename$)
Set object = rtitem.EmbedObject( EMBED_ATTACHMENT, "", filename$, "thewordfile" )
Call rtitem.AddNewLine( 1 )

End Sub

Sub SaveWord(worddoc As Variant, filename$)
'Saves the Word document as filename$

worddoc.SaveAs filename$

End Sub

Sub AddWordTextAndLink(worddoc As Variant, mytext$, mylink$)
'Adds the text in mytext$ to the Word document then makes it a link the the address in mylink$ then moves down to the next line

Call worddoc.Activewindow.Selection.TypeText(mytext$)
Call worddoc.Activewindow.Selection.HomeKey(5, 1)
Call worddoc.Hyperlinks.Add(worddoc.Activewindow.Selection.Range, mylink$ , "")
Call worddoc.Activewindow.Selection.TypeText(Chr$(13) + Chr$(13))

End Sub

Sub AddWordText(worddoc As Variant, myText$)
'Adds the text in mytext$ to the Word document then moves down to the next line

Call worddoc.Activewindow.Selection.TypeText(mytext$ + Chr$(13))

End Sub

Sub CloseWord(msword As Variant, worddoc As Variant)
'Closes the Word document then closes the instance of Word

worddoc.Close
msword.Application.Quit

End Sub

This was first published in November 2000

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.