Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 4 of 4

Thread: how to share variable between Excel VBA & Word VBA

  1. #1
    New Member
    Join Date
    Feb 2002
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi.

    I am using Excel 2000 to Control Word 2000 for publication generation. I will need to pass certain variable from Excel VBA to Word VBA (I use Word VBA's Autoexec module to run the script once Excel opens the Word document). Does anyone know how to do it?

    thanks,

    John

  2. #2
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    2,940
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default

    One way you could do it is this:-

    In Word use something like this in a standard module:-

    Code:
    Sub WordMacro(VariableFromXL As String)
    MsgBox VariableFromXL
    End Sub
    and in Excel use something like this:-

    Code:
    Sub OpenWordAndPassVariable()
    Dim wdApp As Word.Application
    
    YourVariable = "Hello there world!"
    
    Set wdApp = CreateObject("Word.Application")
    
    wdApp.Documents.Open "C:tempwordfile.doc"
    wdApp.Visible = True
    
    wdApp.Run "WordMacro", YourVariable
    
    End Sub
    You can pass up to 30 arguments using the Run method.

    Also, the code above assumes you've set a reference to the Word object library in Tools, References.

    HTH,
    D

  3. #3
    New Member
    Join Date
    Feb 2002
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thanks for your reply!

    Are these codes for Excel 2000 or Excel 97.

    I have Excel 97 and also click "Microsoft Word 8.0 Libary" but when I run the code I get an error message "Compile error: wrong number of arguments or invalid property assignment" at the line:
    wdApp.Run "WordMacro", YourVariable
    (.Run is highlighted).

    Could you tell me why?

    Thanks,

    John

  4. #4
    Board Regular
    Join Date
    Mar 2008
    Location
    Berkshire, England
    Posts
    335
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: how to share variable between Excel VBA & Word VBA

    Thanks for this - worked like a dream!

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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