“自动化错误:被调用的对象与其客户端断开连接”

通过阅读相同标题的许多问题后,我仍然努力防止在我的代码运行时出现标题错误。

代码从这里开始:

Option Explicit Sub Toggle_Click() Dim lngMoveBy As Long Dim Loop1 As Long Dim intShapeNumber As Integer intShapeNumber = Right(Application.Caller, Len(Application.Caller) - Len("Toggle")) If ThisWorkbook.Sheets("Correction Type Options").Shapes("ToggleBackground" & intShapeNumber).Fill.ForeColor.RGB = RGB(255, 255, 255) Then Toggle_ErrorPrevention intShapeNumber 

然后移到同一个模块中的这个Sub:

 Sub Toggle_ErrorPrevention(ByVal intShapeNumberVal As Integer) Dim lngHLSegmentNumberingRow As Long Dim lngClaimRemovalHaveWantedClaimsRow As Long Dim lngClaimRemovalHaveUnwantedClaimsRow As Long With ThisWorkbook.Sheets("Correction Type Options").Columns(1) lngHLSegmentNumberingRow = .Find(What:="HL Segment Numbering", Lookat:=xlWhole).Row 

在上述最后一行失败之前, lngHLSegmentNumberingRow = .Find(What:="HL Segment Numbering", Lookat:=xlWhole).Row ,带有“自动化错误:被调用的对象已经与客户端断开连接”。

如果在发生错误后保存并closures文档并重新启动它,“校正types选项”页上的第1列将突出显示。

这是我第一次从其他潜艇上调用潜艇,所以我一直在把这个潜艇的通话本身作为潜在的问题来源。 我仍然不确定这是否正确。


这里是完整的模块,以防万一:

 Option Explicit Sub Toggle_Click() Dim lngMoveBy As Long Dim Loop1 As Long Dim intShapeNumber As Integer intShapeNumber = Right(Application.Caller, Len(Application.Caller) - Len("Toggle")) If ThisWorkbook.Sheets("Correction Type Options").Shapes("ToggleBackground" & intShapeNumber).Fill.ForeColor.RGB = RGB(255, 255, 255) Then Toggle_ErrorPrevention intShapeNumber If ThisWorkbook.Sheets("Correction Type Options").Shapes("ToggleBackground" & intShapeNumber).Fill.ForeColor.RGB = RGB(255, 255, 255) Then lngMoveBy = 0.6 Else lngMoveBy = -0.6 End If With ThisWorkbook.Sheets("Correction Type Options").Shapes("Toggle" & intShapeNumber) For Loop1 = 1 To 24 .IncrementLeft lngMoveBy DoEvents Next Loop1 End With If ThisWorkbook.Sheets("Correction Type Options").Shapes("ToggleBackground" & intShapeNumber).Fill.ForeColor.RGB = RGB(255, 255, 255) Then With ThisWorkbook.Sheets("Correction Type Options").Shapes("ToggleBackground" & intShapeNumber) .Fill.ForeColor.RGB = RGB(0, 255, 0) .TextFrame.Characters.Text = "On" .TextFrame.Characters.Font.Bold = True .TextFrame.Characters.Font.ColorIndex = 1 .TextFrame.HorizontalAlignment = xlLeft .TextFrame.VerticalAlignment = xlCenter End With Else With ThisWorkbook.Sheets("Correction Type Options").Shapes("ToggleBackground" & intShapeNumber) .Fill.ForeColor.RGB = RGB(255, 255, 255) .TextFrame.Characters.Text = "Off" .TextFrame.Characters.Font.Bold = True .TextFrame.Characters.Font.ColorIndex = 1 .TextFrame.HorizontalAlignment = xlRight .TextFrame.VerticalAlignment = xlCenter End With End If End Sub Sub Toggle_ErrorPrevention(ByVal intShapeNumberVal As Integer) Dim lngHLSegmentNumberingRow As Long Dim lngClaimRemovalHaveWantedClaimsRow As Long Dim lngClaimRemovalHaveUnwantedClaimsRow As Long With ThisWorkbook.Sheets("Correction Type Options").Columns(1) lngHLSegmentNumberingRow = .Find(What:="HL Segment Numbering", Lookat:=xlWhole).Row lngClaimRemovalHaveWantedClaimsRow = .Find(What:="Claim Removal - Have Wanted Claims", Lookat:=xlWhole).Row lngClaimRemovalHaveUnwantedClaimsRow = .Find(What:="Claim Removal - Have Unwanted Claims", Lookat:=xlWhole).Row End With With ThisWorkbook.Sheets("Correction Type Options") If intShapeNumberVal + 1 = lngHLSegmentNumberingRow Then If .Shapes("ToggleBackground" & lngClaimRemovalHaveWantedClaimsRow - 1).Fill.ForeColor.RGB = RGB(0, 255, 0) Then Application.Run .Shapes("Toggle" & lngClaimRemovalHaveWantedClaimsRow - 1).OnAction If .Shapes("ToggleBackground" & lngClaimRemovalHaveUnwantedClaimsRow - 1).Fill.ForeColor.RGB = RGB(0, 255, 0) Then Application.Run .Shapes("Toggle" & lngClaimRemovalHaveUnwantedClaimsRow - 1).OnAction End If If intShapeNumberVal + 1 = lngClaimRemovalHaveWantedClaimsRow Then If .Shapes("ToggleBackground" & lngHLSegmentNumberingRow - 1).Fill.ForeColor.RGB = RGB(0, 255, 0) Then Application.Run .Shapes("Toggle" & lngHLSegmentNumberingRow - 1).OnAction End If If intShapeNumberVal + 1 = lngClaimRemovalHaveUnwantedClaimsRow Then If .Shapes("ToggleBackground" & lngHLSegmentNumberingRow - 1).Fill.ForeColor.RGB = RGB(0, 255, 0) Then Application.Run .Shapes("Toggle" & lngHLSegmentNumberingRow - 1).OnAction End If End With End Sub 

免责声明:我知道这是一个远射

这个错误似乎在不寻常的时间出现,一个(半)重复的解决scheme是以最好的方式处理对象。 所以试试这个:

 Sub Toggle_ErrorPrevention(ByVal intShapeNumberVal As Integer) Dim findRange As Range Dim lngHLSegmentNumberingRow As Long 'more declarations Set findRange = ThisWorkbook.Sheets("Correction Type Options").Columns(1).Find(What:="HL Segment Numbering", Lookat:=xlWhole) If Not rng Is Nothing Then lngHLSegmentNumberingRow = findRange.Row End If 'other code... End Sub 

如果.Find没有返回任何东西,那么你会得到一个错误 ,因为你不能得到NothingRow属性。