ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Error Handler Not Working (https://www.excelbanter.com/excel-discussion-misc-queries/42326-error-handler-not-working.html)

Bill

Error Handler Not Working
 
Hello,

I have a macro that first tries to open a Word document with a particular
file name that is built from various pieces of data being processed by the
macro. Before attempting this, I have set the error handler:

For x = 1 to 5
On Error GoTo myOpenNewDoc
Set myDoc = myDocApp.Documents.Open(myDirectory & myFileName & x &
".doc")
GoTo Procede

myOpenNewDoc:

Set myDoc = myDocApp.Documents.Add
myDoc.SaveAs (myDirectory & myFileName & x & ".doc"

Procede:
<...
Next x

The idea is to open the document if it already exists, or to create it if it
does not. This seems to work when x = 1, but for each subsequent pass I get
the following error:

Run-Time Error '5174':
This file could not be found.
Try one or more of the following:
* Check the spelling of the name of the document.
* Try a different name
(C:\WorkingDirectory\ProjectA\...\ProjectA_Locatio n2.doc)

When I click "Debug" it highlights the first "Set" command above. This is
exactly the error I am trying to handle by jumping to myOpenNewDoc:. It
seems to be ignoring the "On Error" statement.

I think there is an easier way to do this, but regardless I want to know why
my error handler is being ignored. Does anyone have any advice?

Thanks,

Bill




All times are GMT +1. The time now is 02:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com