vbams-officems-wordword-2013

Calling Macro from inside Macro - Error Compile Error: Expected variable or procedure, not module - Private Sub App


I have been copying code moslty to work on and learn macro's in Word. I have got it working where I can get a MsgBox to appear before printing, but I would like it to call another module/macro to compartmentalize the modules.

For testing, this works:

Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
MsgBox "Before Print"
End Sub

But if I do:

Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
Call Greeting
End Sub

Which is a working macro that I have which simple opens a MsgBox and says "Greetings", I get the following error:

Compile Error: Expected variable or procedure, not module

How can I call another Macro inside this Private Sub App*?


Solution

  • While this post has been covered before, I will share the answer here as well in case others land here.

    The issue is that you can't have a module named the same as a sub. See Screenshot below for what is WRONG!

    enter image description here

    Interestingly, if you put the sub that calls the other sub (greeting) inside the same module (greeting) it works! But I would say this is bad practice and should be avoided. See example below:

    enter image description here

    I will generally just append _Sub to my module names to avoid this issue like below:

    enter image description here

    Also a note about using the keyword Call.

    You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.

    See here for more information --> What does the Call keyword do in VB6?