webbrowser.DocumentStream question

G

Guest

Hello !
I made some test with webbrowser control in VS.2005. When I load a word
document in webbrowser through .Navigate method (from my hdd), I want to get
the stream of the loaded doc file with .DocumentStream, but this property is
null. The property .DocumentText is = "" too. Am I missing something ?
Thank you very much !
 
T

Tim Anderson

Toma Marinov said:
Hello !
I made some test with webbrowser control in VS.2005. When I load a word
document in webbrowser through .Navigate method (from my hdd), I want to
get
the stream of the loaded doc file with .DocumentStream, but this property
is
null. The property .DocumentText is = "" too. Am I missing something ?
Thank you very much !

The webbrowser loads documents asynchronously. This means that you cannot
access the document immediately after loading it. You have to wait for the
load to complete. You can handle the DocumentCompleted event or check the
ReadyState for a value of Complete.

Tim
Visual Studio 2005 DLL Hell:
http://www.itwriting.com/blog/?postid=261
 
G

Guest

Hello and thank you for your answer !
At the begining I thought that this was the problem, but when I process
DocumentCompleted event, I saw that the word document was loaded very fast.
After that on button.click event I try to acces DocumentStream or
DocumentText properties...But they are null, ""...
I'll continue to examine this situation...
Kind regards,
Toma
 
T

Tim Anderson

Toma Marinov said:
Hello and thank you for your answer !
At the begining I thought that this was the problem, but when I process
DocumentCompleted event, I saw that the word document was loaded very
fast.
After that on button.click event I try to acces DocumentStream or
DocumentText properties...But they are null, ""...
I'll continue to examine this situation...

Apologies, I actually missed that it was a *word* document you were trying
to access.

If you look here:

http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/overview/Overview.asp

you will see that the WebBrowser control is an ActiveX document container,
and that while it normally hosts MSHTML (to render HTML), it can also host
other types of document.

Note this remark:
"Because WebBrowser objects are ActiveX objects, the WebBrowser objects can
host objects other than HTML documents. Check for the type of document you
are hosting."

and this remark concerning the WebBrowser's Document property:

"When other document types are active, such as a Microsoft Word document,
this property returns the document automation object of that document. For
Word documents, this would be the Document object."

However, the WebBrowser wrapper in VS 2005 exposes a Document property that
is specifically an HtmlDocument. You can overcome this by importing the
ActiveX Microsoft WebBrowser control, in which the Document property is of
type object. Using the following code I successfully cast this to a Word
document in DocumentComplete:

private void axWebBrowser1_DocumentComplete(object sender,
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)

{

mshtml.HTMLDocument htmldoc;

Microsoft.Office.Interop.Word.Document worddoc;

if (this.axWebBrowser1.Document is Microsoft.Office.Interop.Word.Document) {

MessageBox.Show("Got a word document");

}

if (this.axWebBrowser1.Document is mshtml.HTMLDocument)

{

MessageBox.Show("Got an HTML document");

}


}



.... So, how do you get at this property while using the new wrapper? My
first thought was to cast the ActiveXInstance property to an

AxSHDocVw.AxWebBrowser, but that doesn't appear to work. Ideas anyone?

Tim

Read my tech blog:

http://www.itwriting.com/blog
 
T

Tim Anderson

... So, how do you get at this property while using the new wrapper? My

VB can do it. This works:

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
Handles WebBrowser1.DocumentCompleted
Dim doc As Microsoft.Office.Interop.Word.Document

Dim docobj As Object

docobj = Me.WebBrowser1.ActiveXInstance.document

If TypeOf (docobj) Is Microsoft.Office.Interop.Word.Document Then

doc = docobj

MessageBox.Show("Got a word doc")

End If

End Sub

So what is the equivalent C# code?

Read my tech blog:

http://www.itwriting.com/blog
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top