Need Help with a Conditional

G

Greg Maxey

Hi,

A little while ago I was trying to figure out how to hyperlink to another
document while closing and saving changes to the document containing the
hyperlink. My solution was an AutoOpen() macro in the destination document
containing the following code.

AutoOpen()



End Sub

This worked fine whenever I was hyperlinking to this document from the
Reports.doc however if I just tried to open the document I was getting an
error because Reports.doc wasn't open.

I need to to alter my AutoOpen macro such the it functions like

If Report.doc is Open Then
Documents("Report.doc").Close SaveChanges:=wdSaveChanges
Else End

I don't have a clue how this would be done.

Thanks.
 
J

Jonathan West

Hi Greg,

2 options to this

1. Cycle through the Documents collection to see if report.doc is open.

2. Use the On Error command to trap the error and act accordingly if
report.doc wssn't open.
 
G

Greg Maxey

Jonathan,

I can read your words but I don't know how to execute. Well at least one of
the methods I can nuke out. I still take baby steps with VBA. It is mostly
trial and error stuff or Monkey see ... Monkey do.

I made the following work:

Sub AutoOpen()

On Error GoTo Ignore
Documents("Report.doc").Close SaveChanges:=wdSaveChanges
Ignore:

End Sub

How would I go about cycling through the Documents Collection?

Thanks.
 
J

Jonathan West

Greg Maxey said:
Jonathan,

I can read your words but I don't know how to execute. Well at least one of
the methods I can nuke out. I still take baby steps with VBA. It is mostly
trial and error stuff or Monkey see ... Monkey do.

I made the following work:

Sub AutoOpen()

On Error GoTo Ignore
Documents("Report.doc").Close SaveChanges:=wdSaveChanges
Ignore:

End Sub

That is the right general idea. If you don't want to the code to carry on to
the Ignore point if there isn't an error, then you need to goto sowhere else
at that point, or maybe do an Exit Sub, so that the wrror-handling code is
dseparated from the other code. But I suspect in this particular acse it
isn't necessary, this is just a thought for the future.
How would I go about cycling through the Documents Collection?

Try this

Dim oDoc as Document
For Each oDoc in Documents
If oDoc.Name = "Report.doc" Then
oDoc.Close SaveChanges:=wdSaveChanges
Exit For
End If
Next oDoc
 
G

Greg Maxey

Jonathan,

Your explanation of the GoTo method was a little muddled
for me, but I see how the cycle method works. Thanks for
your time and help.

R/Greg
 
J

Jonathan West

Greg Maxey said:
Jonathan,

Your explanation of the GoTo method was a little muddled
for me, but I see how the cycle method works. Thanks for
your time and help.

OK, just to try & clarify that. You have the On Error Goto part all right.
The most common arrangement is to put the error-handling code off at the
bottom of the routine, so you have your label near the bottom of the
routine, and the error-handling code immediately after it.

However, you generally *don't* want the error-handling code to run if there
was no error! Therefore, you have to stop the code from carrying on down
through to the error-handling section at the end. The normal way of thing
this is to put an Exit Sub (or Exit Function) command immediately before the
label. That way, the normal code execution won't ever get to the
error-handling section. The code layout is like this

On Error Goto Errhandle
'normal code goes here

Exit Sub

Errhandle:
'Error-handling code goes here

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