VBA,在浏览器关闭和重新打开后获取并管理活动网页



在我的访问中&VBA应用程序我必须获取和管理Web订单应用程序的网页。通常,我使用此帖子中指示的解决方案,该解决方案通常完美地工作:

vba打开网页,登录并获取打开的页面

但是,现在我还有另一个旧的Web应用程序可以连接我的访问软件。在此Web应用程序中,登录后,网页自行关闭,并使用第一个用户页面打开其他浏览器会话。换句话说:

1(用户用用户凭据填写登录表格2(登录后,登录网页关闭浏览器3(其他浏览器会话使用Web应用程序控制面板打开

如果使用链接解决方案,我会看到错误。我如何在新的浏览器会话中获取新的网页,该页面在登录后打开它?

谢谢!

编辑

我尝试了以下代码(我的目的是在网页中设置文本值(。但是,非常奇怪的是,它只有在我在代码中放置一个文本框时才能起作用:也许代码必须等待一些事件!我该如何解决?

Dim SWs As SHDocVw.ShellWindows, vIE As SHDocVw.InternetExplorer
Dim winShell As Shell
Dim dt As Date
dt = DateAdd("s", 10, DateTime.Now)
Dim ieA As InternetExplorer
Dim IeDocA As HTMLDocument
Do While dt > DateTime.Now
   Set winShell = New Shell
   For Each ieA In winShell.Windows
       If ieA.LocationURL = sURL Then
       ieA.Visible = True
       ieA.Silent = True
       Do While ieA.Busy: DoEvents: Loop
       Do Until ieA.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
       Set IeDocA = ieA.Document
       
       //only with this msgbox it works, how can I do this w/out it?       
       MsgBox IeDocA.title
       With IeDocA
             .getElementsByName("text").value="something"
       End With
       Set winShell = Nothing
       Exit Do
       End If
   Next ieA
   Set winShell = Nothing
   DoEvents
Loop

您可以在窗口中查找URL。您将需要参考Microsoft Shell控件和自动化。

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Desc: The Function gets the Internet Explorer window that has the current
'   URL from the sURL Parameter.  The Function Timesout after 30 seconds
'Input parameters:
    'String sURL - The URL to look for
'Output parameters:
    'InternetExplorer ie - the Internet Explorer window holding the webpage
'Result: returns the the Internet Explorer window holding the webpage
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function GetWebPage(sUrl As String) As InternetExplorer
Dim winShell As Shell
Dim dt As Date
dt = DateAdd("s", 30, DateTime.Now)
Dim ie As InternetExplorer
Do While dt > DateTime.Now
    Set winShell = New Shell
    'loop through the windows and check the internet explorer windows
    For Each ie In winShell.Windows
        'check the URL
        If ie.LocationURL = sUrl Then
            'set some properties
            ie.Visible = True
            ie.Silent = True
            Set GetWebPage = ie
            'loop while IE is busy
            Do While ie.Busy
                DoEvents
            Loop
            Set winShell = Nothing
            Exit Do
        End If
    Next ie
    Set winShell = Nothing
    DoEvents
Loop
End Function

最新更新