Riddle me this: Using Application.Run with a Function RRS feed

  • Question

  • I think I've found what I would describe as a bug.  I'm using Word 2007.

    Open the VBA window, give the template a name of "Library", add a module "Main" and add in the following code:

    Sub Test1()<br/>
      Dim str As String<br/>
      Dim project As String: project = "Library"<br/>
      Dim module As String: module = "Main"<br/>
      Dim func As String: func = "Test2"<br/>
      On Error GoTo err<br/>
      str = func<br/>
      x = Application.Run(str)<br/>
      str = module + "." + func<br/>
      x = Application.Run(str)<br/>
      str = project + "." + module + "." + func<br/>
      x = Application.Run(str)<br/>
      Exit Sub<br/>
      MsgBox "Error on " & str<br/>
      Resume Next<br/>
    End Sub<br/>
    Function Test2() As String<br/>
      Test2 = ""<br/>
    End Function

    Running the Test1 subroutine will bring up the following message:   Error on Library.Main.Test2

    Basically you can't reference the name of the project, as mentioned here: when using a function.  With subs it seems to work fine -- only functions raise the error.

    Wednesday, January 12, 2011 10:11 PM


  • Hi ts

    mmm, no, not a bug - that's the way it's designed. Similar principle to the fact that only Public Sub procedures with no parameters appear in the Macros list. Indeed, the capability to include Parameters when using Run was introduced comparatively late.

    As Application.Run only "pushes" a macro to run, it's logical that it can't return the result of a function. And the object model Help does not indicate that it can return anything a function would pass back - for just this reason.

    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Bruce Song Monday, January 17, 2011 7:08 AM
    • Marked as answer by Bruce Song Monday, January 24, 2011 10:47 AM
    Thursday, January 13, 2011 8:34 AM