这是我的代码为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'''
发布于 2020-09-23 20:02:51
一切都在发展。还有到目前为止已知的可能性。最近,您还可以尝试以下操作。例如:
...
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, 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
发布于 2020-09-25 17:21:57
任何人都知道如何处理此-How以停止弹出窗口;在导出excel时使用VBA脚本的SAP安全性
https://stackoverflow.com/questions/64009373
复制相似问题