I have a process in Excel that adds images from a PDF file with the following code:
valRef = "TabName" FilePath = "\network\file.pdf" ' Process stops in the next line Worksheets(valRef).OLEObjects.Add Filename:=FilePath, Link:=False, DisplayAsIcon:=False
Some times, the process stops when adding the PDF file with the error ‘Run-time error ‘1004’ Cannot insert object
When the debug window comes up, I can press F5 and the process continues. The PDF file exists and with the same file, in the same network location, some times works without stopping.
Any ideas on how to prevent the process to stop?
I know it looks ugly, but you could keep retrying with an error handler
if 1 = 0 then 'never enter this section, unless an error occurs looperror: Resume 'without this, the second time an error occurs, it will trigger, ignoring the on error end if on error goto looperror Worksheets(valRef).OLEObjects.Add Filename:=FilePath, Link:=False, DisplayAsIcon:=False on error goto 0
If you don’t want this to go on endlessly, you could add a counter and cancel after a few tries, or ask the user via msgbox if they want to retry.