Cannot export if folder already exists

Started by goldnhue, October 22, 2010, 07:28:45 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

goldnhue

I have been exporting Visio files programatically using:

     pgObj.Export

But this generates an error if a folder already exists by the same name as the one I am exporting.
So I changed it to check for the folder first, and delete if it exists:

      Set fso = CreateObject("Scripting.FileSystemObject")
      If fso.FolderExists(folderdelete) Then fso.DeleteFolder folderdelete
      pgObj.Export

Problem is, sometimes, at random, the folder deletion does not complete before the export, so the export still generates an error.    Is there a way to ensure the folderdelete is finished before executing the export?


aledlund

I sometimes attempt a circumvention for this by putting an application.doevents between the two file operations. This allows queued events to get some work done before coming back to my code.
al

goldnhue

#2
Thanks, aledlund.
I tried a doevents (can't use application.doevents ... get: object doesn't support this method), but still get occasional error.

This time I'm getting error on the fso.deletefolder .... permission denied

Any other suggestions?

aledlund

It sounds like you're getting closer to the real problem (the delete never completed). My next step would be to make sure I have an errorhandler wrapped around it and put a stop when it fires. I'd then go check to the real status of the target file.
al

goldnhue

Thank you.
I still occasionally get the error.  Maybe I'm not trapping it correctly?  Code snippet below:

Private Sub ExportPage()
        Dim pathfilename as String, pgObj As Visio.Page
        pathfilename="ExportedFile&Path"
        Set pgObj = Visio.ActivePage

        ' Delete any existing folder by same name, then export the page
        delfolder (PathFileName)
        DoEvents      ' This doesn't help at all
     
        ' if error, try it one more time before exiting
        On Error GoTo Err2
        pgObj.Export PathFileName
     
Exit sub
Err2:
    delfolder (PathFileName)
    pgObj.Export PathFileName
    Resume Next
   
End sub

Private Sub delfolder(PathFileName)
        'Delete folder if it exists
        Dim fso, folderdelete
        folderdelete = Left(PathFileName, Len(PathFileName) - 4) & "_files"
        Set fso = CreateObject("Scripting.FileSystemObject")
        If fso.FolderExists(folderdelete) Then fso.DeleteFolder folderdelete
        Set fso = Nothing
End Sub

aledlund

I was thinking something more like this
al




Option Explicit

'
'
'
Private Sub doingSomething()

On Error GoTo ErrHandler


    Dim intRetryCt As Integer
    Set intRetryCt = 2
    Dim blnSucceed As Boolean
    blnSucceed = False
    Dim strFolder As String
    strFolder = Left(strPath, Len(strPath) - 4) & "_files"
    While 0 < intRetryCt
        blnSucceed = deleteFolder(strFolder)
        If blnSucceed Then GoTo DeleteSucceeded
        ' make sure we can get out
        intRetryCt = intRetryCt - 1
    Loop
    If blnSucceed = False Then GoTo ErrHandler

DeleteSucceeded:

    MsgBox "got it "
    Exit Sub

ErrHandler:
    Dim strMsg As String
    strMsg = Err.Description
    Debug.Print strMsg
    MsgBox "This is in trouble " & strMsg

End Sub

'
'
'
Private Function deleteFolder _
    (ByVal strPath As String) _
    As Boolean
   
    On Error GoTo ErrHandler
   
    Dim blnReturn As Boolean
    blnReturn = False
   
    Dim strFolder As String
    Dim fsoFolder As Folder
    Dim fso As Scripting.FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim blnFolderExists As Boolean
    blnFolderExists = False
    blnFolderExists = fso.FolderExists(strPath)
    If blnFolderExists = True Then
        fsoFolder = fso.GetFolder(strFolder)
        ' check to see the folder is empty
        If 0 < fsoFolder.Files.Count Then
            ' delete the files first
            If deleteFiles(strPath) = False Then GoTo ErrHandler
            ' then delete the folder
            fsoFolder.Delete
            blnReturn = True
        Else
        ' folder is empty go delete it
        fsoFolder.Delete
        blnReturn = True
        End If ' test for files in the folder
    Else
        ' folder does not exist so we pretend that we got rid of it
        blnReturn = True
    End If ' test for folder exists
    Set fso = Nothing
    deleteFolder = blnReturn
    Exit Function
ErrHandler:
    Debug.Print "delete Folder " & Err.Description
    Set fso = Nothing
    deleteFolder = False
End Function

Private Function deleteFiles _
    (ByVal strPath As String) _
    As Boolean
   
    On Error GoTo ErrHandler
   
    Dim strFolder As String
    Dim fsoFolder As Folder
    Dim fsoFile As File
    Dim fso As Scripting.FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim blnFolderExists As Boolean
    blnFolderExists = False
    blnFolderExists = fso.FolderExists(strPath)
    If blnFolderExists = True Then
        Set fsoFolder = fso.GetFolder(strPath)
        For Each fsoFile In fsoFolder.Files
            fsoFile.Delete
        Next fsoFile
    End If ' test for folder exists
   
    Set fsoFile = Nothing
    Set fsoFolder = Nothing
    deleteFiles = True
    Exit Function
ErrHandler:
    Debug.Print "deleteFiles " & Err.Description
    Set fsoFile = Nothing
    Set fsoFolder = Nothing
    deleteFiles = False
End Function


aledlund

typo in the code
   fsoFolder = fso.GetFolder(strFolder) s/b    fsoFolder = fso.GetFolder(strPath)

goldnhue

#7
Thank you, al.
I tried the suggested code, but I still somtimes get the error. .... always on the same source line:  
pgObj.Export PathFileName

So I started monitoring the Internet Explorer directory while the code ran and it appears that the error sometimes occurs even if the folder is deleted/doesn't exist ... but this may just be a delay in code execution that I sometimes cannot see the folder deletion, or it may be something fluky about the pgObj.Export.

It is random occurance and I cannot isolate any unique factors ... I test running the exact same export over and over on the exact same page, it may work several times, then suddenly I get an error.  This is driving me crazy!  The only factor I have been able to isolate is, everything ran fine before moving to a Windows 7, 64 bit PC, Visio 2010 ... I'm wondering if anyone else has exprienced a similar problem with exporting in this environment.

UPDATE:  I tested the exact same code with no errors using Visio 2007 on an XP computer, also on a Vista computer.
But when running exact same code using Visio 2010 on a Windows 7 32 bit and 64 bit computers, the error occurs.  So error appears to be related to either Visio 2010 and/or Windows 7.  

The only work-around that I have found is adding an error trap that pops up a message box where the user has to press a button to proceed ... seems to give it enough time to do whatever is needed.  

If anyone has similar issue and finds a solution, please let me know!

Thanks.
   

Darryll

I've got the exact same problem in Visio 2010. My code worked fine in 2007. I need to get it working again today so if I find a solution I'll post it.

goldnhue

Thanks, Darryll, and good luck!

Until solution is found, I'm using an awkward work around:
I first run a folder exists check and delete if it does before running the export.  If an error occurs, it goes to an error trap that runs the folder check/delete again, but then pops up a message box prompting the user to press OK to proceed, after which it then resumes next.

Hope you can find a better solution.

Darryll

Couldn't find a solution to this issue which is just annoying. I've reverted to using 2007 for exporting.

goldnhue

So I can further isolate the variables, may I ask what Windows version you are using? And are you on 32 or 64 bit PC?