我的问题是导出的 Excel 文件在宏完成后打开。我无法用其他文章解决这个问题。有人有想法吗?
我尝试了多次提示来关闭文件,但没有任何结果。 最好是它不会自行打开。
Sub Makro()
Dim SapGuiAuto, Application, Connection, session As Object
' First I just open SAP and insert the string'
session.findById("wnd[2]/usr/ctxtDY_PATH").Text = "C:\\TEMP"
session.findById("wnd[2]/usr/ctxtDY_FILENAME").Text = "EXPORT_" & i & ".XLSX"
session.findById("wnd[2]/tbar[0]/btn[11]").Press
session.findById("wnd[1]").Close
session.findById("wnd[0]/tbar[0]/btn[3]").Press
Range("B3").Select
Selection.Copy
Workbooks.Open ("C:\\TEMP\EXPORT_" & i & ".XLSX")
Windows("EXPORT_" & i & ".XLSX").Activate
Set mp = Range("B3")
eintragmp = Mid(mp.Value, 1, 2)
Windows("SAP Verknüpfung.xlsx").Activate
Cells(i, 6) = eintragmp
Workbooks("EXPORT_" & i & ".XLSX").Close SaveChanges:=False
' filePath = "C:\TEMP\EXPORT_" & i & ".XLSX"
' If Dir(filePath) <> "" Then
' Kill filePath
Else
Windows("SAP Verknüpfung.xlsx").Activate
Cells(i, 6) = "Fertig"
If Cells(i, 6) = "Fertig" Then
Cells(i, 7) = "Umlauf"
End If
End If
On Error GoTo 0
Next i
session.findById("wnd[0]/tbar[0]/btn[3]").Press
Set Application = Nothing
Set session = Nothing
Set Connection = Nothing
End Sub
我导出后关闭导出文件的做法如下
Sub Makro
' The variable xlfile should contain the filename but not the complete path
' in your case it is probably "EXPORT_" & i & ".XLSX"
Dim xlfile as string
' Code to download the file
runCloseWorkbook xlFile, 0
End Sub
过程
runCloseWorkbook
将尝试关闭工作簿一定次数。通常,从 SAP 下载 Excel 文件是异步运行的,而 VBA 代码继续执行。因此,代码尝试在所有 Excel 实例中搜索名为 xlfile
的打开工作簿,并尝试将其关闭。如果未找到该文件,它将在五秒后重试。代码将在 10 次尝试后停止此过程。这种方法对我来说效果很好。
Option Explicit
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Private Declare Function IIDFromString Lib "ole32" _
(ByVal lpsz As Long, ByRef lpiid As GUID) As Long
Private Declare Function AccessibleObjectFromWindow Lib "oleacc" _
(ByVal hWnd As Long, ByVal dwId As Long, ByRef riid As GUID, _
ByRef ppvObject As Object) As Long
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Const S_OK As Long = &H0
Private Const IID_IDispatch As String = "{00020400-0000-0000-C000-000000000046}"
Private Const OBJID_NATIVEOM As Long = &HFFFFFFF0
Sub runCloseWorkbook(wkbName As String, counter As Long)
Const MAX_TRIES As Long = 10
If closeWorkbook(wkbName) Then
Exit Sub
Else
counter = counter + 1
If counter <= MAX_TRIES Then
Application.OnTime Now + TimeValue("00:00:05"), "'runCloseWorkbook """ & wkbName & """,""" & counter & "'"
End If
End If
End Sub
Function closeWorkbook(wkbName As String) as Boolean
Dim hWinXL As Long
hWinXL = FindWindowEx(0&, 0&, "XLMAIN", vbNullString)
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Do While hWinXL > 0
If getXLApp(hWinXL, xlApp) Then
For Each wb In xlApp.Workbooks
If wb.Name = wkbName Then
wb.Close False
closeWorkbook = True
' xlApp.Quit
End If
Next
End If
hWinXL = FindWindowEx(0, hWinXL, "XLMAIN", vbNullString)
Loop
End Function
Function getXLApp(hWinXL As Long, xlApp As Excel.Application) As Boolean
' Function GetXLApp(hWinXL As Long, xlApp As Object) As Boolean
Dim hWinDesk As Long, hWin7 As Long
Dim obj As Object
Dim iid As GUID
Call IIDFromString(StrPtr(IID_IDispatch), iid)
hWinDesk = FindWindowEx(hWinXL, 0&, "XLDESK", vbNullString)
hWin7 = FindWindowEx(hWinDesk, 0&, "EXCEL7", vbNullString)
If AccessibleObjectFromWindow(hWin7, OBJID_NATIVEOM, iid, obj) = S_OK Then
Set xlApp = obj.Application
getXLApp = True
End If
End Function
您可能需要更换线路
Workbooks("EXPORT_" & i & ".XLSX").Close SaveChanges:=False
在您的代码中使用以下行
runCloseWorkbook "EXPORT_" & i & ".XLSX", 0