簡體   English   中英

SAP GUI 導出自動打開 excel

[英]SAP GUI export automatically opens excel

我正在嘗試編寫一個從 SAP GUI 中的 ALV 網格檢索數據的宏。 一切正常,直到數據導出。 將數據從 ALV 網格導出到 .xlsx 文件時,該文件將在導出后自動打開。

我需要我的腳本等待導出打開,然后將數據從新打開的導出文件復制到腳本來自的 .xlsm 文件。

如果我嘗試在 SAP GUI 中導出文件的命令之后立即激活 export.XLSX 文件,我會收到“下標超出范圍”錯誤。 我想也許我可以循環激活命令,直到它停止出錯(當 export.xlsx 文件打開時),但這會導致 excel 崩潰。 我該怎么辦?

Function funcLSAT(strEnv)

Dim wkbExport As Workbook
Dim strError As String

If Not IsObject(SapGuiApp) Then
    Set SapGuiApp = CreateObject("Sapgui.ScriptingCtrl.1")
End If
If Not IsObject(Connection) Then
    Set Connection = SapGuiApp.OpenConnection(strEnv, True)
End If
    
Set session = Connection.Children(0)
session.findById("wnd[0]/tbar[0]/okcd").Text = "[TCODE]"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/cntlG_CC_MCOUNTY/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
session.findById("wnd[0]/usr/cntlG_CC_MCOUNTY/shellcont/shell").selectContextMenuItem "&XXL"
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "[filepath]"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "export.xlsx"
session.findById("wnd[1]/tbar[0]/btn[11]").press
Set session = Nothing
Set Connection = Nothing
Set SapGuiApp = Nothing

Do
On Error Resume Next
Windows("export.XLSX").Activate
Loop Until (Err.Number = 0)
On Error GoTo 0

Range("A2:AS2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

End Function

前一段時間我遇到了同樣的問題,當時我的宏必須等待創建 .txt 然后繼續,所以我發現了這個:

Dim Directory As String, File As String
Directory = ActiveWorkbook.Path & "\" 'path for the file
File = Directory & "datos.txt" 'name of the file along with the path


  FindIt = Dir(File)
  While Len(FindIt) = 0
  FindIt = Dir(File)
  Wend

希望能幫助到你。

這是我將導出為 xlsx、復制到工作表並關閉的操作。 Application.wait 不會執行您希望它在此處執行的操作。 Application.wait 不會發布 excel,因此 sap 文件永遠不會打開。 設置計時器將釋放 excel 以便加載文件。

它可能不是寫得最好的,但它有效。 首先做一個函數來查看文件是否打開。 然后設置一個 0.5 秒左右的計時器以進行循環。 你可以做一個更長的計時器,可能不需要循環,但是用很短的時間完成可以防止 excel 發布的時間超過它必須的時間。 一旦文件鎖定到,它將復制內容並關閉文件,並退出循環。 然后我也殺死了該文件,因為在下次運行時它需要在第一次運行時消失,isfile open 將拿起舊文件。 如果它在另一個實例中打開,它會被寫入應該抓取工作簿的位置,但是,我還沒有嘗試過。 我的 vba 工作簿加載方式,SAP 文件最終加載到同一個實例中。 祝你好運!

函數 IsFileOpen(FileName As String) Dim iFilenum As Long Dim iErr As Long

On Error Resume Next
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0
 
Select Case iErr
Case 0:    IsFileOpen = False
Case 70:   IsFileOpen = True
Case Else: Error iErr
End Select
 

結束函數

Dim xlfile as string, xldir as string
Dim PauseTime, Start
Dim control As Long
Dim xlapp As Object
xldir= "Your file folder path here"   
control = 0
xlfile = "Your filename here"
Do Until control = 5
PauseTime = 0.5 ' Set duration.
Start = Timer    ' Set start time.
Do While Timer < Start + PauseTime
    DoEvents    ' Yield to other processes.
 Loop
  If control = 1 And IsFileOpen(xldir & xlfile) = False Then
  Exit Do
  End If
 If IsFileOpen(xldir & xlfile) = True Then
  Set xlapp = GetObject(xldir & xlfile).Application
  If control = 0 Then
  Workbooks(xlfile).Sheets(1).Name = Left(xlfile, Len(xlfile) - 5)
  Workbooks(xlfile).Sheets(Left(xlfile, Len(xlfile) - 5)).Copy    
  before:=ThisWorkbook.Sheets(1)
  ThisWorkbook.Activate
  control = 1
   End If
  For Each wb In Application.Workbooks
 If wb.Name = xlfile Then
  wb.Close
    End If
 Next wb
 End If
  Loop
 control = 0
 kill(xldir&xlfile)

暫無
暫無

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

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