title: IWebBrowser2.Document not returning IHTMLDocument2
body: <p>I am trying to cast an IWebBrowser2 COM object into a IHTMLDocument2 so that I can manipulate the contents of the IE web browser.</p>
  
  <p>Here is the start of my code:</p>
  
  <pre><code>int main()
  {
      if (SUCCEEDED(OleInitialize(NULL)))
      {
          CComQIPtr&lt;IWebBrowser2&gt; pBrowser2;
          CComQIPtr&lt;IDispatch&gt; pDispatch;
  
          CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
              IID_IWebBrowser2, (void**)&amp;pBrowser2);
          if (pBrowser2)
          {    
              //Here, pDispatch remains null and hr == E_FAIL
              HRESULT hr = pBrowser2-&gt;get_Document(&amp;pDispatch);
          }
          OleUninitialize();
      }    
  }
  </code></pre>
  
  <p>At the call to IWebBrowser2::get_Document() the pDispatch variable remains null and the returned HRESULT is E_FAIL.</p>
  
  <p>What do I need to do to get the IHTMLDocument2 object from the IWebBrowser2?</p>