Andriy
Andriy

Reputation: 41

Cannot cast 'mshtml.HTMLDocumentClass' to 'mshtml.IMarkupServices'

I have a problem casting mshtml.HTMLDocumentClass to mshtml.IMarkupServices

IHTMLDocument2 doc = new HTMLDocumentClass();
doc.write(new object[] { item.HTMLBody });
IHTMLTxtRange range = ((IHTMLBodyElement)doc.body).createTextRange();
IHTMLTxtRange baseRange = ((IHTMLBodyElement)doc.body).createTextRange();

var markupServices = (IMarkupServices)doc; // <--- exception is thrown here

The same code works in one case but throws an exception in another. The calls are the same but doesn't work the same way in different places of code.

Upvotes: 3

Views: 772

Answers (1)

Andriy
Andriy

Reputation: 41

Problem was with ApartmentState:

thread.SetApartmentState(Thread.CurrentThread.GetApartmentState());

Upvotes: 1

Related Questions