Excel 15 VBA无法从Windows 8.1上的IE接收事件
我在Excel VBA中使用COM库。在过去,我用VBA代码击沉了HTMLDocument抛出的事件,这是我的游戏!。这似乎不再有效。我已经调查过了,有很多细节可以提供。我已经用OleView钻研了类型库,并用我认为下面正在发生的事情注释了VBA代码。这可能是一个bug,也可能是我刚刚搞砸的东西。请帮忙 目标是让doc_ondblclick中的代码运行,即让事件处理程序连接起来。目前,虽然由于StackOverflow回答了另一个问题,我可以访问一个IHTMLDocument界面;我无法成功访问引发事件的接口 更新:此代码适用于运行Windows XP、Excel 2007和IE8的旧笔记本电脑!所以我认为这是一个错误! **进一步更新:我怀疑这项技术太旧了,现在已经被微软在IE11以后的版本中放弃了。为了观察变异,微软有变异观察员Excel 15 VBA无法从Windows 8.1上的IE接收事件,vba,internet-explorer,excel,dom,Vba,Internet Explorer,Excel,Dom,我在Excel VBA中使用COM库。在过去,我用VBA代码击沉了HTMLDocument抛出的事件,这是我的游戏!。这似乎不再有效。我已经调查过了,有很多细节可以提供。我已经用OleView钻研了类型库,并用我认为下面正在发生的事情注释了VBA代码。这可能是一个bug,也可能是我刚刚搞砸的东西。请帮忙 目标是让doc_ondblclick中的代码运行,即让事件处理程序连接起来。目前,虽然由于StackOverflow回答了另一个问题,我可以访问一个IHTMLDocument界面;我无法成功访问
typenamie.Document给你什么?它返回HTMLDocument。我刚刚为您添加了一个Debug.Assert行。那么您应该能够分配Set doc=ie.Document?你有没有得到类型不匹配?你的线路不工作;我也犯了同样的错误。但是谢谢你的建议。你能在你的机器上重新创建吗?它对你有用吗?我想知道Windows 8开发团队是否没有对这样一个遗留技术COM进行回归测试。实际上,我刚刚清理了一个旧的Windows XP笔记本,代码正常工作。那么,到底是IE的最新版本还是Windows的问题?
Option Explicit
'* Attribute VB_Name = "DHTMLHandler"
'* Environment:
'* Windows 8.1 64 bit;
'* Excel Microsoft Office Home and Student 2013, Version: 15.0.4551.1512
'* Tools->References indicate following libraries checked
'* VBA : Visual Basic For Applications {000204EF-0000-0000-C000-000000000046} C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA7.1\VBE7.DLL
'* Excel : Microsoft Excel 15.0 Object Library {00020813-0000-0000-C000-000000000046} C:\Program Files\Microsoft Office 15\Root\Office15\EXCEL.EXE
'* stdole : OLE Automation {00020430-0000-0000-C000-000000000046} C:\Windows\SysWOW64\stdole2.tlb
'* Office : Microsoft Office 15.0 Object Library {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52} C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\MSO.DLL
'* SHDocVw: Microsoft Internet Controls {EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B} C:\Windows\SysWOW64\ieframe.dll
'* MSHTML : Microsoft HTML Object Library {3050F1C5-98B5-11CF-BB82-00AA00BDCE0B} C:\Windows\SysWOW64\mshtml.tlb
'* Based on code at http://support.microsoft.com/kb/246247
'* In the MSHTML type library we have these following lines for coclass HTMLDocument, indicating events interfaces (marked with "[source]")
'coclass HTMLDocument {
' [default] dispinterface DispHTMLDocument;
' [default, source] dispinterface HTMLDocumentEvents;
' [source] dispinterface HTMLDocumentEvents2;
' [source] dispinterface HTMLDocumentEvents3;
' [source] dispinterface HTMLDocumentEvents4;
'* I'd say the following line looks like it should start sinking events defined by HTMLDocumentEvents because in VBA we can only sink the [default] [source] interface
Dim WithEvents doc As HTMLDocument '* this is what I want
Dim docNoEvents As IHTMLDocument2 '* I know this works, it QIs successfully but it won't sink events!
Private Function doc_ondblclick() As Boolean
'* An example of an event being handled (at least that is if I could get the wiring to work)
Debug.Print "dblclick"
End Function
Public Sub Test()
'* this method is called from Module1 which simply instantiates this class : Dim handler As New DHTMLHandler: handler.Test
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
ie.Navigate "www.bbc.co.uk/weather"
ie.Visible = True
While ie.Busy
DoEvents
Wend
'* following on from example code at http://support.microsoft.com/kb/246247 it can seen the HTMLDocument object is
'* acquired from a browser level object. In the SHDocVw type library looking there is only 1 browser interface
'* defined which exports a Document method and that is IWebBrowser
'interface IWebBrowser : IDispatch { ...
' [id(0x000000cb), propget, helpstring("Returns the active Document automation object, if any.")]
' HRESULT Document([out, retval] IDispatch** ppDisp);
'...}
Dim itfWebBrowser As IWebBrowser
Set itfWebBrowser = ie '* QueryInterfaces for IWebBrowser
Set docNoEvents = itfWebBrowser.Document
Debug.Assert TypeName(ie.Document) = "HTMLDocument" 'Re G Serg
'* Following lines error for me with "Type Mismatch", if it fails for you please confirm by posting.
Set doc = docNoEvents
'Also the following (shorter, simpler version) doesn't work
'Set doc = ie.Document
End Sub