【发布时间】:2018-10-03 00:05:55
【问题描述】:
我正在尝试从 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
【问题讨论】:
-
正确使用“Declare Sub Sleep Lib”kernel32”(ByVal dwMilliseconds As Long)”和“DoEvents”。永远是我的救星。
-
嗨瑞奇,感谢您的帮助。我不是VBA专家。您能否解释一下如何用您的解决方案解决这个问题。
-
我已经更新了你的代码,但它说它正在接受同行评审,直到它发布。如果明天还没有更新,我会想出一条新路线。我给了你一个良好的开端,但我没有做所有的腿部工作。这就是学习的乐趣所在。我希望它很快就会发布。祝你有个愉快的夜晚!
-
嗨 Ricky,对不起,我尝试了你在第一条评论中所说的任何内容,但我仍然遇到问题。这可能是因为放置错误。所以我问你。感谢帮助。我正在等待更新的代码。希望它会尽快发布。
-
您最大的问题实际上是简单的问题。您已关闭所有内容,但没有重新打开。在代码底部添加以下内容,但将 false 更改为 true With Application .ScreenUpdating = True .DisplayAlerts = True .AskToUpdateLinks = True end with