【问题标题】:Macro code is not waiting until file load from SAP宏代码不会等到从 SAP 加载文件
【发布时间】: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

标签: excel vba sap-gui


【解决方案1】:

我只是举个例子。因为我想我的编辑有点过火了,我尊重这个网站在这里教的不仅仅是让人们为某人做所有的工作。更重要的是要引导他们,让他们真正了解我所做的所有更改的含义以及我这样做的原因。

'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。如果您可以避免调用键盘函数,这不是一个好习惯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多