ActiveX Microsoft Web浏览器对象(将网页嵌入excel文件)

Qbi*_*bik 0 excel vba webbrowser-control excel-vba

我尝试将网页嵌入到.xlsm Excel 2010文件中.那就是我到目前为止所做的:
在Sheet1中,我插入了名为"Microsoft Web Browser"的ActiveX控件,其默认名称是WebBrowser1(Developer-> Insert-> More Controls-> Microsoft Web Browser).然后使用VBA编辑器,我将以下代码放入表单模块Sheet1:

Private Sub Worksheet_Activate()
WebBrowser1.Navigate "http://stackoverflow.com/"
End Sub
Run Code Online (Sandbox Code Playgroud)

我已尝试将各种网站和html文件放在我的本地硬盘上,但WebBrowser1控件中显示的内容始终相同 - "Internet Explorer无法显示网页"错误,单独使用Excel VBA时没有错误.似乎Microsoft Web Browser Object无法建立与网页的连接.

Leo*_*lla 5

试试这个:

    'declare the web browser object for future reference and / or for listening to its events
    Dim WithEvents ie As WebBrowser

    'navigate when the worksheet is activated.
    Private Sub Worksheet_Activate()
        Set ie = ActiveSheet.WebBrowser1
        ie.Navigate2 "http://stackoverflow.com/"
    End Sub
Run Code Online (Sandbox Code Playgroud)

  • @Qbik 声明 Dim WithEvents 让我们注册有问题的对象,以便 VBA 可以侦听它的事件,就像工作表对象默认公开它的事件(onActivate、SelectionChange 等)一样。例如,对于 WebBrowser ,您将能够在 OnQuit 、 onDownloadComplete 事件等上进行编码。它实际上并没有改变它的属性。 (2认同)