• Running Macros (WordXP)

    Author
    Topic
    #377004

    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

    Viewing 0 reply threads
    Author
    Replies
    • #619732

      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:

      • #619761

        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.

        • #619777

          > 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?

          • #619779

            We’ve been down this path before.

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

            • #619790

              > 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 = “C:Documents and SettingsusernameApplication DataMicrosoftWordSTARTUPtemplate.dot”
              ‘This works without a reference set to the global template
              Application.Run “FormatLetter.Detective”, strPath
              End Sub

            • #619797

              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).

            • #619933

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

            • #620058

              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. grin

            • #620770

              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

            • #619931

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

              Try putting the sub in a Global template.

              Try

              Option Explicit
              
              Private Sub TestReference()
                  Dim strSource As String
                  strSource = """" & "ABC" & """"
                  Debug.Print strSource
                  strSource = strRemoveQuotes(strSource)
                  Debug.Print strSource
              End Sub
              

              Where strRemoveQuotes is a Public Function in a Global template

            • #620057

              > 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. Create a new template (File|New…|Template), open the VBE and add a module.
              2. Add this code:
                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
              3. Rename the module to Lounge020925 (or something else, but remember what it is!).
              4. Save the template with any name to the Templates folder.
              5. Close the template.
              6. Add the new template as a global template in the Tools|Templates and Add-ins… dialog.
              7. Create a new document (Ctrl+N), open the VBE, and add a module.
              8. Add this code:
                You will need to substitute the appropriate module name if you did not use Lounge020925.

                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
              9. 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??

    Viewing 0 reply threads
    Reply To: Running Macros (WordXP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: