"自动化错误:调用的对象已与其客户端断开连接。"从另一个子呼叫

时间:2017-03-15 21:54:50

标签: excel vba excel-vba

在阅读了同一个标题的许多问题之后,我仍然在努力防止标题错误在我的代码运行时发生。

代码从这里开始:

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之前,使用"自动化错误:调用的对象已与其客户端断开连接。"

如果我在错误发生后保存并关闭文档并重新启动它,那么"更正类型选项"表格突出显示。

这是我第一次从其他潜水员那里呼叫潜水艇,所以我一直特别注意呼叫本身作为问题的潜在根源。我仍然不相信它是正确的。

以下是完整的模块,如果它有帮助:

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

1 个答案:

答案 0 :(得分:2)

免责声明:我意识到这是一个长镜头......

此错误似乎在异常时间弹出,一个(半)重复解决方案是以尽可能最好的方式处理对象。所以试试这个:

.Find

如果Row未返回任何内容,则会出现错误,因为您无法获得Nothing的{​​{1}}属性。