I had the same problem and found a solution at
http://codecentrix.blogspot.com/2008/02/when-ihtmlwindow2document-throws.html
Here comes the code:
using System.Runtime.InteropServices;
...
IHTMLWindow2 win = fbel.contentWindow as
IHTMLWindow2;
if (win != null)
{
IHTMLDocument2 doc;
try
{
doc = win.document as IHTMLDocument2;
}
catch (System.UnauthorizedAccessException ex)
{
// this exception happens with (I)FRAMES
from different domain
// solution adapted from:
//
http://codecentrix.blogspot.com/2008/02/when-ihtmlwindow2document-throws.html
doc = null;
IServiceProvider sp = (IServiceProvider)
win;
Object brws = null;
sp.QueryService(ref IID_IWebBrowserApp,
ref IID_IWebBrowser2, out brws);
// Get the document from IWebBrowser2.
IWebBrowser2 browser = (IWebBrowser2)
(brws);
doc = (IHTMLDocument2)browser.Document;
}
...
private static Guid IID_IWebBrowserApp = new Guid
("0002DF05-0000-0000-C000-000000000046");
private static Guid IID_IWebBrowser2 = new Guid("D30C1661-
CDAF-11D0-8A3E-00C04FC9E26E");
[ComImport(), ComVisible(true), Guid
("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceTypeAttribute
(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServiceProvider
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int QueryService(ref Guid guidService, ref Guid riid,
[MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}
....
DF