代码之家  ›  专栏  ›  技术社区  ›  Andreas

从VBA控制Internet Explorer

  •  -1
  • Andreas  · 技术社区  · 6 年前

    我创建了一个宏/用户表单,它创建了一个html文件,并希望InternetExplorer11显示该页面。

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

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

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

    什么也没发生。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Deepak-MSFT    6 年前

    若页面已经打开,那个么你们需要找到那个特定的页面,然后你们可以尝试刷新它。您可以尝试通过标题查找页面。

    请参见下面的示例代码。

    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