Avatar of JohnRobinAllen
Flag 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.
Visual Basic ClassicMicrosoft Word

Avatar of undefined
Last Comment

8/22/2022 - Mon

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.

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.


Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

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.