Consulting

Results 1 to 8 of 8

Thread: Solved: Run a Word VBA from Excel VBA

  1. #1
    VBAX Regular
    Joined
    Sep 2005
    Posts
    34
    Location

    Solved: Run a Word VBA from Excel VBA

    Hi,

    I'm writing some code for Excel which will run through a list of file names and open and print them. Some of the files are Excel, some Word, some PDF etc.

    I've got the code for doing this and I'm really pleased.

    The problem I have is that when a couple of the Word files open I actually want to run a Word macro (to be found in normal.dot) and when that finishes the Excel code continues.

    How do I get the Word code to run?

    Thanks.

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi chrismc,

    Try looking up the Run method. It can be used to run macros in other files. I believe that the following should work for you for a word document. (Obviously, you'll need to update the path to normal.dot though.
    [vba]Application.Run "C:\Test.doc!MyMacro"[/vba]

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    VBAX Regular
    Joined
    Sep 2005
    Posts
    34
    Location
    Thanks - I'll try it.

    Chris

  4. #4
    VBAX Regular
    Joined
    Sep 2005
    Posts
    34
    Location
    This is what worked for me using your idea about run.


    [VBA]
    Sub FromExcel()
    Dim wdApp As Object
    Set wdApp = GetObject(, "Word.Application")
    wdApp.documents.Open "c:\temp\go.doc" 'This is the document containing the macro
    wdApp.Run "mymsg" 'This is the macro to run
    wdApp.documents.Close
    Set wdApp = Nothing
    End Sub

    [/VBA]

  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Chris,

    My apologies for not mentioning that you'd probably need to bind to the Word instance to run it. Glad you got it figured out.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  6. #6
    VBAX Regular
    Joined
    Sep 2006
    Posts
    13
    Location
    Hi,

    I'm fairly new to VBA and have a questions regarding the above code.

    I've edited the code so the target file is a different one but I'm getting a runtime code when it runs (or doesn't!)

    ActiveX component can't create object or return reference to this object (Error 429)

    and was wondering what this was to do with and if you have any solutions to solve it.

    Thanks much

    T

  7. #7
    VBAX Regular
    Joined
    Sep 2005
    Posts
    34
    Location
    Hi Trubble,

    I've had error 429 in some of my code. Usually when trying to get Excel to do something with Outlook when Outlook isn't running.

    I honestly can't remember details so hopefully someone more switched than I am will give you a better answer. I think you need to do a search on the forum for late binding and early binding. These are two slightly different ways of getting applications to talk to each other. Depending which you use I think you have to set up references in VBA (Tools, References, select relevant Object Libraries).

    I hope this points you in the right direction.

    Chris

  8. #8
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi there,

    There is a brief discussion on early vs late binding on my site. The article also links to a more detailed explanation of how to program it at Bob Phillips site.

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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