gpt4 book ai didi

loops - VBscript错误处理-移至 “For Each”循环中的下一项

转载 作者:行者123 更新时间:2023-12-03 07:47:50 28 4
gpt4 key购买 nike

我有一个循环,处理文件夹的每个文件。万一文件损坏而无法打开,我想:
-向用户显示错误消息
-停止执行剩余的代码
-循环到下一个文件

问题是,如果无法在循环开始时打开文件,则仍有许多代码将要执行,这将引发其他错误。我无法退出循环,因为这意味着并非所有文件都会得到处理,因此我无法继续执行代码。请参阅下面的代码部分。

Set fso = CreateObject("Scripting.FileSystemObject")
For each objFile in fso.GetFolder(".").Files
If lCase(fso.GetExtensionName(objFile)) = srcExtName Then
Set app = GetNewAppInstance (Program)
Set fileHandle = GetNewFileHandler(app)

On Error Resume Next
Set objOpenFile = fileHandle.Open(objFile.path)
If err.number <> 0 Then
next
MsgBox "The file " & objFile.name & " cannot be opened. Please verify that the file is not corrupted or locked." & vbNewLine & "The file will be skipped."
Set fileHandle = nothing : app.Quit : Set app = nothing
<<< How can I jump to the next file from here ? >>>
End If
On error goto 0
strSrcPath = fso.BuildPath(objOpenFile.path, fso.GetBaseName(objOpenFile.name) & "._temp")
objOpenFile.SaveAs strSrcPath, tmpExtNum
objOpenFile.Close

Set objOpenFile = nothing
Set fileHandle = nothing
app.Quit : Set app = nothing

Set app = GetNewAppInstance (Program)
Set fileHandle = GetNewFileHandler(app)

Set objOpenFile = fileHandle.Open(strSrcPath)
strTgtPath = fso.BuildPath(objOpenFile.path, fso.GetBaseName(objOpenFile.name) & "." & srcExtName)
objOpenFile.SaveAs strTgtPath, srcExtNum
objOpenFile.Close
fso.DeleteFile(strSrcPath)

Set objOpenFile = nothing
Set fileHandle = nothing
app.Quit : Set app = nothing
End If
Next
End Sub

谢谢!

最佳答案

尝试这个:

Set fso = CreateObject("Scripting.FileSystemObject")
For each objFile in fso.GetFolder(".").Files
If lCase(fso.GetExtensionName(objFile)) = srcExtName Then
Set app = GetNewAppInstance (Program)
Set fileHandle = GetNewFileHandler(app)

On Error Resume Next
Set objOpenFile = fileHandle.Open(objFile.path)
If err.number <> 0 Then
MsgBox "The file " & objFile.name & " cannot be opened. Please verify that the file is not corrupted or locked." & vbNewLine & "The file will be skipped."
Set fileHandle = nothing : app.Quit : Set app = nothing
Else
strSrcPath = fso.BuildPath(objOpenFile.path, fso.GetBaseName(objOpenFile.name) & "._temp")
objOpenFile.SaveAs strSrcPath, tmpExtNum
objOpenFile.Close

Set objOpenFile = nothing
Set fileHandle = nothing
app.Quit : Set app = nothing

Set app = GetNewAppInstance (Program)
Set fileHandle = GetNewFileHandler(app)

Set objOpenFile = fileHandle.Open(strSrcPath)
strTgtPath = fso.BuildPath(objOpenFile.path, fso.GetBaseName(objOpenFile.name) & "." & srcExtName)
objOpenFile.SaveAs strTgtPath, srcExtNum
objOpenFile.Close
fso.DeleteFile(strSrcPath)

Set objOpenFile = nothing
Set fileHandle = nothing
app.Quit : Set app = nothing
End If
On error goto 0
End If
Next

我已经将代码的“其余部分”移到了if语句的else块中,因此只有在没有错误的情况下它才会执行。

关于loops - VBscript错误处理-移至 “For Each”循环中的下一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20099407/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com