从 VBA 控制 Internet Explorer

Control Internet Explorer from VBA

我创建了一个 macro/userform,它创建了一个 html 文件并希望 Internet Explorer 11 显示该页面。
当您使用用户窗体时,您会创建应显示在页面上的新数据。如何在 IE11 上刷新页面?

我尝试使用元刷新,但如果用户窗体在 IE 刷新页面时写入页面,页面将变为空白并停止刷新。

我试过的另一种方法是sendkeys,但它似乎不起作用。

AppActivate "The page - Internet Explorer"
SendKeys "{F5}", True

没有任何反应。

是否有任何其他选项可以确保页面刷新并始终可见? (意思是用户窗体正在使用文件时不要刷新)

如果页面已经打开,那么您需要找到该特定页面,然后您可以尝试刷新它。您可以尝试按标题查找该页面。

请参阅下面的示例代码。

Sub demo1()
flag = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next
    my_url = objShell.Windows(x).Document.Location
    my_title = objShell.Windows(x).Document.Title

    If my_title Like "Microsoft" & "*" Then
        Set ie = objShell.Windows(x)
        ie.Refresh
        flag = 1
        Exit For
    Else
    End If
Next

If flag = 0 Then
    Debug.Print ("A matching webpage was NOT found")
Else
    Debug.Print ("A matching webpage was found")
End If
End Sub

尝试修改代码中的标题以匹配您的网页。