Results 1 to 6 of 6
  1. #1
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Running a macro from a different document (VBA/Word/2003 SP2)

    We've been down this road before (at least in the Excel version), but I still manage to lose my way. So please bear with me.

    The code in Module1 of CleanOutDoc.dot is reproduced below. The procedure "InternalTest" runs satisfactorily. <pre>Option Explicit
    '


    Public Sub InternalTest()
    Call CleanUp1
    Call CleanUp2(ThisDocument.Name)

    End Sub
    '
    '
    Public Sub CleanUp1()
    Dim TheFileToClose As String

    TheFileToClose = "No Passed Arguments"
    MsgBox TheFileToClose
    End Sub
    '
    '
    '
    Public Static Sub CleanUp2(TheFileToClose As String)
    '
    TheFileToClose = "One Argument Passed"
    MsgBox TheFileToClose
    End Sub
    </pre>


    A second document contains the code I have shown below here. From this code the the first Run command executes properly, but I get an error on the second.

    You will observe that there are two Run commands in this code that have been commented out. The first of these functions properly, while the second will not compile--it expects an equal sign. I am becoming more confused as time progresses. <pre>Option Explicit
    'Public Sub ExternalTest()
    '
    Dim myvarg1 As String
    Dim source1 As String
    Dim source2 As String
    '
    source1 = "CleanOutDoc.dot'!Module1.CleanUp1"
    source2 = "CleanOutDoc.dot'!Module1.CleanUp2"
    myvarg1 = "my word"
    Application.Run source1
    Application.Run source2, myvarg1
    '
    ' Application.Run (source1)
    ' Application.Run (source2, myvarg1)
    End Sub</pre>



    A snapshot of the Project Explorer is attached.

    Can someone please explain what's going on and hopefully clear up my lack of understanding.

    T.I.A.
    Attached Images Attached Images
    Regards
    Don

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

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

    Re: Running a macro from a different document (VBA/Word/2003 SP2)

    You could use

    Call Application.Run(source2, myvarg1)

    The statement

    Application.Run (source2, myvarg1)

    will not work because the ( ) make VBA think you're calling a function.

    By the way, in Word you don't have to specify the name of the document/template and module if the procedure name is unique Simply use

    source1 = "CleanUp1"
    source2 = "CleanUp2"

  4. #3
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Running a macro from a different document (VBA/Word/2003 SP2)

    Thank You Hans

    However your response brings more queries.

    Please see the attached Excel file for a tabular listing of my observations.

    The < source1 = "CleanUp1" > syntax produces inconsistent results on my machine. The results seem to be dependent on whether or not there is an argument associated with the called procedure

    The < Document.Module.Procedure > syntax does not work on my machine if the called procedure has an argument associated with it. . This along with the previous comment means that duplicate procedure names are not allowable if arguments are being passed.

    Can you duplicate my resuls? Or do I have an installation problem?

    I will run these tests on my desktop tomorrow and advise of the results.
    Attached Files Attached Files
    Regards
    Don

  5. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Re: Running a macro from a different document (VBA/Word/2003 SP2)

    On my home PC (Office XP), I could get it to work. On my work PC (Office 2003), I cannot get Application.Run to work with procedures in a module in a different document at all.
    I'd do the following instead:
    - Give the template a unique Project Name in Tools | <project> Properties...
    - Set a reference to the template in Tools | References...
    - You can now simply use

    Call MyTemplate.CleanUp1
    Call MyTemplate.Cleanup2(myvarg1)

    where MyTemplate is the project name of the template.

  6. #5
    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 a macro from a different document (VBA

    There are a few methods here and there that only pass parameters as variants. If Application.Run is one of those, you might try making the parameter for CleanUp2 a variant rather than a string.

    If you put unnecessary parentheses around a parameter in calling a procedure or function, VB passes it by value rather than by reference. (This seldom is important, but can create issues where you want the called procedure to be able to update the variable in the calling procedure.) The syntax "(var1, var2)" probably is not compatible with this hidden behavior.

  7. #6
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Running a macro from a different document (VBA

    Thank you Jefferson
    Regards
    Don

Posting Permissions

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