I have a macro that saves PDF files for each sheet in my drawing. I'm having a problem with it if it tries to save over an existing file with the same name. I think it may be an issue with Windows, but I'm not really sure what is happening. Here is the section of code that deals with saving my files:
'Check if existing file can be overridden
On Error Resume Next 'Skip file deletion on error
oFSO.DeleteFile strNewPath & strNewFileName 'Delete existing file
On Error GoTo 0 'Disable error handler
If oFSO.FileExists(strNewPath & strNewFileName) = True Then 'Check if file was deleted
'Message user that existing file can not be overridden
swApp.SendMsgToUser2 "Unable to override existing file. (" & strNewFileName & ")", swMbStop, swMbOk
Debug.Print "**Unable to override existing file (" & strNewFileName & ")**"
Else 'If file was deleted
'Set Sheet to export
bStatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, strSheetName)
'Save Sheet as PDF
swModel.Extension.SaveAs strNewPath & strNewFileName, swSaveAsCurrentVersion, _
swSaveAsOptions_UpdateInactiveViews, swExportPDFData, lngErrors, lngWarnings
If lngErrors = 0 Then 'Check if error occured during save
Debug.Print " New File Name: " & strNewFileName'Add new filename to list of new files
frmNewFiles.lboFiles.AddItem strNewFileName
Else 'If error occured
'Send error message to User
swApp.SendMsgToUser2 "Error occured on sheet """ & vSheets(i) & """." _
& vbCrLf & "No file created.", swMbWarning, swMbOk
End If
End If
When it gets to the first IF statement, the macro will sometimes see the file as deleted, even though I can see that the file still exists in Windows Explorer. The "SaveAs line gets run with no errors, so no messages ever go to the user that the file was not saved correctly. Looking in Windows Explorer, the original file is still there.
This doesn't happen everytime that I file needs to be overridden. Most of the time, the macro works as expected. I have tried some different things out, and it seems to mostly occur when the file has been opened and closed shortly before the macro is run. It also only seems to occur if Windows Explorer is currenly open to the folder that holds the file to be replaced.
Could this have something to do with Windows 7 caching or indexing the file at the same time the macro is running? Why isn't the FileExists code recognizing that the file still exists?