3

I have the following two Sub defined in my Word Addin (.dotm) which I have put in StartUp directory

Public Sub SayHi1()
    MsgBox "Hi......."
End Sub

Public Sub SayHi2(ByVal n As String)
    MsgBox "Hi " & n
End Sub

Then from a new document I am able to call 1st Sub without argument as below:

Sub AppRun_AddIn_NoArg()
    Application.Run "MyProject.Module1.SayHi1"
End Sub

But when I try to run the 2nd Sub with argument I get error saying "Object doesn't support this property or method"

Sub AppRun_AddIn_WithArg()
    Application.Run "MyProject.Module1.SayHi2", "Tejas"
End Sub

Error Message: enter image description here

TechGeek
  • 2,172
  • 15
  • 42
  • 69
  • 3
    For some reason it works if you remove the `MyProject` bit - but then obvously you might end up calling the wrong macro. Apparently this has something to do with ["If you specify the document name, your code can only run macros in documents related to the current context — not just any macro in any document"](http://msdn.microsoft.com/en-us/library/office/ff838935.aspx). – GSerg Sep 26 '14 at 16:29
  • Is there a reason why you aren't using Call? – Mr. Mascaro Sep 26 '14 at 16:47
  • @GSerg Yeah, Without project name & module name it worked. – TechGeek Sep 26 '14 at 21:35
  • @jbarker2160: no specific reason. Is there any advantage I will get by using Call ? – TechGeek Sep 26 '14 at 21:35
  • @Tejas There is - you'd be able to call it using the fully qualified name, and you'll get intellisense. And it will be slightly faster to work. You use `.Run` when the macro name is not known at compile time, if you do know it at compile time, just call it directly. – GSerg Sep 27 '14 at 10:01
  • @GSerg When i write call, I dont get my function or project name in intellisense. My function is in another file, so call won't work I think. Can you show an example? – TechGeek Sep 27 '14 at 14:05
  • In the code editor, go to Tools - References, click Browse, select "Word Documents" from the File Type dropdown, navigate to the template and select it. This creates a reference. – GSerg Sep 27 '14 at 14:48

1 Answers1

2

This appears to be long-standing problem with Word.

As KB190235 suggests:

Cause:
You have included a template name as part of the Macroname argument string.

Resolution:
Remove the template name from the Macroname argument.

Workaround:
To avoid naming conflicts among referenced projects, give your procedures unique names, so that you can call a procedure without specifying a project or module.

Community
  • 1
  • 1
GSerg
  • 76,472
  • 17
  • 159
  • 346