Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VB to execute a Word macro

Status
Not open for further replies.

samvonac

Programmer
May 18, 2001
8
US
I want to use a VB app to open a word doc and launch a macro. I have the
following code:

Sub Main()
Dim docsObj As Word.Documents
Dim docObj As Word.Document
Dim appWord As Word.Application
Set appWord = CreateObject("word.application")
Set docsObj = appWord.Documents
Set docObj = docsObj.Open("C:\mydoc.doc")
docObj.Application.Run ("MyMacro")
docObj.SaveAs "MyNewName.doc"

On Error Resume Next

docObj.Close

On Error GoTo 0

Set docsObj = Nothing
Set docObj = Nothing
appWord.Quit
End Sub


I get an error when it gets to "docObj.Application.Run ("MyMacro")" saying that
macro could not be found. I know that the macro exists and the macro is saved in
normal.dot.

I am using VB6 and Word2000. Any ideas?

Thanks in advance.
 
Instead of:
docObj.Application.Run ("MyMacro")

try something like:
appWord.Run "Project.Module1.MyMacro"

The code below was taken from this link:

Sub WDTest()
Dim WD as Object

Set WD = CreateObject("Word.Application")

WD.Documents.Open "C:\My Documents\WordDoc.Doc"

' Note that the project name and module name are required to
' path the macro correctly.
WD.Run "Project.Module1.WordMacro"

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top