Link to home
Start Free TrialLog in
Avatar of kenmck
kenmck

asked on

How do I run a Word Macro From VB?

I have a Word template with a macro. This macro makes the report and sends it to the printer.

Does anyone know a way from VB that I can open this template and run the macro automatically?
Avatar of daffyduck14mil
daffyduck14mil

Yes, you can do it this way:

Make sure you reference the microsoft word object library. Then, using your word object you can use the wdObject.Documents.Add("C:\PathtoTemplate\Template.dot")

Then, you can use wdObject.Run MacroName:="TheMacroThatDoesTheJob"

If you have instantiated word and it's standing by with the template open that has the macro, it should run fine.

Grtz.©

D.
Avatar of Richie_Simonetti
Call your macro "Auto_open", everytime the user loads the template macro is executed.
Cheers
Sorry, call your macro like this (from whitin your template):

Private Sub Document_New()
Call Auto_Open ' Or whatever you name your macro
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kenmck

ASKER

Cheers that worked fine.

Thanks for your help.
Just a question:
If that worked fine, why a "B" grade?
Glad to help you.
Cheers.
Avatar of kenmck

ASKER

I have the problem of having to make VB wait till Word has opened before it can pass the variables. It works fine but will be better I am assured when using .net.

Cheers