Variable Question

M

mike

Hi,

I am trying to loop through a word document's variables with the following
code:

Dim objVar as Word.Variable

For Each objVar In objWord.ActiveDocument.Variables
objVar.Value = rs.Fields(objVar.Name)
Next

Is this possible?

Thanks
 
M

mike

I found the problem, it ActiveDocument.Variables.Count = 0 however when I do
this:

ActiveDocument.Variables("test") = "test"

the test variable exists. Why is that?

Thanks
 
D

Doug Robbins

That code is creating the variable.

Going back to what you started with, use

varname = objVar.Name
ActiveDocument.Variables(varname).Value = rs.Fields(varname)


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
J

Joey

I had a similar problem and used a for each avar statement

Dim MessageAffEndDay, TitleAffEndDay, DefaultAffEndDay
MessageAffEndDay = "at JOHANNESBURG on this the ....
day" ' Set prompt.
TitleAffEndDay = ""
DefaultAffEndDay = " " ' Set default.
AFFEND_DAY = InputBox(MessageAffEndDay,
TitleAffEndDay, DefaultAffEndDay)

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


It then adds the variable whether it exists or not. So in
essence the first bit is to have a message box to prompt
you to enter a reply and the for each avar bit adds the
variable to the active document whether it exists or not.

Hope this helps.
 
M

mike

My problem is that I have 20 to 30 variables already on the active document.
I do not understand why the count is zero but if I specify the variable by
name it exists. I want to be able to loop through the activedocument
variables so how can I do it?

Thanks
 
J

Joey

Hi Mike

Are you assigning a new variable every time for each
prompt that the user completes or are you trying to assign
a new value 20/30 times to the same variable name? What
num = 0 means is check whether the variable exist, if it
does, overwrite it, else assign the new value to it.

Look at the description below.

Here I assign variable names to a prompt box (message box)
a name, title and a default.

Here I declare my variable names
Dim MessageAffEndDay, TitleAffEndDay, DefaultAffEndDay

The message that will appear in the message box will
be "at JOHANNESBURG bla die bla"
MessageAffEndDay = "at JOHANNESBURG on this the ....
day" ' This will set the message at the top in the
message box

TitleAffEndDay = ""DefaultAffEndDay
= " " ' This will set default reply in the
box so that if they don't enter anything - blanks spaces
will be inserted where the variable should be inserted in
the document

This tells you that the information supplied should be
assigned to a variable called AFFEND_DAY. So the variable
must contain the info of the InputBox.
AFFEND_DAY = InputBox(MessageAffEndDay,TitleAffEndDay,
DefaultAffEndDay)

Now you will be saying if num = 0 (meaning if the variable
doesn't exist, add the variable to the active document. So
this loops through each doc variable in the document.

For Each aVar In ActiveDocument.Variables
If aVar.Name = "AFFEND_DAY" Then num = aVar.Index
Next aVar
If num = 0 Then
ActiveDocument.Variables.Add Name:="AFFEND_DAY",
Value:=AFFEND_DAY
Else

But if the variable does exist, add it in any event - so
overwrite the variable
ActiveDocument.Variables(num).Value = AFFEND_DAY
End If
 
M

mike

Thanks for the post. I am using docvariables to insert data from a
datsource. These variables are defined on the document and I want to loop
through activedocument.variables collection to insert the information. I
can hard code the insert by using activedocument.Variables("varName").Value
= newValue however I rather not hardcode it. Here is the code I am trying
to use:

Dim objVar as Word.Variable
For Each objVar In ActiveDocument.Variables
objVar.Range.InsertAfter (rsData.Fields(objVar.Name))
Next

Thanks
 
D

Doug Robbins

Maybe you did not read my response to your earlier post. Use

Dim objVar as Word.Variable, varname as string
For Each objVar In ActiveDocument.Variables
varname = objVar.Name
ActiveDocument.Variables(varname).Value = rsData.Fields(varname)
Next
ActiveDocument Fields.Update

Note a document variable does not have a .Range

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
J

Joey

Reading stuff from a database to add into a doc with
docvariables I did the following.

Under Modules made a new module called AutoExec so this
executes when you go into word. Your module must be called
AutoExec and the routine must be called Main. Read about
auto macros to learn more. The first part is where I read
info from the Registry and use in as doc variables and the
next reads from a database. I use 4 address fields and
decide if the one is blank what to do so if the address is
3 lines instead of 4 it also works and vice versa.

My database is called ADNDatabase.

Trust this will solve the problem.

Sub Main()

strADNDB = ""
strMASTPATH = ""

Dim strSection As String
strSection = "HKEY_CURRENT_USER\Software\XYZ Company
Name"
strADNDB = System.PrivateProfileString(FileName:="",
Section:=strSection, Key:="ADNDataBase")
strMASTPATH = System.PrivateProfileString
(FileName:="", Section:=strSection, Key:="ADNMastPath")

Set ADNDataBase = OpenDatabase(strADNDB)
Set rdCompInfo = ADNDataBase.OpenRecordset("CompInfo")

If IsNull(rdCompInfo.Fields(1).Value) = True Then
CoName = ""
Else
CoName = rdCompInfo.Fields(1).Value
End If
If IsNull(rdCompInfo.Fields(6).Value) = True Then
CoAdd1 = ""
Else
CoAdd1 = rdCompInfo.Fields(6).Value
End If
If IsNull(rdCompInfo.Fields(7).Value) = True Then
CoAdd2 = ""
Else
CoAdd2 = rdCompInfo.Fields(7).Value
End If
If IsNull(rdCompInfo.Fields(8).Value) = True Then
CoAdd3 = ""
Else
CoAdd3 = rdCompInfo.Fields(8).Value
End If
If IsNull(rdCompInfo.Fields(9).Value) = True Then
CoAdd4 = ""
Else
CoAdd4 = rdCompInfo.Fields(9).Value
End If

rdCompInfo.Close
ADNDataBase.Close

If (CoAdd1 <> "") And (CoAdd2 <> "") And (CoAdd3 <> "")
And (CoAdd4 <> "") Then
CoAdd = CoAdd1 & ", " & CoAdd2 & ", " & CoAdd3 & ", "
& CoAdd4
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd2 <> "") And (CoAdd3
<> "") Then
CoAdd = CoAdd1 & ", " & CoAdd2 & ", " & CoAdd3
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd2 <> "") And (CoAdd4
<> "") Then
CoAdd = CoAdd1 & ", " & CoAdd2 & ", " & CoAdd4
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd3 <> "") And
(CoAdd4 <> "") Then
CoAdd = CoAdd1 & ", " & CoAdd3 & ", " &
CoAdd4
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd2 <> "") Then
CoAdd = CoAdd1 & ", " & CoAdd2
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd3 <> "")
Then
CoAdd = CoAdd1 & ", " & CoAdd3
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd4
<> "") Then
CoAdd = CoAdd1 & ", " & CoAdd4
Documents.add
Else
If (CoAdd1 <> "") Then
CoAdd = CoAdd1
Documents.add
Else
CoAdd = " "
Documents.add
End If
End If
End If
End If
End If
End If
End If
End If

Dim X As New EventClassModule
Set X.app = Word.Application

End Sub
 

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