WORD VARIABLES NOT CLEARING

J

JJ

I have a huge vba project in word. I previously had a variable called
"ELet_Heading" in my .dot document. I renamed this to "ELet_RE" and
added a few more variables. When I run the macro its seems to remember
what the previous variables were called and do not count through all
variables although I use the following in VBA:
For Each avar In ActiveDocument.Variables
If avar.Name = "ELet_RE" Then num = avar.Index
Next avar
If num = 0 Then
ActiveDocument.Variables.Add Name:="ELet_RE",
Value:=TextBox2.value
Else
ActiveDocument.Variables(num).Value = Textbox2.value
End If
I do this for each and every variable but when I do the test counting
the variables it only has 10 variables instead of for instance 15
variables and looking at the avar.Name I see that it doesn't run
through all the variable names. Is there a way of clearing / deleting /
removing the variables attached to that template so that it can see the
new variables as well.
 
J

Jean-Guy Marcil

JJ was telling us:
JJ nous racontait que :
I have a huge vba project in word. I previously had a variable called
"ELet_Heading" in my .dot document. I renamed this to "ELet_RE" and
added a few more variables. When I run the macro its seems to remember
what the previous variables were called and do not count through all
variables although I use the following in VBA:
For Each avar In ActiveDocument.Variables
If avar.Name = "ELet_RE" Then num = avar.Index
Next avar
If num = 0 Then
ActiveDocument.Variables.Add Name:="ELet_RE",
Value:=TextBox2.value
Else
ActiveDocument.Variables(num).Value = Textbox2.value
End If
I do this for each and every variable but when I do the test counting
the variables it only has 10 variables instead of for instance 15
variables and looking at the avar.Name I see that it doesn't run
through all the variable names. Is there a way of clearing / deleting
/ removing the variables attached to that template so that it can see
the new variables as well.

For this part of your code, you do not need to iterate through all
variables:
ActiveDocument.Variables("ELet_RE").Value = Textbox2.value
will create the variable if it does not exist, or change its value if it
does.

Similarly:
ActiveDocument.Variables("ELet_RE").Value = ""
will in effect delete the variable.

You only need to iterate if you want to read the content of the variable and
it may be possible that the variable does not exist yet. In such cases an
error would be generated, so iteration is necessary. Finally, when
iterating, it would make your code neater (and marginally faster) if you
added an Exit For, as in:

Dim num As Long
Dim avar as Variable

For Each avar In ActiveDocument.Variables
If avar.Name = "ELet_RE" Then num = avar.Index
Exit For 'No need to continue iteration, variable has been found
Next avar
If num = 0 Then
ActiveDocument.Variables.Add Name:="ELet_RE", Value:=TextBox2.value
Else
ActiveDocument.Variables(num).Value = Textbox2.value
End If

Finally, you can clear all variables with:

Dim avar As Variable

For Each avar In ActiveDocument.Variables
avar.Value= ""
Next avar



--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

JJ

JJ said:
I have a huge vba project in word. I previously had a variable called
"ELet_Heading" in my .dot document. I renamed this to "ELet_RE" and
added a few more variables. When I run the macro its seems to remember
what the previous variables were called and do not count through all
variables although I use the following in VBA:
For Each avar In ActiveDocument.Variables
If avar.Name = "ELet_RE" Then num = avar.Index
Next avar
If num = 0 Then
ActiveDocument.Variables.Add Name:="ELet_RE",
Value:=TextBox2.value
Else
ActiveDocument.Variables(num).Value = Textbox2.value
End If
I do this for each and every variable but when I do the test counting
the variables it only has 10 variables instead of for instance 15
variables and looking at the avar.Name I see that it doesn't run
through all the variable names. Is there a way of clearing / deleting /
removing the variables attached to that template so that it can see the
new variables as well.
 
J

JJ

Thanx for the info. I got the following solution to work.

With ActiveDocument.Variables
For i = .Count To 1 Step -1
.Item(i).Delete
Next i
End With
 
J

JJ

Thanx. Found the following solution which works well.
With ActiveDocument.Variables
For i = .Count To 1 Step -1
.Item(i).Delete
Next i
End With
 

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