Results 1 to 11 of 11
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling Word macro through automation. (2000 sp-3/ 98SE)

    I was reading this article: WD2000: How to Pass a Parameter to a Macro Using Automation, which states that "To pass a parameter to a Word Visual Basic for Applications macro using automation, you need to move your Word macro code to the ThisDocument object module of the document or template that contains the macro you are calling." This works as expected for code such as:
    <code>
    Set oWordActiveDoc = oWord.Documents.add("MyTemplate.dot")
    msg = "ThisDocument module of template."
    oWordActiveDoc.test msg
    </code>
    What I'm hoping to do is to be able to access the existing Word macro code, in the modules of the VBA project of the attached template, without the need to fiddle with the ThisDocument module. I'm sure I've seen this done, but can't find any examples. For instance, I might want to pass a parameter to the macro "MyTest(str As String)" in

    Project Window
    -> Project (MyTemplate)
    -> Modules
    -> Macros1

    Is this possible to do?

    Alan

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Calling Word macro through automation. (2000 sp-3/ 98SE)

    You could leave the main code in a standard module, and just put a stub in ThisDocument that calls the code in the standard module:

    Public Sub RunMyTest(strArg As String)
    Call MyTest(strArg)
    End Sub

    You can then use

    oWordActiveDoc.RunMyTest "Alan Miller"

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Word macro through automation. (2000 sp-3/ 98SE)

    Thanks, I'd thought about that - it seems to work OK and I guess would suffice for most situations I can envisage. Still, I was wondering if it is possible to drill down deeper, through the document or template's VBE, VBProject or any of the other object model components I know nothing about. <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

    Alan

  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: Calling Word macro through automation. (2000 s

    How about something like this, using Word's Application.Run method?

    Code in Excel workbook module:
    <UL><code>Sub RunAttachedTemplateMacroWithParameter()
    ' These values are for testing... change as required
    Const strTemplate = "PhoneNotes.dot"
    Const strMacroName = "modPhoneNotes.ScribbleInDoc"
    Const strParameter1 = "This is some test text!"
    ' Direct object refs require ref in Tools>References to MS Word library
    Dim appWord As Word.Application, docWord As Word.Document
    Set appWord = New Word.Application
    Set docWord = appWord.Documents.Add(appWord.Options.DefaultFileP ath(wdUserTemplatesPath) & _
    appWord.PathSeparator & strTemplate)
    With docWord
    .ActiveWindow.Visible = True
    .Application.Run strMacroName, strParameter1
    End With
    Set docWord = Nothing
    Set appWord = Nothing
    End Sub</code>[/list]Code in named module of attached template:
    <UL><code>Sub ScribbleInDoc(strScribbleText As String)
    ActiveDocument.Content.InsertAfter vbCrLf & vbCrLf & strScribbleText
    End Sub</code>[/list]Does this approach work for your project?

  5. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Word macro through automation. (2000 s

    Thanks Jefferson. I think that will do what I'm wanting. I couldn't get this to work because I was trying to qualify the macro path with the template name & module, as suggested in VBA Help:

    MacroName Required String. The name of the macro. Can be any combination of template, module, and macro name. For example, the following statements are all valid.

    Application.Run "Normal.Module1.MAIN"
    Application.Run "MyProject.MyModule.MyProcedure"
    Application.Run "'My Document.doc'!ThisModule.ThisProcedure"


    Your method is better because:
    a) It's simpler
    [img]/forums/images/smilies/cool.gif[/img] It works <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    cheers
    Alan

  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: Calling Word macro through automation. (2000 s

    It's interesting: when there is no parameter and the procedure name is unique and within the document's "scope," you usually don't have to add any qualification. But start to vary these things, and it's very hit or miss. I feel good about the above method working for the attached template, but I'm less confident about global templates. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  7. #7
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Word macro through automation. (2000 s

    I appears to work (so far) for templates referenced in the VBA project as well.

    The end usage of what I'm writing will probably be the production of batches of Word documents, each generated from within the same XL procedure. For this reason, I'm wondering if it's possible to reuse a running instance of Word (when available) rather than firing off one per document with "Set oWord = New Word.Application". I'm not sure it would be possible or easy to grab a reference to a running instance. But maybe it's possible to create just one instance from XL, check to see if it's still running each time, then either reuse it or run a new instance as appropriate?

    Alan

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Calling Word macro through automation. (2000 s

    You can use GetObject to try to get hold of a running instance of Word, and use CreateObject if that fails. See <post#=446,288>post 446,288</post: >.

  9. #9
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Word macro through automation. (2000 s

    Thanks Hans. I'll give that method a try. Just out of interest, does
    Set wrdApp = GetObject(, "Word.Application")

    grab the first running instance found that matches "Word.Application" from the process IDs? Or is there a collection or similar of all running instances? Not that it's important, just curious.

    Alan

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Calling Word macro through automation. (2000 s

    Unless you explicitly create a new instance of Word in code, it's unusual to have more than one instance running. If you start Word several times interactively, it'll use the already running instance. But yes, GetObject just grabs the first instance it finds.

  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: Calling Word macro through automation. (2000 s

    From my experience using GetObject with IE, the selection might reach for the first opened instance, or the last accessed, or... I actually gave up trying because I wanted to grab a particular open web page, and it just wasn't reliable. With your application, since you are creating your own application, any instance should be as good as any other. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

Posting Permissions

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