Hello,
I have an odd issue.
In VBA, I first delete a file (e.g. FileName.pdf) and then rename another file (e.g. FileName2.pdf) to the name of the deleted file (e.g. FileName.pdf):
The code snippet is:
This works fine as long as not switching my notebook from power plan “Balanced” to “Power Saver”.
If I do this then the rename function fails.
The code of the function is:
In “Power Save” mode, the rename fails and returns a time out.
If I add “sleep 250” between the delete and the rename then it works OK, but I don't want to slow down the overall functionality. Additionally, it could be that 250 ms is not enough if run on a slower machine. So, sleep is not really an option.
What is really odd, is that if I remove “On Error GoTo lblErr” then the rename works! But I don’t want to remove the error handling.:banghead:
Somebody got an idea how to solve that?
Regards
Christian
I have an odd issue.
In VBA, I first delete a file (e.g. FileName.pdf) and then rename another file (e.g. FileName2.pdf) to the name of the deleted file (e.g. FileName.pdf):
The code snippet is:
Code:
varTemp = funDeleteFile(strPath & "" & strFile)
If varTemp <> gcstrfunDeleteFile_Success Then
funPrintISS = funPrintISS & " " & varTemp & "."
GoTo lblExit
End If
varTemp = funRenameFile(strMergeFile, strPath & "" & strFile)
If varTemp <> gcstrfunRenameFile_Success Then
funPrintISS = funPrintISS & " " & varTemp & "."
GoTo lblExit
End If
This works fine as long as not switching my notebook from power plan “Balanced” to “Power Saver”.
If I do this then the rename function fails.
The code of the function is:
Code:
Function funRenameFile(sSourceFile As String, sDestinationFile As String)
Dim lngTimeElapsed As Long
Const clngTimeWait As Long = 500 ' [ms]
Const clngTimeMax As Long = 10000 ' [ms]
funRenameFile = gcstrfunRenameFile_Error
On Error GoTo lblError
Name sSourceFile As sDestinationFile
lblRetry:
' Wait until the file is renamed
lngTimeElapsed = 0
Do Until (Dir(sDestinationFile) <> "")
Sleep clngTimeWait
lngTimeElapsed = lngTimeElapsed + clngTimeWait
' it takes too long -> error
If (lngTimeElapsed >= clngTimeMax) Then
' set return value = error message
funRenameFile = "TIMEOUT (" & lngTimeElapsed & " ms) deleting file."
GoTo lblExit
End If
DoEvents
Loop
' regular end
funRenameFile = gcstrfunRenameFile_Success
lblExit:
On Error Resume Next
DoCmd.Hourglass False
Exit Function
' --- error-handling routines --- --- --- --- --- --- ---
lblError:
If (lngTimeElapsed >= clngTimeMax) Then
funRenameFile = funRenameFile & " " & " <Error (" & Err.Number & "): " & Err.Description & ">"
Resume lblExit
Else
Sleep clngTimeWait
lngTimeElapsed = lngTimeElapsed + clngTimeWait
Resume lblRetry
End If
End Function
In “Power Save” mode, the rename fails and returns a time out.
If I add “sleep 250” between the delete and the rename then it works OK, but I don't want to slow down the overall functionality. Additionally, it could be that 250 ms is not enough if run on a slower machine. So, sleep is not really an option.
What is really odd, is that if I remove “On Error GoTo lblErr” then the rename works! But I don’t want to remove the error handling.:banghead:
Somebody got an idea how to solve that?
Regards
Christian