无法从 SAP 系统下载 SAP 提取 - 错误“无效的 GUI 输入数据:FOCUS DATA”

问题描述 投票:0回答:3

需要有关宏的帮助,该宏尝试从 SAP 系统下载摘录,但恰好停在需要下载的位置。但手动完成时不会出现同样的问题。

以下是我们在SAP系统中遵循的步骤:

  • 第一步,输入 TCode:ZFIS 并执行以下选择(财务新 GL >> 行项目报告 >> Cognos 下载)
  • 输入所需的详细信息并执行
  • 结果需要以txt格式保存在文件夹路径中
  • 到达黄线代码时出现问题
  • 手动保存不会造成任何麻烦,但是当尝试使用编码运行它时,会出现以下错误。不知道为什么...

我们尝试了所有的可能性(即与我们的IT部门核实并尝试安装新版本的SAP系统)但我们仍然无法找到解决方案。

最后,我来这里是为了看看是否能找到同样的解决方案。

附上VBA代码供大家参考:

Sub CognosUpload()

Dim SAPApplication
Dim SAPConnection
Dim SAPSession
Dim SAPGuiAuto
Dim StoringPath As Variant
Dim fso As Scripting.FileSystemObject
Dim ts As Scripting.TextStream

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False

CurYear = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "YYYY")
Period = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "MM")

MsgBox ("Please select a folder to save all the SAP Extracts.")

Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        StoringPath = .SelectedItems(1) & "\"
    End With
    'In Case of Cancel
NextCode:
    StoringPath = StoringPath
    If StoringPath = "" Then Exit Sub

LastSelectedRow = Cells(Rows.Count, 3).End(xlUp).Row


For i = 3 To LastSelectedRow

If LastSelectedRow = 1 Then

SelectedCode = Cells(3, 3).Value

Else

SelectedCode = Cells(i, 3).Value

End If

With Range("PLANTCODES")
    Set Fn = .Cells.Find(What:=SelectedCode, LookIn:=xlValues)
    j = Fn.Address
End With

CodeforFilename = Range(j).Offset(0, 1).Value
   
'ChooseFilename = InputBox("Enter the desired name for the file")

If Not IsObject(SAPApplication) Then
   Set SAPGuiAuto = GetObject("SAPGUI")
   Set SAPApplication = SAPGuiAuto.GetScriptingEngine
End If
If Not IsObject(SAPConnection) Then
   Set SAPConnection = SAPApplication.Children(0)
End If
If Not IsObject(SAPSession) Then
   Set SAPSession = SAPConnection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject SAPSession, "on"
   WScript.ConnectObject SAPApplication, "on"
End If


    SAPSession.findById("wnd[0]").maximize
    SAPSession.findById("wnd[0]/tbar[0]/okcd").Text = "/nZFIS"
    SAPSession.findById("wnd[0]").sendVKey 0
    SAPSession.findById("wnd[0]/usr/lbl[5,3]").SetFocus
    SAPSession.findById("wnd[0]/usr/lbl[5,3]").caretPosition = 0
    SAPSession.findById("wnd[0]").sendVKey 2
    SAPSession.findById("wnd[0]/usr/lbl[9,11]").SetFocus
    SAPSession.findById("wnd[0]/usr/lbl[9,11]").caretPosition = 0
    SAPSession.findById("wnd[0]").sendVKey 2
    SAPSession.findById("wnd[0]/usr/lbl[16,14]").SetFocus
    SAPSession.findById("wnd[0]/usr/lbl[16,14]").caretPosition = 4
    SAPSession.findById("wnd[0]").sendVKey 2
    SAPSession.findById("wnd[0]/tbar[1]/btn[17]").press
    SAPSession.findById("wnd[1]/usr/txtV-LOW").Text = "GSA"
    SAPSession.findById("wnd[1]/usr/txtENAME-LOW").Text = ""
    SAPSession.findById("wnd[1]/usr/txtV-LOW").caretPosition = 7
    SAPSession.findById("wnd[1]/tbar[0]/btn[8]").press
    SAPSession.findById("wnd[0]/usr/txtP_YEAR").Text = CurYear
    SAPSession.findById("wnd[0]/usr/txtP_PERIO").Text = Period
    SAPSession.findById("wnd[0]/usr/txtP_PERIO").SetFocus
    SAPSession.findById("wnd[0]/usr/txtP_PERIO").caretPosition = 2
    SAPSession.findById("wnd[0]/usr/btn%_S_BUKRS_%_APP_%-VALU_PUSH").press
    SAPSession.findById("wnd[1]/tbar[0]/btn[16]").press

    
    ThisWorkbook.Sheets(1).Select
    Cells(i, 3).Copy
    
      
    SAPSession.findById("wnd[1]/tbar[0]/btn[24]").press
    SAPSession.findById("wnd[1]/tbar[0]/btn[8]").press
    SAPSession.findById("wnd[0]/tbar[1]/btn[8]").press
    SAPSession.findById("wnd[0]/tbar[1]/btn[45]").press
    SAPSession.findById("wnd[1]/tbar[0]/btn[0]").press
    SAPSession.findById("wnd[1]/usr/ctxtDY_PATH").Text = StoringPath
    SAPSession.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = SelectedCode & ".txt"
    SAPSession.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 9
    **SAPSession.findById("wnd[1]/tbar[0]/btn[0]").press**
    
Sheets(2).Select

InputFolder = (StoringPath & SelectedCode & ".txt")

Set fso = New Scripting.FileSystemObject

Set ts = fso.OpenTextFile(InputFolder)

'Sheets(1).Activate
Range("A:I").Clear
Range("A1").Select

Call ClearTextToColumns

    Do Until ts.AtEndOfStream
        ActiveCell.Value = ts.ReadLine
        ActiveCell.Offset(1, 0).Select
    Loop
    
    Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="|", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
        TrailingMinusNumbers:=True
    'Range("A:A").Delete shift:=xlToLeft

ts.Close

Set fso = Nothing

Rows("1:1").Delete Shift:=xlUp
Rows("2:2").Delete Shift:=xlUp
Rows("1:1").Font.Bold = True
Columns("A:A").Delete Shift:=xlLeft
Columns("A:G").EntireColumn.AutoFit
If Range("A3").Value = "" Then GoTo Listmsg
MyFileName = "Congnos Download for " & CodeforFilename

sFname = StoringPath & MyFileName & ".csv"
lFnum = FreeFile
'ActiveSheet.UsedRange.Rows
Open sFname For Output As lFnum
'Loop through the rows'
    For Each rRow In Sheets("CSV Extract").UsedRange.Rows
    'Loop through the cells in the rows'
    For Each rCell In rRow.Cells
        If rCell.Column = 5 Or rCell.Column = 6 Then
            If rCell.Row = 1 Or rCell.Row = 2 Then
              sOutput = sOutput & rCell.Value & ";"
            Else
                sOutput = sOutput & Trim(Round(rCell.Value)) & ";"
            End If
        Else
            sOutput = sOutput & rCell.Value & ";"
        End If
    Next rCell
     'remove the last comma'
    sOutput = Left(sOutput, Len(sOutput) - 1)
    
    'write to the file and reinitialize the variables'
    Print #lFnum, sOutput
    sOutput = ""
 Next rRow

'Close the file'
Close lFnum

Sheets(1).Select

Listmsg:

Sheets(1).Select

Next i

Sheets(1).Select
Range("B3").Select

MsgBox "CSV file has been created for you, now you can upload the file in Cognos."


ResetSettings:


Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False

End Sub

Sub ClearTextToColumns()
    On Error Resume Next
    If IsEmpty(Range("A1")) Then Range("A1") = "XYZZY"
    Range("A1").TextToColumns Destination:=Range("A1"), _
        DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=False, _
        Tab:=False, _
        Semicolon:=False, _
        Comma:=False, _
        Space:=False, _
        Other:=False, _
        OtherChar:=""
    If Range("A1") = "XYZZY" Then Range("A1") = ""
    If Err.Number <> 0 Then MsgBox Err.Description
End Sub
excel vba user-interface sap-gui
3个回答
0
投票

问题中不太清楚错误发生在哪里,但你说错误发生在下面最后一行?请注意, 温德[1]... 意味着应该打开第二个窗口或对话框。我预计它没有正确打开,或者可能由于此步骤上面的行而发生了一些其他意外事件或错误。如果 ZFIS 屏幕/交易已更改,可能会发生这种情况吗?

SAPSession.findById("wnd[1]/tbar[0]/btn[24]").press
SAPSession.findById("wnd[1]/tbar[0]/btn[8]").press
SAPSession.findById("wnd[0]/tbar[1]/btn[8]").press
SAPSession.findById("wnd[0]/tbar[1]/btn[45]").press
SAPSession.findById("wnd[1]/tbar[0]/btn[0]").press

最好的方法是在代码中较早地设置一个 VBA 断点 (F9),然后使用 (F8) 单步执行它,并在每一步中查看 SAP GUI 屏幕,看看脚本哪里出了问题。


0
投票

显然这些年来这个错误仍然存在...我花了几个小时试图修复它,发现如果我在文件名字段上

SetFocus
,错误就不会发生。

尝试在按下按钮保存文件之前添加此命令行:

Session.findById("wnd[1]/usr/ctxtDY_PATH").SetFocus

确切的命令可能会根据用户/交易的不同而有所不同,但这个命令对我和其他人都有效。


0
投票

对于某些用户来说,我遇到了同样的问题,对我有用的解决方案是编写代码来检查我的案例中的文件 EXPORT.XLSX 是否已经在我想要保存的文件夹中(C:\Temp),如果是,则删除然后在运行 SAP 步骤之前调用此代码,并且从 SAP 获取文件时使用“创建”而不是“替换”

© www.soinside.com 2019 - 2024. All rights reserved.