none
Calling function in a separate document RRS feed

  • Question

  • Hi all,

    I have been developing in excel for a while, but have limited experience in word.

    In excel you can call a function on a separate workbook like this:

    application.Run "test_com_addin.xls!mAutoOpen.Callme"


    but that doesn't seem to work in word. I have tried different variations:

    Application.Run "mDest.DestProc"
    Application.Run "projDest.mDest.DestProc"
    Application.Run "docDest.doc!mDest.DestProc"
    Application.Run "'docDest.doc'!projDest.mDest.DestProc"
    ' etc...

    No dice. Can this be done?

    Some more background:

    I have a COM add in that sets up a callback to the given document, but the it only works when the target document is the active document.

    Any suggestions?

    Many thanks,

    Yiannis

    Wednesday, May 30, 2012 11:10 AM

Answers

  • Hi Yiannis

    I don't think you can add a reference to a Word document as part of a COM Add-in. I think that would only work as part of a Word VBA document-level solution.

    Why is the code in a document that's not the active document? Is this document supposed to act as some kind of "utility provider"? If yes, then save it as a Word Template file (*.dotm). don't open it; have your add-in load it as a template addin, via the WordApplication.Addins object. Then the macros will be available "globally" within the Word application and you can use them with the Run method, as you're trying to do.


    Cindy Meister, VSTO/Word MVP

    Wednesday, May 30, 2012 4:06 PM
    Moderator

All replies

  • Normally, in Word, Application.run is used to call procedures in projects that are loaded in memory, such as templates that are activated when Word starts up.

    If the target document that contains the code is just a regular document that was not actually loaded in memory when Word started, the call will fail. In such cases, what I normally do is add a reference to the target document in the calling project, and then you do not even need Application.Run, you simply call the procedure with a regular call:

    projDest.mDest.DestProc

    Wednesday, May 30, 2012 12:55 PM
  • Thanks,

    What you're saying is fair enough. But in my case, I have an application wide (COM) addin and I need my C#/C++ to call back to the document (as long as it's loaded) using the name of the target method (=> via Application.Run ). By looking around I found there is several people claiming that something similar to what I posted for excel would work for word, but I can't figure out how...

    Yiannis

    Wednesday, May 30, 2012 1:23 PM
  • As I said, you need to add a reference to the target document in your COM add-in before you can call the procedure. And once you do add the reference, you do not need to use Application.Run, simply call the procedure by using its full name (Project.Module.Procedure).

    Application.Run is used to call procedures that are already loaded in memory (located in templates that are loaded when Word is instantiated, not simply by opening the document later on), which is not the case with your target document, or else Application.Run would work.

    Wednesday, May 30, 2012 2:08 PM
  • As I said, you need to add a reference to the target document in your COM add-in before you can call the procedure. And once you do add the reference, you do not need to use Application.Run, simply call the procedure by using its full name (Project.Module.Procedure).

    Application.Run is used to call procedures that are already loaded in memory (located in templates that are loaded when Word is instantiated, not simply by opening the document later on), which is not the case with your target document, or else Application.Run would work.

    I didn't understand you were talking about adding a reference to the target word document in the COM add-in.

    How do you do that? I think the approach defeats the purpose of an application-wide add-in, but I am really interested to know how this is done. Do you have any references to any documentation?

    Thanks,

    Yiannis

    Wednesday, May 30, 2012 2:23 PM
  • Hi Yiannis

    I don't think you can add a reference to a Word document as part of a COM Add-in. I think that would only work as part of a Word VBA document-level solution.

    Why is the code in a document that's not the active document? Is this document supposed to act as some kind of "utility provider"? If yes, then save it as a Word Template file (*.dotm). don't open it; have your add-in load it as a template addin, via the WordApplication.Addins object. Then the macros will be available "globally" within the Word application and you can use them with the Run method, as you're trying to do.


    Cindy Meister, VSTO/Word MVP

    Wednesday, May 30, 2012 4:06 PM
    Moderator
  • Thanks Cindy,

    The .dotm solution is probably what I am looking for.

    Yianins

    Wednesday, May 30, 2012 5:38 PM