Results 1 to 12 of 12
  1. #1
    Star Lounger
    Join Date
    Jun 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Running Macros (WordXP)

    Greetings All,

    My sole surviving grey brain cell has a problem( well several but thats another story). How do you run a macro in one template
    from another without referencing?

    My dilemna is that I have new templates with a completely new set of styles. Using arrays that match the new styles against the old ones
    I can reattach an existing doc to its new template and then using find/replace to update the doc. This works but I don't want to duplicate the
    code in each & every template.

    now I can pass a array to the add-in/run code which then does the re-attachment but,The add-in then has to run a macro
    in the attached template that sends it the style arrays. As I can't have two templates referencing each other
    I've come unstuck.

    Application.Run "Thistemplate.ThisModule.ThisProcedure" seems to be what I need but I just can't seem to get a handle on it.
    Can anyone help me with the exact syntax

    I also looked at Application.VBE.ActiveVBProject.VBComponents as a possible solution but can't even get to the macros within a
    module let alone run them.


    Brain-dead

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Running Macros (WordXP)

    Frank, I see that your long quest continues (Passing Arrays between Templates WordXP/97/VBA, 03-Apr-02).

    This board has seen at least two long threads on accessing code in "other" templates in Word, which I do not have the energy to review and summarize:

    <UL><LI>Referencing a function in another template (VBA (Word97/2000)), 18-Aug-02

    <LI>Running code in a different Word template (Word 2000 SR1a), 21-Dec-01[/list]Regarding Application.Run, it will have been discussed in the above threads, and I will also add as a footnote, in case it is somehow helpful, that in Excel, the delimiter is a "bang" (!) rather than a "dot" (.):

    Application.Run "Book2.xls!Bk2Hi" (See Post #158218.)

    Hope this helps.

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Running Macros (WordXP)

    If you are calling a Sub or Function in a Global template you need to include a REference to the Global template in the calling template.

    Do you find this does not work?

    If so, please post the EXACT code and explain how you are establishing the Reference.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Running Macros (WordXP)

    > If you are calling a Sub or Function in a Global template you need to include a
    > REference to the Global template in the calling template.

    I do not find it to be necessary. In one of my global template modules I have

    Sub CloneMe()
    ...
    End Sub

    If I create a new document and add a module and add this:

    Sub DocMacroTest()
    Application.Run "CloneMe"
    End Sub

    Then close the VBE and return to Word and use the Macros dialog to run DocMacroTest, it works just fine without adding a reference. Why is that?

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Running Macros (WordXP)

    We've been down this path before.

    If the Sub has args, then a Reference is needed.

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Running Macros (WordXP)

    > If the Sub has args, then a Reference is needed.

    I don't find that to be the case in my little tests for either Functions or for Subs taking arguments, but it's certainly possible that I've missed something. I'm using Word 2000 VBA, with the test macros in a module in a document.

    Sub TestGlobalTemplateFunction()
    'This works without a reference set to the global template
    MsgBox WordBasic.Call("WdLib.SaveIt", 2, "", 1)
    'So does this one
    MsgBox Application.Run("WdLib.SaveIt", 2, "", 1)
    End Sub

    Sub TestGlobalTemplateSub()
    Dim strPath As String
    strPath = "Cocuments and SettingsusernameApplication DataMicrosoftWordSTARTUPtemplate.dot"
    'This works without a reference set to the global template
    Application.Run "FormatLetter.Detective", strPath
    End Sub

  7. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Running Macros (WordXP)

    As you point out, Application.Run allows you include arguments, but the arguments must be of Variant type. If you had a sub or function in the global template which required typed arguments, Application.Run might not work.
    So one benefit of including a reference to the global template would be that you can send the sub or function typed arguments (as well as having the convenience of calling the sub or function directly).

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Running Macros (WordXP)

    That's because there is automatically a Reference to the document.

    Try putting the sub in a Global template.

    Try
    <pre>Option Explicit

    Private Sub TestReference()
    Dim strSource As String
    strSource = """" & "ABC" & """"
    Debug.Print strSource
    strSource = strRemoveQuotes(strSource)
    Debug.Print strSource
    End Sub
    </pre>


    Where strRemoveQuotes is a Public Function in a Global template

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Running Macros (WordXP)

    AFAIK, the type of parameter is not the issue. The call and Sub must have compatible arg types independently of the Reference.

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Running Macros (WordXP)

    > That's because there is automatically a Reference to the document.
    > Try putting the sub in a Global template.

    I was calling a sub in a Global template, a template in Word's start-up folder. In your example, you are not using Application.Run, so I can see why Word does not recognize the function name. But it does seem to work without a reference to the template when I use Application.Run.

    Here's a new demo.
    1. <LI>Create a new template (File|New...|Template), open the VBE and add a module.
      <LI>Add this code:
      <pre>Option Explicit

      Sub WhatTimeIsIt(strName As String)
      MsgBox "Hey there, " & strName & ", it is " & Format(Now, "hh:mm") & "."
      End Sub

      Function ProperCase(strText As String)
      ProperCase = StrConv(strText, vbProperCase)
      End Function</pre>

      <LI>Rename the module to Lounge020925 (or something else, but remember what it is!).
      <LI>Save the template with any name to the Templates folder.
      <LI>Close the template.
      <LI>Add the new template as a global template in the Tools|Templates and Add-ins... dialog.
      <LI>Create a new document (Ctrl+N), open the VBE, and add a module.
      <LI>Add this code:
      You will need to substitute the appropriate module name if you did not use Lounge020925.
      <pre>Option Explicit

      Sub TestGlobalTemplateSub()
      Application.Run "Lounge020925.WhatTimeIsIt", Application.UserName
      End Sub

      Sub TestGlobalTemplateFunc()
      MsgBox Application.Run("Lounge020925.ProperCase", _
      "my shift key is broken. please help me. THANKS.")
      End Sub</pre>

      <LI>Test the subs. They should run without any explicit reference in the document to the global template.
    Does no one else get the same result as I do??

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Running Macros (WordXP)

    In a truly bizarre coincidence, both the sub and the function I tested were defined with variant parameters! However, I was able to use the same technique with parameters typed as strings. I didn't try any numbers because... I'm pooped out on testing this. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  12. #12
    Star Lounger
    Join Date
    Jun 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Running Macros (WordXP)

    Gentle Persons a late ending to this thread,

    I have indeed managed to pass arrays between projects and this has enabled me to send 2 - dim'd style mapping arrays.

    Now a legacy document can be automatically attached to its new template and have a mapping array update the styles
    in the document and delete the old ones.

    Thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •