how to share variable between Excel VBA & Word VBA

johnou

New Member
Joined
Feb 27, 2002
Messages
4
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
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
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
 
Upvote 0
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
 
Upvote 0
Re: how to share variable between Excel VBA & Word VBA

Thanks for this - worked like a dream!
 
Upvote 0

Forum statistics

Threads
1,214,617
Messages
6,120,541
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top