我有一些VBA代码,可以将SAP数据导出到.XLSX文件,然后将该文件导入到excel中。导入文件后,我有执行许多其他操作的代码(例如,解析和组织数据、计算单元格等)。宏完成后,导出的SAP .XLSX文件将打开,我不希望它打开。
我尝试使用Kill函数,它成功地完成了,但在宏的末尾,它没有打开文件,而是显示了一条关于未找到文件的消息,这是有意义的。
我尝试在宏完成之前关闭该文件,但这也会导致错误,因为该文件实际上并未打开。
我尝试在宏完成之前打开文件,然后再关闭文件,它成功地完成了这一操作,但在宏结束时,文件再次打开。
但是,当我破坏代码并单步执行时,文件最后不会打开。
我尝试了其他一些不同的计时器函数,如DoEvents等,但仍然没有成功。
有没有人知道如何: 1.防止导出的文件在代码的末尾打开,或者2.防止显示“找不到文件”的消息,以便在宏中终止该文件。
示例代码
'Opens SAP connection
Dim SAPGUI
Dim Applic
Dim connection
Dim Session
Dim WSHShell
Application.DisplayAlerts = False
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus
Set WSHShell = CreateObject("WScript.Shell")
Do Until WSHShell.AppActivate("SAP Logon ")
Application.Wait Now + TimeValue("0:00:01")
Loop
Set WSHShell = Nothing
Set SAPGUI = GetObject("SAPGUI")
Set Applic = SAPGUI.GetScriptingEngine
Set connection = Applic.OpenConnection("04. R3 PRD []", True)
Set Session = connection.Children(0)
' Session.findById("wnd[0]").iconify
Session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "100"
Session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = Environ("Username")
Session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = sysstart.psswrd
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "/nZUI_SELECTION"
Session.findById("wnd[0]").sendVKey 0
''Performs some filters and opens data
' Export from SAP to .xlsx file.
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL"
Session.findById("wnd[1]/tbar[0]/btn[0]").press
Session.findById("wnd[1]/usr/ctxt[0]").Text = "C:\Users\" & Environ("Username") & "\Downloads"
Session.findById("wnd[1]/usr/ctxt[1]").Text = "SAP_export.XLSX"
Session.findById("wnd[1]/tbar[0]/btn[11]").press
'Closes SAP connection
Set Session = Nothing
connection.CloseSession ("ses[0]")
Set connection = Nothing
'Clear table from SMS Input
ThisWorkbook.Worksheets("SMS Input").Select
Cells.Select
Selection.ClearContents
'Insert .xlsx file data
Cells.Select
Selection.Delete Shift:=xlUp
Range("A6").Select
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
"OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=C:\Users\" & Environ("Username") & "\Downloads\SAP_export.XLSX;Mode=Share Deny None" _
, _
";Extended Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OL" _
, _
"EDB:Engine Type=37;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Je" _
, _
"t OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Lo" _
, _
"cale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Je" _
, "t OLEDB:Bypass UserInfo Validation=False"), Destination:=Range("$A$6")). _
QueryTable
.CommandType = xlCmdTable
.CommandText = Array("Sheet1$")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = "C:\Users\" & Environ("Username") & "\Downloads\SAP_export.XLSX"
.ListObject.DisplayName = "Table_export3"
.Refresh BackgroundQuery:=False
.MaintainConnection = False
End With
导入文件后,我解析一些列,并跨工作表组织信息。
在代码的最后,我尝试了上面提到的不同变体。
'Open the export and then close to avoid it opening at end of macro.
Workbooks.Open Filename:="C:\Users\" & Environ("Username") & _
"\Downloads\SAP_export.XLSX"
Workbooks("SAP_export.XLSX").Close savechanges:=False
Application.Wait Now + TimeValue("0:00:01")
Kill "C:\Users\" & Environ("Username") & "\Downloads\SAP_export.XLSX"
起初,我认为这是使用OLEDB导入.XLSX这一部分的问题,但事实并非如此,如果我在关闭连接后结束代码,文件将在结束时自动打开。我看过其他关于这个问题的帖子,但没有可靠的答案。希望这是很清楚的..。
提前感谢您的反馈,
发布于 2016-11-23 17:02:51
进一步的建议:
在VBA程序结束时(未做任何更改),运行以下脚本。
例如:
. . .
'Open the export and then close to avoid it opening at end of macro.
set Wshell = CreateObject("WScript.Shell")
Wshell.run "c:\tmp\SAP_Workbook_Close.vbs",1,false
End Sub
SAP_Workbook_Close.vbs:
SAP_Workbook = "SAP_export.xlsx"
on error resume next
do
err.clear
Set xclApp = GetObject(, "Excel.Application")
If Err.Number = 0 Then exit do
'msgbox "Wait for Excel session"
wscript.sleep 2000
loop
do
err.clear
Set xclwbk = xclApp.Workbooks.Item(SAP_Workbook)
If Err.Number = 0 Then exit do
'msgbox "Wait for SAP workbook"
wscript.sleep 2000
loop
on error goto 0
Set xclSheet = xclwbk.Worksheets(1)
xclApp.Visible = True
xclapp.DisplayAlerts = false
xclapp.ActiveWorkbook.Close
Set xclwbk = Nothing
Set xclsheet = Nothing
'xclapp.Quit
set xclapp = Nothing
致敬,ScriptMan
发布于 2018-06-06 03:57:44
我的解决方案是如果有人还需要帮助的话。我创建了一个sub来杀死很多我导出的文件。thx脚本人..。
.findById("wnd[0]/mbar/menu[0]/menu[10]/menu[3]/menu[1]").Select
.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Informe\"
.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "tcurr.xlsx"
.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 10
.findById("wnd[1]/tbar[0]/btn[11]").press
End With
'Application.Wait Now + TimeValue("00:00:02")
Call Esperar("tcurr.XLSX")
Public Sub Esperar(Optional ByVal archivo As String)
On Error Resume Next
Set Wshell = CreateObject("WScript.Shell")
Do
Err.Clear
Set xclapp = GetObject(, "Excel.Application")
If Err.Number = 0 Then Exit Do
DoEvents '---> This do the trick in VB7
'msgbox "Wait for Excel session"
wscript.Sleep 2000
Loop
Do
Err.Clear
Set xclwbk = xclapp.Workbooks.Item(archivo)
If Err.Number = 0 Then Exit Do
DoEvents
'msgbox "Wait for SAP workbook"
wscript.Sleep 2000 Loop
On Error GoTo 0
Set xclsheet = xclwbk.Worksheets(1)
xclapp.Visible = True
xclapp.DisplayAlerts = False
xclapp.ActiveWorkbook.Close
Set xclwbk = Nothing
Set xclsheet = Nothing
End Sub
发布于 2020-03-06 17:43:30
今天我看到了这个问题的另一个解决方案。我真的很想和你分享。
例如:
...
' Export from SAP to .xlsx file.
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL"
Session.findById("wnd[1]/tbar[0]/btn[0]").press
Session.findById("wnd[1]/usr/ctxt[0]").Text = "C:\tmp"
Session.findById("wnd[1]/usr/ctxt[1]").Text = "SAP_export.XLSX"
Session.findById("wnd[1]/tbar[0]/btn[11]").press
Dim xlApp As Object
Application.Wait Now + TimeSerial(0, 0, 5)
Set xlApp = GetObject("c:\tmp\SAP_export.XLSX").Application
xlApp.Workbooks(1).Close False
'xlApp.Quit
...
致敬,ScriptMan
https://stackoverflow.com/questions/40729968
复制相似问题