错误处理不适用于幻灯片 ID
Error handling not working for slide ID
在处理我的代码中的异常时遇到问题。我已经编写了下面的代码来将 powerpoint table 的内容传输到一个数组中,但它一直抛出异常说
Slides (unknown member): Invalid request. Invalid Slide ID.
我输入了错误句柄来尝试跳过这个,因为如果出现错误,条目留空对我来说并不重要。但是,我无法让它传递这个错误! (接下来我不能使用resume)。任何帮助都会很棒。
'loop through table and store values to a temporary array in order to sort it
'NOTE that the Table starts at (1,1) and the storage array starts at (0,0) (see "j")
For j = 1 To oTbl.Rows.Count
'First check that the string being stored is a hyperlink and not an empty cell
If Not (oTbl.Cell(j, 2).Shape.TextFrame.TextRange.ActionSettings(ppMouseClick).Hyperlink.SubAddress) = "" Then
'create a temp variable to operate on the hyperlink address in each row in the table
subAd = oTbl.Cell(j, 2).Shape.TextFrame.TextRange.ActionSettings(ppMouseClick).Hyperlink.SubAddress
'Find the CURRENT slideIndex and store in column 1 (for sorting)
'write error condition for if the slideID points to a slide that has been deleted
On Error GoTo errCatch
pLinkNumber = Left(subAd, InStr(subAd, ",") - 1)
aStorage(j - 1, 1) = ActivePresentation.slideS.FindBySlideID(CLng(pLinkNumber)).SlideIndex
'store the CURRENT hyperlink address as column 0 (after defining colum 1 for error handling reasons)
aStorage(j - 1, 0) = subAd
Else
errCatch:
End If
Next j
您的错误路径与您的 "happy path".
交织在一起
好的错误处理应该在它自己的执行路径中。
我会将 errCatch
标签重命名为例如Skip
,然后在程序底部添加:
Exit Sub
ErrCatch:
Err.Clear
Resume Skip
现在 "happy path" 在 Exit Sub
结束,"error path" 跳转到这个清除错误并恢复到 Skip
标签的小子程序。 "resume" 部分告诉 VBA 运行时 "hey, we're no longer in an error state" - 当前您的代码进入错误状态,然后没有任何信息告诉 VBA 您已完成处理该错误,所以 errCatch
跳转到,然后 "happy path" 被 VBA 理解为 error-handling 子例程:它期望您处理错误并最终 Resume
到 "happy path",但实际情况是错误基本上没有得到处理,并且错误状态一直存在到下一个循环迭代中。
那是说 On Error GoTo
语句可能应该被拉出循环范围,假设 oTbl.Rows.Count
没有理由失败(或者失败了吗?)。 运行 每次迭代都是多余的。
I cannot use resume next
为什么不呢?在其自身有限的范围内,它是一个很好的工具!
Private Function GetSlideByID(ByVal id As Long) As Slide
On Error Resume Next
Set GetSlideByID = ActivePresentation.slideS.FindBySlideID(id)
End Function
现在您可以这样做了:
Dim thisSlide As Slide
Set thisSlide = GetSlideByID(CLng(pLinkNumber))
If Not thisSlide Is Nothing Then
aStorage(j - 1, 1) = thisSlide.SlideIndex
aStorage(j - 1, 0) = subAd
End If
并且不再需要 On Error
语句 或 行标签!
在处理我的代码中的异常时遇到问题。我已经编写了下面的代码来将 powerpoint table 的内容传输到一个数组中,但它一直抛出异常说
Slides (unknown member): Invalid request. Invalid Slide ID.
我输入了错误句柄来尝试跳过这个,因为如果出现错误,条目留空对我来说并不重要。但是,我无法让它传递这个错误! (接下来我不能使用resume)。任何帮助都会很棒。
'loop through table and store values to a temporary array in order to sort it
'NOTE that the Table starts at (1,1) and the storage array starts at (0,0) (see "j")
For j = 1 To oTbl.Rows.Count
'First check that the string being stored is a hyperlink and not an empty cell
If Not (oTbl.Cell(j, 2).Shape.TextFrame.TextRange.ActionSettings(ppMouseClick).Hyperlink.SubAddress) = "" Then
'create a temp variable to operate on the hyperlink address in each row in the table
subAd = oTbl.Cell(j, 2).Shape.TextFrame.TextRange.ActionSettings(ppMouseClick).Hyperlink.SubAddress
'Find the CURRENT slideIndex and store in column 1 (for sorting)
'write error condition for if the slideID points to a slide that has been deleted
On Error GoTo errCatch
pLinkNumber = Left(subAd, InStr(subAd, ",") - 1)
aStorage(j - 1, 1) = ActivePresentation.slideS.FindBySlideID(CLng(pLinkNumber)).SlideIndex
'store the CURRENT hyperlink address as column 0 (after defining colum 1 for error handling reasons)
aStorage(j - 1, 0) = subAd
Else
errCatch:
End If
Next j
您的错误路径与您的 "happy path".
交织在一起好的错误处理应该在它自己的执行路径中。
我会将 errCatch
标签重命名为例如Skip
,然后在程序底部添加:
Exit Sub
ErrCatch:
Err.Clear
Resume Skip
现在 "happy path" 在 Exit Sub
结束,"error path" 跳转到这个清除错误并恢复到 Skip
标签的小子程序。 "resume" 部分告诉 VBA 运行时 "hey, we're no longer in an error state" - 当前您的代码进入错误状态,然后没有任何信息告诉 VBA 您已完成处理该错误,所以 errCatch
跳转到,然后 "happy path" 被 VBA 理解为 error-handling 子例程:它期望您处理错误并最终 Resume
到 "happy path",但实际情况是错误基本上没有得到处理,并且错误状态一直存在到下一个循环迭代中。
那是说 On Error GoTo
语句可能应该被拉出循环范围,假设 oTbl.Rows.Count
没有理由失败(或者失败了吗?)。 运行 每次迭代都是多余的。
I cannot use resume next
为什么不呢?在其自身有限的范围内,它是一个很好的工具!
Private Function GetSlideByID(ByVal id As Long) As Slide
On Error Resume Next
Set GetSlideByID = ActivePresentation.slideS.FindBySlideID(id)
End Function
现在您可以这样做了:
Dim thisSlide As Slide
Set thisSlide = GetSlideByID(CLng(pLinkNumber))
If Not thisSlide Is Nothing Then
aStorage(j - 1, 1) = thisSlide.SlideIndex
aStorage(j - 1, 0) = subAd
End If
并且不再需要 On Error
语句 或 行标签!