簡體   English   中英

從 SAP 下載的 Excel 文件未在我的 VBA 代碼中得到識別

[英]Excel file downloaded from SAP is not getting recognized in my VBA code

我有一個宏代碼,它從 SAP 下載一個 excel 文件,發布它應該對其進行一些數據處理以到達我的最終 output 文件。 代碼下載並打開 excel 文件。 但每次 object 超出范圍時它仍然顯示錯誤。 一旦我單擊 excel 工作表中的某處並繼續運行我的代碼,之后它就可以完美運行。 如何避免這種人工干預。 好心提醒。

If Not IsObject(App) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = App.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 App, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "stark"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "*****"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").SetFocus
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 12
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/okcd").Text = "ABC01"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/radMADE").SetFocus
session.findById("wnd[0]/usr/radMADE").Select
session.findById("wnd[0]/mbar/menu[0]/menu[0]").Select
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\Stark\Desktop\SAP Scripting"
session.findById("wnd[1]/usr/ctxtDY_PATH").SetFocus
session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 44
session.findById("wnd[1]/tbar[0]/btn[0]").press

Windows("EXPORT.xlsx").Activate
Range("AA1").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("AA1").Select
    Selection.Copy
    Range("B2:D2").Select
    Range(Selection, Selection.End(xlDown)).Select

該文件始終下載為 EXPORT.xlsx。 我在Windows("EXPORT.xlsx").Activate一旦我手動輸入工作表並繼續運行代碼,它就會工作。

好心提醒。

我會嘗試

applicatin.workbooks("Export.xlsx").activate

如果仍然存在錯誤,請運行此代碼而不是激活行

Dim i As Long
For i = 1 To Application.Workbooks.Count
    Debug.Print Application.Workbooks(i).Name
Next

這樣,您就可以獲得需要在激活代碼行中使用的工作簿的名稱。

請嘗試下一種方法等待導出的文件在 Excel 中加載。當 VBA 打開工作簿時,它會等待它打開,但如果不是直接打開代碼,請在加載之前嘗試找到導出的工作簿/打開:

Sub testCheckOpenSAPExportedWb()
 'your existing code
 '...
 Session.findById("wnd[1]/tbar[0]/btn[0]").press
  
  Dim wb As Workbook, wbSAP As Workbook, lastRow As Long, count As Long
  
  'loop until the exported workbook is found. If more then 10 iteration sets passed, the loop is exited
  Do While wbSAP Is Nothing
    For Each wb In Workbooks
        count = count + 1
        If wb.Name = "EXPORT.xlsx" Then
            Set wbSAP = wb: Exit Do
        End If
        Application.Wait Now + TimeValue("0:00:01")
        If coumn >= 10 Then MsgBox "The workbook coould not be found in 10 seconds...": Exit Do
    Next
  Loop
  If wbSAP Is Nothing Then Exit Sub
  
    MsgBox "The exported workbook has been found..."
    wbSAP.Range("AA1").FormulaR1C1 = "1" 'I do not understand what you want doing here...
    lastRow = wbSAP.Range("B" & rows.count).End(xlUp).row
    wbSAP.Range("B2:D" & lastRow).Copy 'then you can paste it wherever you need...
    'do here whatever you need with the copied range...
    '...
End Sub

請測試它並發送一些反饋。 我還想知道如果您手動嘗試導出這樣的工作簿需要多長時間才能打開。 那么,是不是只能把文件下載到自己電腦的某個地方,讓VBA打開呢?

我有同樣的問題,並嘗試使用 Sleep API function 循環。它總是超時,因為打開工作簿一直保持到代碼執行完成。 如果您在調試模式下逐行運行它,它會起作用,因為您可以在執行下一行代碼之前留出時間讓工作簿打開。 但是,如果讓宏繼續運行,Excel 會等待宏中的所有代碼執行完畢,然后打開文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM