Help with variables!!!

A

angellijah

I need to take variables entered in an input box and use them in other
procedures within the project. After many failed attempts, I have this, but
it doesn't reset the variables with each instance. Any ideas? I tried Public
variables but it said I couldn't use them with a Sub or Function.

Public Sub filenames()

Dim WeekNumber, RegionName, Month, WeekNumberBox, RegionNameBox, MonthBox
WeekNumberBox = "Enter the fiscal two-digit week number"
RegionNameBox = "Enter the first three letters of the region name"
MonthBox = "Enter the first three letters of the current month"

'assign results
WeekNumber = InputBox(WeekNumberBox, , , 400, 400)
RegionName = InputBox(RegionNameBox, , , 400, 400)
Month = InputBox(MonthBox, , , 400, 400)

'assign results to document variables
For Each aVar In ActiveDocument.Variables
If aVar.Name = "weeknum" Then weeknum = aVar.Index
Next aVar
If weeknum = "" Then
ActiveDocument.Variables.Add Name:="weeknum", Value:=WeekNumber
Else
ActiveDocument.Variables(weeknum).Value = WeekNumber
End If

For Each aVar In ActiveDocument.Variables
If aVar.Name = "regname" Then regname = aVar.Index
Next aVar
If regname = "" Then
ActiveDocument.Variables.Add Name:="regname", Value:=RegionName
Else
ActiveDocument.Variables(regname).Value = RegionName
End If

For Each aVar In ActiveDocument.Variables
If aVar.Name = "mnth" Then mnth = aVar.Index
Next aVar
If mnth = "" Then
ActiveDocument.Variables.Add Name:="mnth", Value:=Month
Else
ActiveDocument.Variables(mnth).Value = Month
End If
 
D

Doug Robbins - Word MVP

You do not need to test for the existence of document variables to assign a
value to them.

Your code could be reduced to:

With ActiveDocument
.Variables("weeknum").value = InputBox("Enter the fiscal two-digit week
number", , , 400, 400)
.Variables("regname").value = InputBox("Enter the first three letters of
the region name", , , 400, 400)
.Variables("mnth").value = InputBox("Enter the first three letters of
the current month", , , 400, 400)
End With

or you could save your user one step by using

With ActiveDocument
.Variables("weeknum").value = InputBox("Enter the fiscal two-digit week
number", , , 400, 400)
.Variables("regname").value = InputBox("Enter the first three letters of
the region name", , , 400, 400)
.Variables("mnth").value = Format(Date, "mmm")
End With

To reuse a value that has been stored in the above manner, use

Dim month as String
month = ActiveDocument.Variables("mnth").Value

Today, month would then contain "Nov"

Instead of using multiple InputBoxes which will drive a sane user to
insanity, it would probably be better to use a userform.

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

and the following pages of fellow MVP Greg Maxey's website :

http://gregmaxey.mvps.org/Create_and_employ_a_UserForm.htm

http://gregmaxey.mvps.org/Populate_UserForm_ListBox.htm
If that information is of use to you, please do consider contributing to the
maintenance of that website to ensure its continued availability.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top