Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS-Word Passing Variables in VBA

Status
Not open for further replies.

MyFlight

Technical User
Feb 4, 2002
193
I am trying to pass a Variable between Macros (Main and Analog) in MS-Word.
The user enters a Number (Cmr) in the message box. This is used to open multiple Files that start with this number.

The Complete filename are: 123456_Analog65.txt and 123456_Digtial65.txt etc...
This is only a small part of a large macro I want to use on multiple files

Example Macro:

Sub Main()
Dim Message, Title, Default
Dim Cmr As String
Message = "Enter New File Name:" ' Set prompt.
Title = "File Will be saved with this Name" ' Set title.
Default = " " ' Set default.
Cmr = InputBox(Message, Title, Default)
Application.Run MacroName:="Analog"
Application.Run MacroName:="Digital"
end sub

Sub Analog()
Dim MyRawData As String
Dim MyReconfiguredData As String
Rawdir = "C:\"
Rawdata = "_Analog65.txt"
MyRawData = Rawdir + Cmr +Rawdata
MyReconfiguredData = "Analog65.txt"
ChangeFileOpenDirectory "C:\"
Documents.Open FileName:=MyRawData, ConfirmConversions:=False, ReadOnly _
:=False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate _
:="", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="" _
, Format:=wdOpenFormatAuto
end sub

The Main Problem is how to use the Variable (Cmr) from another Macro (Analog).

Any assistance will be greatly appreciated

 
Simply use a Public variable:
Public Cmr As String
Sub Main()
Dim Message, Title, Default
Message = "Enter New File Name:" ' Set prompt.
Title = "File Will be saved with this Name" ' Set title.
Default = " " ' Set default.
Cmr = InputBox(Message, Title, Default)
Application.Run MacroName:="Analog"
Application.Run MacroName:="Digital"
End Sub

Sub Analog()
Dim MyRawData As String
Dim MyReconfiguredData As String
Rawdir = "C:\"
Rawdata = "_Analog65.txt"
MyRawData = Rawdir + Cmr +Rawdata
MyReconfiguredData = "Analog65.txt"
ChangeFileOpenDirectory "C:\"
Documents.Open FileName:=MyRawData, ConfirmConversions:=False, ReadOnly _
:=False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate _
:="", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="" _
, Format:=wdOpenFormatAuto
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
You can make Cmr an argument in the subroutine, Analog:
Sub Analog(Cmr),
then call it using that argument:
Analog Cmr (instead of Application.Run MacroName:="Analog"

You could also declare Cmr as a global variable:
Dim Cmr As String
Sub Main()
'don't declare Cmr again
...
end sub
sub Analog()
...
end sub


_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top