InternetExplorer Object from Hwnd...

Tobriand

New Member
Joined
Dec 20, 2012
Messages
7
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.

Unknown:
Code:
[SIZE=2][ComImport]
      [Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
      [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      public interface IServiceProvider
      {
         void QueryService(ref Guid guidService, ref Guid riid,
          [MarshalAs(UnmanagedType.Interface)] out object IEObj);
      }//IServiceProvider
[/SIZE]


Known:
Code:
[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]
[SIZE=2]         mshtml.IHTMLDocument2 aHtmlDoc= [/SIZE]
[SIZE=2]                    (mshtml.IHTMLDocument2)ObjectFromLresult[/SIZE]
[SIZE=2]                         (sendMessageResult, IID_IHTMLDocument, IntPtr.Zero);
[/SIZE]


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

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

               aWB = (IWebBrowser2)aObj;
               
            }//try
            catch(Exception e)
            {
               int err = Marshal.GetLastWin32Error();
            }//catch

            return aWB;
            
         }//if
         else
            return null;

      }//GetIEObjFromHnd

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.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top