Handling an Error

G

Greg Maxey

Hello Masters,

Yesterday I was working to help an OP find a solution for placing a
truncated file name and path in the footer of her document. The method that
I proposed was to use a DOCVARIABLE field. During testing I noticed that
after the docvariable is added to the document that an error is generated if
the code is run a second time. I thought that I would simply delete the
variable after it has served its purpose but then realized that if the user
updated fields then there would be no reference. My solution was an as
follows:

On Error Resume Next
ActiveDocument.Variables.Add Name:="Path", Value:=myString
ActiveDocument.Variables("Path").Value = myString

Which I figure will add the variable if it isn't there or skip the add line
and refresh if the variable is already in the document. Is this the correct
way?

Thanks
 
H

Helmut Weber

Hi Submariner Greg,
I wonder whether I dare to answer,
as your question addresses the masters.
On Error Resume Next
ActiveDocument.Variables.Add Name:="Path", Value:=myString
ActiveDocument.Variables("Path").Value = myString

Anyway, to the best of my kowledge, if you always want to be
on the safe side, I'd check what kind of error occurred,
and reset the error afterwards.
Works with On Error GoTo 0, usually,
but only with undocumented On Error GoTo -1 in loops.

Like this:
Sub test444()
Dim s As String
s = "Hi Greg, how are you?"
On Error GoTo nextstep
With ActiveDocument.Variables
.Add Name:="Path", Value:=s ' strange path ;-)
nextstep:
MsgBox Err.Number
If Err.Number = 5903 Then
.Item("Path").Value = s
On Error GoTo -1
End If
End With
MsgBox Err.Number
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg

Helmut,

Thanks. If I understand correctly the code should appear as:

On Error GoTo Nextstep
With ActiveDocument.Variables
..Add Name:="Path", Value:=myString
Nextstep:
If Err.Number = 5903 Then
..Item("Path").Value = myString
On Error GoTo -1
End If
End With
 
H

Helmut Weber

Hi Greg

that will cover 999 of 1000 cases, I'd say.

By far good enough.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
Hello Masters,

Yesterday I was working to help an OP find a solution for placing a
truncated file name and path in the footer of her document. The
method that I proposed was to use a DOCVARIABLE field. During
testing I noticed that after the docvariable is added to the document
that an error is generated if the code is run a second time. I
thought that I would simply delete the variable after it has served
its purpose but then realized that if the user updated fields then
there would be no reference. My solution was an as follows:

On Error Resume Next
ActiveDocument.Variables.Add Name:="Path", Value:=myString
ActiveDocument.Variables("Path").Value = myString

I think Helmut answered regarding Error handling.
I just wanted to add my two bits regarding document variables.

On Error Resume Next
ActiveDocument.Variables.Add Name:="Path", Value:=myString
ActiveDocument.Variables("Path").Value = myString
can be reduced to
ActiveDocument.Variables("Path").Value = myString
because if the variable does not exist, it will be created, if it exists, it
will be overwritten.

Simple, no?
This is one of those many cases where the on-line VBA help is not actually
helping!

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

Jay Freedman

Hi Greg,

Actually, the full title is "Masters of the Universe" but I'll let it slide
this time. :)

You can avoid the whole issue by not using .Variables.Add at all. If you
execute the statement

ActiveDocument.Variables("Path").Value = myString

and the variable doesn't exist, VBA will silently add it to the collection
with the given value. If it does exist, the statement will change the value.

Similarly, to delete a variable, use the statement

ActiveDocument.Variables("Path").Value = ""

which won't cause an error if the variable doesn't exist, where the
statement

ActiveDocument.Variables("Path").Delete

would give a 5825 error.
 

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