Question

I'm having a problem with the InternetExplorer in SHDocVw.dll. I also have mshtml.tlb referenced (while googling, I did read 1 comment that said I should have mshtml.dll referenced, but that this couldn't be done in Microsoft Visual Studio Express, I don't know how true this is though). Here's one small function in its most basic form that won't work for me:

public static HtmlElement GetDocumentControlByID(
    ref SHDocVw.InternetExplorer IEObj, 
    string ControlID)
{
    HtmlElement ReturnElement = IEObj.Document.GetElementById(ControlID);
    return ReturnElement;
}

The problem is that when I create the IEObj instance, it is considered type System.__ComObject instead of SHDocVw.InternetExplorer, and all subparts are also of type System.__ComObject. When I try any of the following statements...

Document WebDoc = IEObj.Document;
HtmlElement ReturnElement = IEObj.Document.GetElementById(ControlID);

...I keep getting the same error message:

Cannot implicitly convert type 'System.__ComObject' to 'System.Windows.Forms.HtmlElement' (obviously the convert-to type is different for the IEObj.Document).

I am new to c# (coming from VBA, so I am familiar with programming), but in VBA, the equivelant works perfectly without needing to convert it in any way.

Is it something I'm doing wrong? In case it's my creation of the object, the following is (roughly) the code I used to test the function:

public static void Main(String [] args)
{
    SHDocVw.InternetExplorer IEObj = new SHDocVw.InternetExplorer();
    IEObj.Navigate("http://sports.ladbrokes.com/");
    while (IEObj.ReadyState != 4)
    {

    }

    // There is a textbox that definitely exists

    HtmlElement NetControl = GetDocumentControlByID(ref IEObj, "username");

    // I was goint to manipulate it after this, but it crashes in the above function.

}

All I really want to do is latch onto various elements so that I can enter text into textboxes, click buttons etc. I would also need to be able to use the Document variables as well (like Document.Body.InnerHtml, etc). The whole project is to be a bunch of functions to be contained in a DLL to be referenced by other projects.

Was it helpful?

Solution

You're trying to use the WinForms HtmlElement class, which isn't a COM object.
You can't mix the native InternetExplorer COM object with the managed classes in WinForms.

You should use the WinForms classes (the WebBrowser control) instead.
In most cases, you don't need COM at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top