我在Excel VBA中使用COM库。在过去,我有HTMLDocument用VBA代码抛出的沉没事件(这对我来说很有用!)。这似乎不再起作用。我已经调查并提供了大量细节。我已经使用OleView钻入了类型库,并使用我认为正在进行的内容注释了VBA代码。这可能是一个错误,或者它可能是我刚刚搞砸了的东西。请帮忙。
目标是让doc_ondblclick中的代码运行,即将事件处理程序连接起来。目前虽然我可以访问一个IHTMLDocument接口(感谢StackOverflow回答另一个问题);我无法成功访问发生事件的界面。
更新:此代码适用于运行Windows XP,Excel 2007,IE8的旧笔记本电脑!所以我认为这是一个错误! **进一步更新:我怀疑这项技术太旧了,现在已经被IE11从IE11中放弃http://msdn.microsoft.com/en-us/library/ie/bg182625%28v=vs.85%29.aspx#legacyAPIs。为了观察突变,微软有突变观察者http://msdn.microsoft.com/en-us/library/ie/dn265034%28v=vs.85%29.aspx
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