我正在尝试从SAP导出。导出完成后,我需要关闭导出文件。我在下面的代码中编写了执行相同的操作的方法,但是我的问题是代码没有等到文件加载完毕并跳过导出文件的关闭。请帮助我解决这个问题。
Sub FBL5N()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
Dim sApplication
Dim session
Dim file As String
Dim objShell As Object
Dim strFolder As String
Dim wb As Workbook
Dim wbk As Workbook
Dim ws As Worksheet
Dim lrw As Integer
Dim Wshell As Object
Set objShell = CreateObject("Wscript.Shell")
strFolder = objShell.SpecialFolders("mydocuments") & "\Temp\"
file = strFolder & "\ARREPORT.xlsx"
If FileExists(file) Then 'See above
SetAttr file, vbNormal
On Error Resume Next
Kill file
On Error Resume Next
End If
If Not IsObject(sApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set sApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = sApplication.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 sApplication, "on"
End If
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nFBL5N"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtDD_KUNNR-LOW").Text = ThisWorkbook.Sheets("Formulated").Range("R9")
session.findById("wnd[0]/usr/ctxtDD_KUNNR-HIGH").Text = ThisWorkbook.Sheets("Formulated").Range("R10")
session.findById("wnd[0]/usr/ctxtDD_BUKRS-LOW").Text = ThisWorkbook.Sheets("Formulated").Range("R8")
session.findById("wnd[0]/usr/ctxtPA_STIDA").Text = ThisWorkbook.Sheets("Formulated").Range("R11")
session.findById("wnd[0]/usr/ctxtPA_VARI").Text = ThisWorkbook.Sheets("Formulated").Range("R12")
session.findById("wnd[0]/usr/ctxtPA_VARI").SetFocus
session.findById("wnd[0]/usr/ctxtPA_VARI").caretPosition = 12
session.findById("wnd[0]/mbar/menu[0]/menu[0]").Select
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = strFolder
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "export.xlsx"
session.findById("wnd[1]").sendVKey 0
Application.Wait Now() + TimeValue("00:00:01")
For Each wb In Application.Workbooks
If wb.Name = "export.xlsx" Then
wb.Close
End If
Next wb
End Sub
答案 0 :(得分:0)
我将举一个例子。因为我猜我的编辑有些过分,而且我尊重这个网站的存在,所以这里不仅要教别人为别人做所有的工作。如此指导他们,以便他们实际上了解我所做的所有更改的含义以及执行它们的原因。
'This is the fun stuff using your computer to sleep in milliseconds. Since we are teaching and i am not going to assume anything. Here is the code for both versions of excel.
'Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)' API Call for 64bit excel
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) ' API Call for 32bit excel
' This is turning off all your screen update functions
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.AskToUpdateLinks = False
End with
' Add Code here
' You can remove your application.wait and try
Sleep 100 ' this is milliseconds so you may have to fine tune it by adding more time or less
Doevents ' Doevents lets the macro keep running and will keep "Not Responding" from showing up at the top of your workbook.
BeforeExit: ' I always add a BeforeExit: So i Don't forget to turn my settings back on to see my hard work
Set objShell = nothing ' It is good practice to always release your objects as well
With Application
.ScreenUpdating = true
.DisplayAlerts = true
.AskToUpdateLinks = true
End with
您能向我解释一下SAP吗?我看到您的代码,也看到您的呼叫键,但我不确定这是用于SAP还是Excel。如果可以避免再次调用键盘功能,则不是一个好习惯。