简体   繁体   English

更改选项卡时,MultiPage 上的 WebBrowser 失败并出现 object 'IWebBrowser2' 错误的“导航”

[英]WebBrowser on MultiPage failing with a 'Navigate' of object 'IWebBrowser2' error when the tabs are changed

On Page 4 of the MultiPage form I've created a WebBrowser1 object.在 MultiPage 表单的第 4 页上,我创建了一个 WebBrowser1 object。 On Page 4 there are 2 buttons: one for msn.com, the other for google.com.在第 4 页上有 2 个按钮:一个用于 msn.com,另一个用于 google.com。 If the UserForm defaults to Page 4 when opened, the buttons work fine initially, but if one of the other Pages is selected, and then the user returns to Page 4, clicking either one of the buttons causes the macro to crash with an error message 'Navigate' of object 'IWebBrowser2' failed.如果用户窗体在打开时默认为第 4 页,则按钮最初可以正常工作,但如果选择了其他页面之一,然后用户返回到第 4 页,单击任一按钮会导致宏崩溃并显示错误消息object“IWebBrowser2”的“导航”失败。

   Private Sub CommandButton23_Click()
      Me.WebBrowser1.Navigate ("https://www.msn.com")
   End Sub

   Private Sub CommandButton24_Click()
      Me.WebBrowser1.Navigate ("https://google.com")
   End Sub

It appears that the WebBrowser needs to be "refreshed" each time Page 4 gets re/loaded.似乎每次重新/加载第 4 页时都需要“刷新”WebBrowser。 One solution is to eliminate the WebBrowser1 object from the UserForm and dynamically created a WebBrowser (wbr, below) initially, and then each time Page 4 is reselected.一种解决方案是从用户窗体中删除 WebBrowser1 object 并最初动态创建一个 WebBrowser(wbr,如下),然后每次重新选择第 4 页。

Dim wbr As SHDocVw.WebBrowser

Private Sub MultiPage1_Change()
    If MultiPage1.SelectedItem.Name = "Page 4" Then
        Set wbr = Nothing
        Set wbr = Me.MultiPage1.SelectedItem.Controls.Add("Shell.Explorer.2")

        wbr.Height = 700
        wbr.Left = 96
        wbr.Top = 24
        wbr.Width = 570
        wbr.Navigate "About:Blank"
    End If
End Sub

Private Sub UserForm_Initialize()
    Set wbr = Me.MultiPage1.SelectedItem.Controls.Add("Shell.Explorer.2")

    wbr.Height = 700
    wbr.Left = 96
    wbr.Top = 24
    wbr.Width = 570
    wbr.Navigate "About:Blank"
End Sub

I can't take credit for this solution - it was actually on another post in stackoverflow for a different problem!我不能把这个解决方案归功于这个解决方案——它实际上是在 stackoverflow 的另一篇文章中针对不同的问题!

Check it out: Resizing WebBrowser Control on Excel UserForm with DPI看看: Resizing WebBrowser Control on Excel UserForm with DPI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打开几个新标签页时,IWebBrowser2 :: Navigate2有时会冻结 - IWebBrowser2::Navigate2 occasionally freezes when opening several new tabs iwebbrowser2对象的方法文档失败 - the method document of the iwebbrowser2 object failed 对象iwebbrowser2的方法文档失败 - method document of object iwebbrowser2 failed 对象'IWebBrowser2'的Excel VBA“方法'文档'失败” - Excel VBA “Method 'Document' of object 'IWebBrowser2' failed” object 'IWebBrowser2' 的方法文档在专用公司服务器上失败 - Method Document of object 'IWebBrowser2' failed on a dedicated company server VBA IE 选中并取消选中具有相同名称和不同值的复选框“object iwebbrowser2 的文档失败”错误 - VBA IE check and uncheck the checkbox with same-name and different-values "document of object iwebbrowser2 failed" error 访问IWebBrowser2文档属性返回空 - Accessing the IWebBrowser2 Document property returns empty Excel VBA:在锁定屏幕的情况下运行 IE.ExecWB 12,2 返回“对象'IWebBrowser2'的方法'ExecWB'失败” - Excel VBA: running IE.ExecWB 12,2 with locked screen returns "Method 'ExecWB' of object 'IWebBrowser2' failed" 多页控件上的 Webbrowser 控件在页面之间切换时消失 - Webbrowser control on multipage control disappears when switching between the pages 多页标签可见,但无效 - Multipage tabs visible, but inactive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM