Consulting

Results 1 to 6 of 6

Thread: Solved: vbscript call to VBA Word macros?

  1. #1
    VBAX Newbie
    Joined
    Jan 2005
    Posts
    3
    Location

    Solved: vbscript call to VBA Word macros?

    Hi, Sorry to be a non-programmer here; I presumed it would be esay, but just cannot find how (if possible) to call a Word macro from a vbscript; can it be done anyway?
    Thanks, Randall

  2. #2
    VBAX Regular zilpher's Avatar
    Joined
    Nov 2004
    Location
    Swindon, UK
    Posts
    30
    Location
    Here's an example:

    [VBA]Dim Word
    Dim WordDoc
    Set Word = CreateObject("Word.Application")

    ' Make Word visible
    Word.Visible = True

    'Open the Document
    Set WordDoc = Word.Documents.open("c:\adocument.doc")

    'Run the macro called foo
    Word.Run "foo"

    ' Close Word
    Word.Quit
    'Release the object variables
    Set WordDoc = Nothing
    Set Word = Nothing[/VBA]

    HTH

  3. #3
    VBAX Newbie
    Joined
    Jan 2005
    Posts
    3
    Location
    Thanks so much; I have now managed to call a macro taken from this site (making a list of files; Showlist). Is it possible to keep Word hidden and close after running, or even only open VBA and not Word??)

    Thanks again, Randall

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    [VBA]Word.Visible = True [/VBA]
    That line makes Word visible. Just remove it if you want to keep Word hidden.

  5. #5
    VBAX Regular zilpher's Avatar
    Joined
    Nov 2004
    Location
    Swindon, UK
    Posts
    30
    Location
    You can't open vba on it's own, it requires a host application, in this case word.

    To keep Word from showing, comment out the line Word.Visible = True.

    If you're going to do that, I'd turn on error handling (On Error Resume Next) to ensure the Word application closes, although that depends on how well your Word macro handles errors too.

    You could also trap errors and report them using the Err object. There's a reasonable vbscript write up on error trapping here:

    http://www.juicystudio.com/tutorial/....asp#errhandle

  6. #6
    VBAX Newbie
    Joined
    Jan 2005
    Posts
    3
    Location
    Hi, thanks!

    What about using "Getobject" instead of "CreateObject" if Word is already running? Would that work just as well?

    Thanks, Randall

Posting Permissions

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