Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

Handling an Error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Maxey - 17 Dec 2004 10:46 GMT
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

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Helmut Weber - 17 Dec 2004 12:31 GMT
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/
Greg - 17 Dec 2004 12:56 GMT
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
Helmut Weber - 17 Dec 2004 13:18 GMT
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/
Greg - 17 Dec 2004 13:23 GMT
Helmut,

Copy all.  Thanks
Jean-Guy Marcil - 17 Dec 2004 14:00 GMT
Greg Maxey was telling us:
Greg Maxey nous racontait que :

> Hello Masters,
>
[quoted text clipped - 10 lines]
> 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!

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Jay Freedman - 17 Dec 2004 14:50 GMT
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.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

> Hello Masters,
>
[quoted text clipped - 16 lines]
>
> Thanks
Greg - 17 Dec 2004 18:07 GMT
Jay, JGM

I am both schooled and humbled.  Thanks.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.