Article ID: 185168
Article Last Modified on 6/17/2005
Sub MyWordMacro(strPassedParam As String)
MsgBox strPassedParam
End Sub
Sub AutomateWord_OpenDoc()
Dim wrdApp As Object
Dim wrdDoc As Object
Dim strFileName As String
Set wrdApp = CreateObject("Word.Application")
On Error GoTo DocError
' Replace the following example string value with the path and
' file name of the document or template containing your macro.
strFileName = "<Path and Filename of document or template>"
' Open the document or template and set a variable equal to the
' document or template.
Set wrdDoc = wrdApp.Documents.Open(strFileName)
' Run the macro. (Replace "MyWordMacro" with the name of your macro.)
wrdDoc.MyWordMacro ("This is a test.")
DocError:
If Err.Number <> 0 Then Msgbox Err.Description
' Quit this instance of Word.
wrdApp.Quit
' Clear variable memory.
Set wrdApp = Nothing
Set wrdDoc = Nothing
End Sub
Sub AutomateWord_OpenDoc()
Dim wrdApp As Object
Dim wrdDoc As Object
Dim strFileName As String
Set wrdApp = CreateObject("Word.Application")
On Error GoTo DocError
' Replace the following example string value with the path and
' file name of the template containing your macro.
strFileName = "<Path and Filename of template>"
' Open the document and set a variable equal to a new blank
' document and its underlying template.
Set wrdDoc = wrdApp.Documents.Add(strFileName)
' Run the macro. (Replace "MyWordMacro" with the name of your macro.)
wrdDoc.MyWordMacro ("This is a test.")
DocError:
If Err.Number <> 0 Then Msgbox Err.Description
' Quit this instance of Word.
wrdApp.Quit
' Clear variable memory.
Set wrdApp = Nothing
Set wrdDoc = Nothing
End Sub
For additional information, please see the following article in the
Microsoft Knowledge Base:
181058 OFF98: How to Run Sample Code from Knowledge Base Articles
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: wordcon vba vb vbe OLE
Keywords: kbhowto kbdtacode KB185168