Link to home
Start Free TrialLog in
Avatar of JohnRobinAllen
JohnRobinAllenFlag for Canada

asked on

How can I run a sub in another module using VBA with Word?

Using MS Word and working with VBA, I have a Sub, call it Sub1, in the my Normal template. I also have a document loaded that contains a module, MontInit, which contains another Sub, call it Sub2.

I want to have Sub1 in the Normal template run Sub2 in the loaded document.

Neither the command “Sub2” nor “Call Sub2” works.

MontInit.Sub2 and Call MontInit.Sub2 also fail.

I tried using ActiveDocument.Name to get the name of the document with the “Sub2” code in the hopes I could somehow let Sub1 know where to look, but that did not work either.
Any suggestions?
Many thanks for any help that works.
Avatar of GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use the 'Run' command to call a macro in another document/template. The syntax can be tricky, so let us know if you have specific problems.
Avatar of JohnRobinAllen


I also tried using "run" but I do not know the proper syntax.
If it is of any help, Run Sub2 does not work.
The quotation marks in Run "Sub2" do not work either.
Perhaps you could give me a specific instance where "Run" will work.

In the meantime I will try to Google "run" in VBA.

Avatar of JohnRobinAllen
Flag of Canada image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also add a reference to another word document. In the VBA environment, Tools -> References, Click "Browse", then Files of Type "Word Document", then browse to the document you want to reference and click "Open"

Once you have referenced another document, you can call any code in it, assuming the scope is public.

Hope this helps. I'm a little confused as to why code in the Normal template would be calling code outside the normal template, though. Normally, it would be the other way around.
In response to baffy2000:
     I have a keyboard shortcut that does different things depending on what kind of specific document is loaded. The code each sub controls by that shortcut does should be secret. I would prefer that others do not know what a poor programmer I am. I therefore put the bulk of the code into the specific document and hide it from view. The keyboard shortcut runs a simple sub in Normal that runs the hidden code in the active document.
     The different tasks performed are:
           a) If the document is an essay that a professor has corrected, it analyzes the essay for the student, prepares a description of the types of errors the student has made  and calculates a grade for the essay based on values stored in a third document that rate each type of error.
          b) If the document has already the result of step a, where the bulk of the work has been done and an evaluation written, then it updates the grade assigned, based on new values specified in the third document. It skips the other work that has already been done: the analysis.

    It sounds complicated, but it works beautifully.