【问题标题】:delay SAP until dialog window open延迟 SAP 直到对话窗口打开
【发布时间】:2019-07-25 14:30:14
【问题描述】:

这是我的 VBA 代码:

 子 logowanie()

    UserForm1.显示

    vSAP = Shell("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus)
    调用 Shell("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus)
    设置 WSHShell = CreateObject("WScript.Shell")

    直到 WSHShell.AppActivate("SAP Logon")
    Application.Wait Now + TimeValue("0:00:01")
    环形
    设置 SapGui = GetObject("SAPGUI")
    设置 Appl = SapGui.GetScriptingEngine

    Application.Wait Now + TimeValue("0:00:01")
    设置连接 = Appl.Openconnection("xxxxxxxxxx", True)

    Application.Wait Now + TimeValue("0:00:02")
    WSHShell.SendKeys UserForm1.TextBox1.Value
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys UserForm1.TextBox2.Value
    WSHShell.SendKeys "{ENTER}"
    Application.Wait Now + TimeValue("0:00:01")
    WSHShell.SendKeys "y_ecd_96000032"
    WSHShell.SendKeys "{ENTER}"
    Application.Wait Now + TimeValue("0:00:01")
    WSHShell.SendKeys "{DOWN}"
    WSHShell.SendKeys "{DOWN}"
    WSHShell.SendKeys "{DOWN}"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "22:00:00"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "*"
    WSHShell.SendKeys "{ENTER}"
    Application.Wait Now + TimeValue("0:00:02")
    WSHShell.SendKeys "DC15"
    Application.Wait Now + TimeValue("0:00:02")
    WSHShell.SendKeys "{ENTER}"
    Application.Wait Now + TimeValue("0:00:02")
    WSHShell.SendKeys "{DOWN}"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "{ENTER}"
    Application.Wait Now + TimeValue("0:00:02")
    WSHShell.SendKeys "^{TAB}"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "{ENTER}"
    Application.Wait Now + TimeValue("0:00:02")
    WSHShell.SendKeys "U:\[...]\a.txt"
    WSHShell.SendKeys "{ENTER}"
    Application.Wait Now + TimeValue("0:00:03")
    WSHShell.SendKeys "{F8}"
    Application.Wait Now + TimeValue("0:00:03")
    WSHShell.SendKeys "{F8}"
    Application.Wait Now + TimeValue("0:00:03")
    WSHShell.SendKeys "+{F4}"
    Application.Wait Now + TimeValue("0:00:02")
    WSHShell.SendKeys "U:\[...]\SRET.xlsx"
    WSHShell.SendKeys "{ENTER}"
    'Application.Wait Now + TimeValue("0:00:03")
    WSHShell.SendKeys "{LEFT}"
    WSHShell.SendKeys "{ENTER}"
    Application.Wait Now + TimeValue("0:00:04")
    应用激活 (vSAP)
    Application.Wait Now + TimeValue("0:00:02")
    WSHShell.SendKeys "%{F4}"
    Application.Wait Now + TimeValue("0:00:02")
    WSHShell.SendKeys "{TAB}"
    WSHShell.SendKeys "{ENTER}"

    结束子

我需要 Application.Wait Now + 命令之间的 [某个时间值]。但有时它不起作用,当 SAP 工作太慢时。怎么做?当每个对话框窗口出现时,我需要一些带有 Application.Wait 的循环。

【问题讨论】:

    标签: excel vba sap


    【解决方案1】:

    我认为最简单的方法是使用 WinAPI 来测试当前窗口的名称。试试这个:

    Public Declare Function GetForegroundWindow Lib "user32" () As Long
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal HWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    Sub Test()
        WaitForWindow "Notepad"
        Debug.Print "Notepad is opened"
    End Sub
    
    Sub WaitForWindow(Title As String)
        Dim TopWindow As String
        Do
            DoEvents
            TopWindow = WindowTitle
            Application.Wait Now + TimeValue("0:00:01")
        Loop Until InStr(1, TopWindow, WindowTitle, vbTextCompare) > 0
    End Sub
    
    Function WindowTitle()
        Dim WinText As String
        Dim HWnd As Long
        Dim L As Long
        HWnd = GetForegroundWindow()
        WinText = String(255, vbNullChar)
        L = GetWindowText(HWnd, WinText, 255)
    
        WindowTitle = Left(WinText, InStr(1, WinText, vbNullChar) - 1)
    End Function
    

    当您运行 Test 方法时,它会等待标题中带有 Notepad 的内容。而是将您的对话框标题放在调用中。

    【讨论】:

    • 嘿,一切看起来都很好(谢谢!),但我有一个问题:AppActivate (vSAP) Application.Wait Now + TimeValue("0:00:02") WSHShell.SendKeys "%{F4}" Application.Wait Now + TimeValue("0:00:02") WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{ENTER}" 它没有关闭 SAP。我需要激活子窗口,而不是“SAP Logon 740”。
    • 即使我理解你的问题,我也很难从我所在的地方帮助你。这是特定于您的设置,由于我没有 SAP,我不知道要解决什么问题。话虽如此,使用其他 WinAPI 调用的选项可以帮助您。
    【解决方案2】:

    这里是解决方案:

    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    
    Public Sub IEFrameToTop()
     Dim THandle As Long
    
     THandle = FindWindow(vbNullString, "**** name of child window here ****")
    
     If THandle = 0 Then
      MsgBox "Could not find window.", vbOKOnly
     Else
      SetForegroundWindow (THandle)
    
      SendKeys "%{F4}"
      Application.Wait (Now + TimeValue("0:00:02"))
      SendKeys "{TAB}"
      SendKeys "{ENTER}"
    
     End If
    End Sub
    

    感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多