InternetExplorer Object from Hwnd...


New Member
Dec 20, 2012
Hi All,

I'm aware a number of people have asked this around the internet, and that the accepted solution in general uses the SHDocVw.ShellWindows collection. However, this doesn't work properly for me ("Out of memory error" when constructing the class, probably I'm working on a VM which doesn't maintain a collection of windows for taskbar use).

It does look like someone else has already figured it out - in C/C++/C#/Whatever language I'm about to post. I can understand what it does, but I'm too new to COM programming concepts and GUIDs to know what the equivalent functions are in VBA. So, does anyone have any idea how to implement the below in VBA? I've marked up the bits I understand and have an equivalent for, and those that are brand new.

Code in question comes from the solution here.

      public interface IServiceProvider
         void QueryService(ref Guid guidService, ref Guid riid,
          [MarshalAs(UnmanagedType.Interface)] out object IEObj);

[SIZE=2]      /// <summary></summary>[/SIZE]
[SIZE=2]      /// Gets the IWebBrowser2 object based on Window Handle
The Hanlde of Internet Explore_Server
      /// <returns>IWebBroswer2 Object</returns>
      private IWebBrowser2 GetIEObjFromHnd(IntPtr theHwnd)
         IWebBrowser2 aWB=null;
         System.Guid IID_IHTMLDocument = typeof(mshtml.IHTMLDocument2).GUID;
         UIntPtr sendMessageResult = UIntPtr.Zero;

         uint aMssg = RegisterWindowMessage("WM_HTML_GETOBJECT");

         SendMessageTimeout(theHwnd, aMssg, UIntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out sendMessageResult);

         if(sendMessageResult != UIntPtr.Zero)
[SIZE=2]         mshtml.IHTMLDocument2 aHtmlDoc= [/SIZE]
[SIZE=2]                    (mshtml.IHTMLDocument2)ObjectFromLresult[/SIZE]
[SIZE=2]                         (sendMessageResult, IID_IHTMLDocument, IntPtr.Zero);

            IServiceProvider aSp = (IServiceProvider)aHtmlDoc.parentWindow;
            Guid aGUID_BrsApp = typeof(IWebBrowserApp).GUID;
            Guid aGUID_WebBrs = typeof(IWebBrowser2).GUID;
            object aObj;

               aSp.QueryService(ref aGUID_BrsApp, ref aGUID_WebBrs, out aObj);

               aWB = (IWebBrowser2)aObj;
            catch(Exception e)
               int err = Marshal.GetLastWin32Error();

            return aWB;
            return null;

In particular, I'm struggling with the typeof(mshtml.IHTMLDocument2).GUID equivalent, and the declaration of what looks like IServiceProvider at the top.

Many thanks in advance.

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...