[英]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.