用VBA关闭SAP导出多个Excel文件

问题描述

这是我的SAP GUI脚本代码,其中单击按钮时,我会将数据导出到文件我有多个行,将生成100的文件。我必须手动关闭所有文件

任何人都可以帮助您关闭所有新打开的excel文件。 我应该在其中添加什么代码

Dim ws As Worksheet
Dim lrow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim lastrow As Long
lastrow = ws.UsedRange.Rows.Count

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 Application,"on"
End If

lrow = ws.Cells(Rows.Count,"A").End(xlUp).Row

For i = 2 To lrow
If ws.Cells(i,"G") = True Then

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nfbl3n"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/radX_AISEL").Select
session.findById("wnd[0]/usr/ctxtSD_SAKNR-LOW").Text = ws.Range("A" & i)
session.findById("wnd[0]/usr/ctxtSD_SAKNR-HIGH").Text = ws.Range("B" & i)
session.findById("wnd[0]/usr/ctxtSD_BUKRS-LOW").Text = ws.Range("C" & i)
session.findById("wnd[0]/usr/ctxtSD_BUKRS-HIGH").Text = ws.Range("D" & i)
session.findById("wnd[0]/usr/ctxtSO_BUDAT-LOW").Text = ws.Range("E" & i)
session.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").Text = ws.Range("F" & i)
session.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").SetFocus
session.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = Cells(1,"K").Value
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = Cells(i,"H").Value
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 10
session.findById("wnd[1]/tbar[0]/btn[11]").press
End If
Next i
End Sub'''

解决方法

一切都在发展。也是迄今为止已知的可能性。最近,您还可以尝试以下方法。 例如:

...
session.findById("wnd[1]/tbar[0]/btn[0]").press
'-------------new-------------------------------------------------------
'Is it really a number = 1 or a parameter = i?
myPath = Cells(1,"K").Value
myFileName = Cells(i,"H").Value
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = myPath
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = myFileName
'not necessary and with a file name shorter than 10 it would even be wrong
'session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 10
session.findById("wnd[1]/tbar[0]/btn[11]").press

myCount = Workbooks.Count
Do
 DoEvents
 Application.Wait Now + TimeSerial(0,5)
 DoEvents
 If Workbooks.Count > myCount Then Exit Do
Loop

Dim xlApp As Object

Set xlApp = GetObject(myPath & "\" & myFile).Application

For j = 1 To Workbooks.Count
 If LCase(Workbooks(j).Name) = LCase(myFile) Then Exit For
Next j

xlApp.Workbooks(j).Close SaveChanges:=False
Set xlApp = Nothing
'--------------new-------------------------------------------------------

Next i
...

关于ScriptMan,

,

任何人都知道如何处理-如何停止弹出窗口;导出excel时使用VBA脚本的SAP GUI安全性

enter image description here