无法绕过错误619 [从VBA执行SAP]

时间:2018-10-26 17:08:01

标签: sap

我有以下代码,即使我进入Application.DisplayAlerts = False,它似乎总是会产生错误619,并且我已采取额外的预防措施来绕过该错误。我将导致错误的行加粗了。我曾尝试使用On Error GoTo,On Error Resume Next和IsError(),但似乎都无法正常工作。

VIM:

'Insert Sum of Amts and list as Balance on Summary tab
Sheets(2).Select
    LastRow = ActiveSheet.UsedRange.Rows.Count
    Range("D" & LastRow + 1).Select
    ActiveCell.FormulaR1C1 = "=SUM(R2C4:R" & LastRow & "C4)"
    Columns("D:D").Select
    Selection.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
Sheets(1).Select
    Range("C7").Select
    ActiveCell.FormulaR1C1 = "=Statement!R" & LastRow + 1 & "C4"

'Insert V-lookup for open items
Sheets(2).Select
    Range("G2").Select
   ActiveCell.FormulaR1C1 = _
        "=IF(OR(RC[-1]=""R"",RC[-1]=""A""),""-"",IF(IFERROR(VLOOKUP(RC[-5],'FBL1N Scheduled'!C1:C2,2,0), ""-"")>TODAY(),IFERROR(VLOOKUP(RC[-5],'FBL1N Scheduled'!C1:C2,2,0), ""-""),IF(IFERROR(VLOOKUP(RC[-5],'FBL1N Scheduled'!C1:C2,2,0), ""-"")<=DATE(YEAR(TODAY()),MONTH(TODAY()),15),IF(TODAY()<=DATE(YEAR(TODAY()),MONTH(TODAY()),15),DATE(YEAR(TODAY()),MONTH(TODAY()),15),EOMONTH(TODAY(),0)))))"
    Range("G2").Select
    Selection.AutoFill Destination:=Range("G2:G" & LastRow), Type:=xlFillDefault
    Range("G2:G" & LastRow).Select
    Selection.NumberFormat = "mm/dd/yyyy;@"

'Insert V-lookup for cleared items
    Range("H2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[-1]<>""-"",""-"",IFERROR(VLOOKUP(RC[-6],'FBL1N Paid'!C1:C2,2,0),""-""))"
    Range("H2").Select
    Selection.AutoFill Destination:=Range("H2:H" & LastRow), Type:=xlFillDefault
    Range("H2:H" & LastRow).Select
    Selection.NumberFormat = "mm/dd/yyyy;@"

            'Autofit and filter
            Cells.Select
            Cells.EntireColumn.AutoFit
            Application.CutCopyMode = False
            Selection.AutoFilter

    'Copy values for Vim
    LastRow = ActiveSheet.UsedRange.Rows.Count
    ActiveSheet.Range("$A$1:$K$" & LastRow).AutoFilter Field:=7, Criteria1:="-"
    ActiveSheet.Range("$A$1:$K$" & LastRow).AutoFilter Field:=8, Criteria1:="-"
        'Insert DPN for processing/obsolete invoices
        With Worksheets("Statement").AutoFilter.Range
          Range("E" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row).Select
        End With
        ActiveCell.FormulaR1C1 = _
        "=IFERROR(VLOOKUP(RC[-3],'VIM Export'!C1:C5,3,0),"""")"
        'Copy down in only visible
        With ActiveSheet.UsedRange
            .Resize(.Rows.Count - 1).Offset(1).Columns("E"). _
               SpecialCells(xlCellTypeVisible).FillDown
        End With

        'Select Reference #
        With Worksheets("Statement").AutoFilter.Range
          Range("B" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row).Select
        End With

        'If none are left processing, skip VIM
        If ActiveCell.Value = "" Then GoTo Valuations:

    'Copy all invoice numbers not posted
    Range(ActiveCell, ActiveCell.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy

'Switch to Summary page to pull vendor data in VIM search
Sheets(1).Select

'Execute in SAP
If Not IsObject(SAPApp) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set SAPApp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = SAPApp.Children(0)
End If
If Not IsObject(session) Then
   Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject SAPApp, "on"
End If

'Open Vim and apply parameters (inserting Doc Numbers)
session.findById("wnd[0]/tbar[0]/btn[12]").press
session.findById("wnd[0]/tbar[0]/btn[12]").press
't-code entered in bar
session.findById("wnd[0]/tbar[0]/okcd").Text = "/n/opt/vim_analytics"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/btn%_S_XBLNR_%_APP_%-VALU_PUSH").press
session.findById("wnd[1]/tbar[0]/btn[24]").press
session.findById("wnd[1]").sendVKey 8
session.findById("wnd[0]/usr/btn%_S_LIFNR_%_APP_%-VALU_PUSH").press
session.findById("wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,0]").Text = Cells(3, 1).Value
session.findById("wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,1]").Text = Cells(4, 1).Value
session.findById("wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,2]").Text = Cells(5, 1).Value
session.findById("wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,3]").Text = Cells(3, 3).Value
session.findById("wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,4]").Text = Cells(4, 3).Value
session.findById("wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,5]").Text = Cells(5, 3).Value
session.findById("wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,1]").SetFocus
session.findById("wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,1]").caretPosition = 7
session.findById("wnd[1]").sendVKey 8
session.findById("wnd[0]").sendVKey 8

On Error Resume Next
**session.findById("wnd[1]/tbar[0]/btn[0]").press**
If Err.Number = 0 Then

    'Export to Excel (Existing XXL Format)
    session.findById("wnd[0]/usr/cntlCL_GRID/shellcont/shell").contextMenu
    session.findById("wnd[0]/usr/cntlCL_GRID/shellcont/shell").selectContextMenuItem "&XXL"
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[1]/tbar[0]/btn[0]").press

    'Select Export and Paste into Workbook
        Range("A1").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Selection.Cut
        Windows("Updated Auto Recon.xlsm").Activate
        Sheets(5).Select
        Range("A1").Select
        ActiveSheet.Paste
        'Clear Formatting
        ThisWorkbook.ActiveSheet.Cells.ClearFormats
        'Close temporary export sheet
        Windows("Worksheet in Basis (1)").Activate
        ActiveWindow.Close

    If Not IsObject(SAPApp) Then
       Set SapGuiAuto = GetObject("SAPGUI")
       Set SAPApp = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(Connection) Then
       Set Connection = SAPApp.Children(0)
    End If
    If Not IsObject(session) Then
       Set session = Connection.Children(0)
    End If
    If IsObject(WScript) Then
       WScript.ConnectObject session, "on"
       WScript.ConnectObject SAPApp, "on"
    End If
    session.findById("wnd[1]/tbar[0]/btn[0]").press

    'Formatting VIM Export
    ''Moves columns to the appropriate postions in the worksheet
    ''Move Reference to Column A; Move CoCode to Column B; Move Doc. ID to Column C; Move Document Status to Column D
        Range("A1").End(xlToRight).Select
        LastColumn = ActiveCell.Column

        For i = 1 To LastColumn
            Cells(1, i).Select
                If ActiveCell.Value = "CoCd" Then
                    ActiveCell.Value = "CoCode"
                ElseIf ActiveCell.Value = "Company Code" Then
                    ActiveCell.Value = "CoCode"
                ElseIf ActiveCell.Value = "DocumentStatus" Then
                    ActiveCell.Value = "Document Status"
                ElseIf ActiveCell.Value = "DOC Status" Then
                    ActiveCell.Value = "Document Status"
                ElseIf ActiveCell.Value = "DOC status" Then
                    ActiveCell.Value = "Document Status"
                ElseIf ActiveCell.Value = "Document Id" Then
                    ActiveCell.Value = "Doc. Id"
                ElseIf ActiveCell.Value = "Vendor Nam" Then
                    ActiveCell.Value = "Vendor Name"
                Else
                End If
        Next i

        For i = 1 To LastColumn
            Cells(1, i).Select
            If ActiveCell.Value = "Reference" Then
                If ActiveCell.Column <> 1 Then
                    Columns(i).Select
                    Selection.Cut
                    Columns("A").Insert Shift:=xlToRight
                Else
                End If
            Else
            End If
        Next i

        For i = 1 To LastColumn
            Cells(1, i).Select
            If ActiveCell.Value = "CoCode" Then
                If ActiveCell.Column <> 2 Then
                    Columns(i).Select
                    Selection.Cut
                    Columns("B").Insert Shift:=xlToRight
                Else
                End If
            Else
            End If
        Next i

        For i = 1 To LastColumn
            Cells(1, i).Select
            If ActiveCell.Value = "Doc. Id" Then
                If ActiveCell.Column <> 3 Then
                    Columns(i).Select
                    Selection.Cut
                    Columns("C").Insert Shift:=xlToRight
                Else
                End If
            Else
            End If
        Next i

        For i = 1 To LastColumn
            Cells(1, i).Select
            If ActiveCell.Value = "Document Status" Then
                If ActiveCell.Column <> 4 Then
                    Columns(i).Select
                    Selection.Cut
                    Columns("D").Insert Shift:=xlToRight
                Else
                End If
            Else
            End If
        Next i

        For i = 1 To LastColumn
            Cells(1, i).Select
            If ActiveCell.Value = "Vendor" Then
                If ActiveCell.Column <> 5 Then
                    Columns(i).Select
                    Selection.Cut
                    Columns("E").Insert Shift:=xlToRight
                Else
                End If
            Else
            End If
        Next i

        For i = 1 To LastColumn
            Cells(1, i).Select
            If ActiveCell.Value = "Vendor Name" Then
                If ActiveCell.Column <> 6 Then
                    Columns(i).Select
                    Selection.Cut
                    Columns("F").Insert Shift:=xlToRight
                Else
                End If
            Else
            End If
        Next i

        'Remove Confirmed Duplicate Status
        i = Application.WorksheetFunction.CountIf(Columns("D:D"), "Confirmed Duplicate")

        If i > 0 Then
            LastRow = ActiveSheet.UsedRange.Rows.Count
            Range(Cells(1, 1), Cells(LastRow, LastColumn)).Select
            Selection.AutoFilter
            ActiveSheet.Range(Cells(1, 1), Cells(LastRow, LastColumn)).AutoFilter Field:=4, Criteria1:="Confirmed Duplicate", _
                Operator:=xlAnd
                Rows("2:" & LastRow).SpecialCells(xlCellTypeVisible).Select
                Selection.Delete Shift:=xlToLeft
                ActiveSheet.ShowAllData
            Else
        End If

        'Remove Cancelled Status
        i = Application.WorksheetFunction.CountIf(Columns("D:D"), "Cancelled")

        If i > 0 Then
            LastRow = ActiveSheet.UsedRange.Rows.Count
            Range(Cells(1, 1), Cells(LastRow, LastColumn)).Select
            Selection.AutoFilter
            ActiveSheet.Range(Cells(1, 1), Cells(LastRow, LastColumn)).AutoFilter Field:=4, Criteria1:="Cancelled", _
                Operator:=xlAnd
                Rows("2:" & LastRow).SpecialCells(xlCellTypeVisible).Select
                Selection.Delete Shift:=xlToLeft
                ActiveSheet.ShowAllData
            Else
        End If

        Application.ScreenUpdating = True
        Application.ScreenUpdating = False
        'Force Doc Id to convert to number
        Range("C:C").Select
            With Selection
                Selection.NumberFormat = "General"
                .Value = .Value
            End With

        Application.ScreenUpdating = True
        Application.ScreenUpdating = False
        'Force Vendor # to convert to number
        Range("E:E").Select
            With Selection
                Selection.NumberFormat = "General"
                .Value = .Value
            End With
        'Sorts Reference number by most recent document status
        LastRow = ActiveSheet.UsedRange.Rows.Count
        Range(Cells(1, 1), Cells(LastRow, LastColumn)).Select
        Range("A2").Activate
        ActiveWorkbook.Worksheets("VIM Export").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("VIM Export").Sort.SortFields.Add Key:=Range( _
            "C2:C" & LastRow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
        ActiveWorkbook.Worksheets("VIM Export").Sort.SortFields.Add Key:=Range( _
            "A2:A" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("VIM Export").Sort
            .SetRange Range(Cells(1, 1), Cells(LastRow, LastColumn))
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With

        Range("A2").Select
Else
    Err.Clear
End If
On Error GoTo 0

'Resume
Valuations:

'Select Statement sheet
Sheets(2).Select

'Clear Filter
ActiveSheet.ShowAllData

    Application.ScreenUpdating = True
    Application.ScreenUpdating = False
   'Change Font and Size, autofit
            Cells.Select
            With Selection.Font
                .Name = "CotySans"
                .FontStyle = "Regular"
                .Size = 9
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .ColorIndex = xlAutomatic
                .TintAndShade = 0
                .ThemeFont = xlThemeFontNone
            End With
        'Format Top Row
            Rows("1:1").SpecialCells(xlCellTypeConstants).Select
                Selection.Font.Bold = True
                With Selection.Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .ThemeColor = xlThemeColorDark1
                    .TintAndShade = -0.149998474074526
                    .PatternTintAndShade = 0
                End With

'Insert Company Code from all tabs
LastRow = ActiveSheet.UsedRange.Rows.Count
    Range("A2").Select
    ActiveCell.FormulaR1C1 = _
        "=IFERROR(VLOOKUP(RC[1],'FBL1N Scheduled'!C1:C4,3,0), IFERROR(VLOOKUP(RC[1],'FBL1N Paid'!C1:C4,3,0),IFERROR(VLOOKUP(RC[1],'VIM Export'!C1:C5,2,0),"""")))"
    Selection.AutoFill Destination:=Range("A2:A" & LastRow - 1)

'Insert Vendor Number from all tabs
Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "Vendor #"
    Range("B2").Select
    ActiveCell.FormulaR1C1 = _
        "=IFERROR(VLOOKUP(RC[1],'FBL1N Scheduled'!C1:C4,4,0), IFERROR(VLOOKUP(RC[1],'FBL1N Paid'!C1:C4,4,0),IFERROR(VLOOKUP(RC[1],'VIM Export'!C1:C5,5,0),"""")))"
    Selection.AutoFill Destination:=Range("B2:B" & LastRow - 1)

'Pull Block Marker
Range("F2").Select
    ActiveCell.FormulaR1C1 = _
        "=IFERROR(VLOOKUP(RC[-3],'VIM Export'!C1:C5,3,0),"""")"
    Range("F2").Select
    Selection.AutoFill Destination:=Range("F2:F" & LastRow)
    Range("F:F").Select
        Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

'Insert VIM Status for processing/obsolete invoices
    Range("L2").Select
        ActiveCell.FormulaR1C1 = _
        "=IFERROR(VLOOKUP(RC[-9],'VIM Export'!C1:C5,4,0),"""")"
'Copy down in only visible
    Selection.Copy
    Range("L" & LastRow - 1).Select
    Range(Selection, Selection.End(xlUp)).Select
    ActiveSheet.Paste

'Insert Status based on VIM Status
    LastRow = ActiveSheet.UsedRange.Rows.Count - 1
    Range("J2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(OR(RC[-3]=""R"",RC[-3]=""A""),""Blocked"",IF(RC[-8]="""",""Need Copy"",IF(RC[2]="""","""",IF(RC[2]=""Return To Vendor"",""Return To Vendor"",IF(RC[2]=""Obsolete"",""Obsolete"",""Processing"")))))"
        Selection.AutoFill Destination:=Range("J2:J" & LastRow)

        'Paste special over formulas
        Range("A:A").Select
        Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
         Range("B:B").Select
        Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Range("F:F").Select
        Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Range("H:J").Select
        Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Application.CutCopyMode = False

    Application.ScreenUpdating = True
    Application.ScreenUpdating = False
            'AutoFit Columns
            Cells.Select
            Cells.EntireColumn.AutoFit
            Application.CutCopyMode = False
        Range("L:L").Value = ""
        LastRow = ActiveSheet.UsedRange.Rows.Count - 1
        Range("B2:C" & LastRow).Select
        With Selection
            .HorizontalAlignment = xlRight
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Range("A2").Select

    'Refresh Pivot Info for dates
    Worksheets("Pivots").PivotTables("PivotTable1").PivotCache.Refresh
    Worksheets("Pivots").PivotTables("PivotTable2").PivotCache.Refresh

    'Hide Data Tabs
    ThisWorkbook.Sheets(Array(3, 4, 5)).Visible = False

    Sheets(1).Select

  'Saves the file
    Dim VN As String
        VN = Range("A3").Value
    Dim VName As String
        VName = Range("B3").Value

            Set objWS = CreateObject("WScript.Shell")
            strDesktopPath = objWS.SpecialFolders("Desktop")
            ActiveWorkbook.SaveAs strDesktopPath & "\" & VN & "." & VName & " as of " & Format(Now, "mm.dd.yyyy") & ".xlsm"

'Update final once macro completes
Application.ScreenUpdating = True

Application.DisplayAlerts = True

End Sub

1 个答案:

答案 0 :(得分:0)

脚本可能没有等待足够长的时间才能显示按钮。 此时,可以等待一个定义的时间,也可以等待直到它实际存在。

例如:

...
session.findById("wnd[0]").sendVKey 8
on error resume next
application.wait (Now + TimeValue("00:00:01"))    '1 second
session.findById("wnd[1]/tbar[0]/btn[0]").press
If Err.Number = 0 Then
   on error goto 0
...

...
session.findById("wnd[0]").sendVKey 8
on error resume next
do
 application.wait (Now + TimeValue("00:00:01"))    '1 second
 session.findById("wnd[1]/tbar[0]/btn[0]").press
 if err.number = 0 then exit do
loop
If Err.Number = 0 Then
   on error goto 0
...

关于ScriptMan