![]() |
Handling Errors When Opening Word Docs From Excel VBA
Hi All
I have written some logic in Excel VBA which opens Word files and then does some processing. I am having trouble catching all the errors from opening the Word files - my code is: On Error GoTo OpenError Word.Application.DisplayAlerts = False Word.Application.Documents.Open sFullPathAndName, ReadOnly:=True bOpenedOK = True Examine_ActiveDocument Word.Application.Documents(fTarget.Name).Close False OpenError: If Not bOpenedOK Then Record_Error When my code tries to open a corrupted Word file (which it cannot open) the error handling above does not catch it and the error fails my search. I, of course, want it to keep running until it has examined all the Word files. When you manually try to open these files Word says "Word experienced an error trying to open the file." Why does my error handling not catch this case? Thanks in advance for any ideas or suggestions, Chrisso |
Handling Errors When Opening Word Docs From Excel VBA
Add this line to your declaration section:
Dim myDoc as Word.Document Change your Open line to: Set myDoc = Word.Application.Documents.Open sFullPathAndName, ReadOnly:=True Add this code: If myDoc Is Nothing Then Record_Error On Error GoTo 0 Hope that helps. On Jan 24, 5:24 am, Chrisso wrote: Hi All I have written some logic in Excel VBA which opens Word files and then does some processing. I am having trouble catching all the errors from opening the Word files - my code is: On Error GoTo OpenError Word.Application.DisplayAlerts = False Word.Application.Documents.Open sFullPathAndName, ReadOnly:=True bOpenedOK = True Examine_ActiveDocument Word.Application.Documents(fTarget.Name).Close False OpenError: If Not bOpenedOK Then Record_Error When my code tries to open a corrupted Word file (which it cannot open) the error handling above does not catch it and the error fails my search. I, of course, want it to keep running until it has examined all the Word files. When you manually try to open these files Word says "Word experienced an error trying to open the file." Why does my error handling not catch this case? Thanks in advance for any ideas or suggestions, Chrisso |
All times are GMT +1. The time now is 01:24 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com