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!

Running code in another file or module (Word) 1

Status
Not open for further replies.

Nelviticus

Programmer
Sep 9, 2003
1,819
GB
According to the VBA help, if you want to run code in another template file or module you can do it in one of several ways:

Code:
MyTemplate.MyModule.MyFunction
Call MyTemplate.MyModule.MyFunction
Application.Run "MyTemplate.MyModule.MyFunction"

However none of these seem to work for me. The function I'm trying to run is Public but the first two versions won't even run ('MyTemplate' gives "Compile error: Variable not defined") and the third errors ("Unable to run the specified macro").

I can't even get it to run code in my Normal template. Any idea what I'm doing wrong? We use Word 2002 SP3 and macro security is set to 'Medium' although it still doesn't work even if I set this to 'Low' and enable 'Trust access to Visual Basic Project'.

Thanks

Nelviticus
 
Nelviticus,

The first two versions are really the same (Call keyword is optional). To use that approach, the calling code should be in a document based on the template or you can manually set a reference to the template file in your calling document: In the VB Editor, select your calling doc project in the Project window. Select References from the main menu then Browse to the template file, select and click OK. One other thing, if you haven't already done so, rename the Project in your template file to "MyTemplate", or whatever (select Project in the project window for the template file then change the Name property in the Properties window). Assuming the template also contains module MyModule which contains procedure MyProcedure then you could run it from your calling document using something like:
Code:
Sub CallTemplateProcedure()
   MyTemplate.MyModule.MyProcedure
End Sub


You can use the third method if the calling document is based on the template or you can reference a document name as indicated in the Help:
Code:
Application.Run "'My Document.doc'!ThisModule.ThisProcedure"
The document name can include the full path.


Regards,
Mike
 
Another method which might or might not be suitable in your case;

Code:
Dim Ppt As PowerPoint.Presentation

Set Ppt = GetObject("C:\testjunk\powerpoint\TaxPackTools.ppt")
DoEvents
Ppt.Application.Run "TaxPackTools!TaxPackToolsCall.StartTaxPacktools" ';, True
DoEvents

where T



Chance,

Filmmaker, gentleman and polla stilo eleous
 
Thanks for the replies, I've managed to get Mike's method to work.

We have a utility template call MasterTemplate.dot in our users' %APPDATA%\Microsoft\Word\STARTUP folders so that it's available whenever Word starts. We also have some corporate Word templates on our network. I'm trying to put code into these templates to call one of the functions in MasterTemplate. This works a treat:

Code:
Application.Run "MasterTemplate.dot!HRToolbar.ColourType"

Have a star!

Nelviticus
 
Actually Chance's method is pretty much the same but Mike beat you to it.

"Where T" indeed, I have often wondered that. Hen pen eleous!

Nelviticus
 
If MasterTemplate.dot is a loaded global template (in Startup) then any procedure, regardless of module, should be able to execute by name.
Code:
Application.Run [b]MacroName[/b]:="TestMe"

This would execute TestMe, regardles of what global template it is in.

I have two global templates loaded. I can call any procedure in either of them, by name. I do not have to state the template, nor the module. It is is loaded as a global, then everything in it is available.

Although, explicitness is generally a good thing....



faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top