如何在Excel VBA表单中嵌入浏览器? [英] How do I embed a browser in an Excel VBA form?

查看:362
本文介绍了如何在Excel VBA表单中嵌入浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,我曾经使用过Microsoft WebBrowser 控件以许多VB6和Excel VBA形式提供.我需要再做一次,但是我不知道该怎么做.

In the past I have used the Microsoft WebBrowser control in many VB6 and Excel VBA forms. I need to do it again, but I can't figure out how to do it.

我尝试打开一个旧的VB6项目,但收到此错误消息:

I tried opening an old VB6 project and I got this error message:

Line 17: Class SHDocVwCtl.WebBrowser of control WebBrowser was not a loaded control class.

我尝试将对Microsoft Web Browser的引用添加到新的VBA项目中,但是它不存在.

I tried adding a reference to Microsoft Web Browser to a new VBA project, but it doesn't exist.

我尝试根据建议

I tried adding a reference to msi.dll as suggested here, but didn't help.

我在开发人员功能区上尝试-插入-更多控件-Microsoft Web浏览器,它确实存在,但显示为Cannot insert object.

I tried from the Developer ribbon - Insert - More Controls - Microsoft Web Browser, it does exist, but it says Cannot insert object.

我尝试按照此处所述使用Shell.Explorer.2对象,但出现错误:

I tried using the Shell.Explorer.2 object as described here, but I get an error:

Sheet1.OLEObjects.Add ClassType:="Shell.Explorer.2", Left:=147, Top:=60.75, Width:=400, Height:=400

Run-time error '1004': 
Cannot insert object.

唯一可行的方法是打开IE窗口,但是我需要将浏览器嵌入表单中,因为我需要添加一些自定义按钮:

The only way that worked was opening an IE window, but I need the browser to be embedded in a form because I need to add a few custom buttons:

Set IE = CreateObject("InternetExplorer.Application") 
IE.Visible = True

推荐答案

我无法像以前那样使旧的Web浏览器控件出现在VBA控件工具箱中,但是我能够动态地将其插入代码中的表格.因此,我无法将小地球仪拖放到表单中,但这不是问题.

I was not able to get the good old Web Browser control to appear in the VBA control toolbox as it used to, but I was able to dynamically insert it in a form from the code. So I can't drag the little globe into a form, but that's not a problem.

一些评论提到更改注册表中的值.这是将嵌入式浏览器插入工作表中所必需的,但是对表单没有影响.

Some comments mentioned changing a value in the registry. That is required in order to insert an embedded browser inside a worksheet, but has no effect on the form.

添加对Microsoft Internet控件(ieframe.dll)的引用,然后以以下形式使用以下代码就足够了:

It was enough to add a reference to Microsoft Internet Controls (ieframe.dll), then use the following code in a form:

Option Explicit

Private WithEvents WB As WebBrowser

Private Sub UserForm_Activate()
  Set WB = Me.Controls.Add("Shell.Explorer.2")
  WB.Navigate "http://google.com"
End Sub

Private Sub WB_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
  Debug.Print "WB_NavigateComplete2"
End Sub

编辑

@VictorK在评论中提醒我,我需要右键单击工具箱以添加控件.我确实记得添加引用后在其中添加了控件,但是我不记得(也不认为)右键单击并添加控件.

@VictorK in a comment reminded me that I need to right click on the toolbox to add the control. I did remember about the control being added there after adding the reference, but I didn't remember (nor think) to right click and add the control.

所以...我要做的就是添加对ieframe.dll的引用而不是Microsoft Web Browser,只是因为Microsoft Web Browser没有出现在列表中.其他一切似乎都正常.

So... all I needed to do was to add a reference to ieframe.dll instead of Microsoft Web Browser, just because Microsoft Web Browser doesn't appear in the list. Everything else seems to be working fine.

我仍然不知道为什么旧的VB6应用程序无法加载,但是现在我真的不在乎.

I still don't know why the old VB6 application doesn't load, but at this point I really don't care.

这篇关于如何在Excel VBA表单中嵌入浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆