View Source from WebBrowser (2005)

D

Doe

I've been trying this for months. Doesn't work.

I've used the code in this article,
http://support.microsoft.com/kb/311288/en-us, HOW TO: Invoke the Find,
View Source, and Options Dialog Boxes for the WebBrowser Control from
Visual Basic .NET.

This code worked in VB.Net 2003 but not in VB.Net 2005.

Specifically this doesn't work... (all code is from article)

Private Function GetDocument() As mshtml.HTMLDocument
Try
Dim htm As mshtml.HTMLDocument = AxWebBrowser1.Document
GetDocument = htm
Catch
Throw (New Exception("Cannot retrieve the document from the
WebBrowser" + _
"Control: " + Err.GetException().Message))
End Try
End Function

I get an error message of can't cast WebBrowser1 to mshtml.HTML
Document. In VB.Net 2003, the browser was AxWebBrowser1, in 2005 it has
changed and is WebBrowser1 (for the first instance).

This code also does not work...

Public Sub ViewSource()
Dim cmdt As IOleCommandTarget
Dim o As Object

Try
cmdt = CType(GetDocument(), IOleCommandTarget)
cmdt.Exec(cmdGUID,
Convert.ToUInt32(MiscCommandTarget.ViewSource), _
Convert.ToUInt32
SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT), o, o)
Catch
Throw (New Exception(Err.GetException().Message))
End Try
End Sub

If I replace GetDocument() (which didn't work, see above), with
WebBrowser1.Document, it says can't cast WebBrowser1.Document to
IOleCommandTarget.

Frankly, I didn't understand this code very much in the first place.
:) But I have tested it in VB.Net 2003 and it works. In 2005 it
doesn't. The browser has changed.

I am wondering if anyone knows how to make it work?

Maybe the codes/addresses changed too as in...

Private cmdGUID As New Guid(&HED016940, -17061, _
&H11CF, &HBA, &H4E, &H0, &HC0, &H4F, &HD7, &H8, &H16)

Or maybe it's something else.

Basically clueless here.

Any help appreciated.

Marnie
 
C

Cor Ligthert [MVP]

Doe,

My expirience is that I cannot use the documentation for the AxWebbrowser
for the Webbrowser.

The confusing part is that Microsoft is talking on MSDN about the Webbrowser
where they mean the AxWebbrowser. My only solution is than to look at the
publishing date of the pages.

Cor
 
C

Charles Law

Hi Doe

The WebBrowser hasn't really changed, it has just been hand-wrapped instead
of the earlier auto-wrapping that occurred to create the AxWebBrowser1
object.

Wherever you had AxWebBrowser1.Document before, replace this with
WebBrowser1.Document.DomDocument.

HTH

Charles
 
C

Cor Ligthert [MVP]

Charles,
Wherever you had AxWebBrowser1.Document before, replace this with
WebBrowser1.Document.DomDocument.
If it is, do you have for me than a better solution for this one.

DirectCast(WebBrowser1.Document.DomDocument,
mshtml.IHTMLDocument2).designMode = "On"
If I do this with the AxWebBrowser which does not need the cast, than it has
in my opinion other results than with the WebBrowser. (In this case with the
webbrowswer the form is first "optimized" in a way I don't want, and it adds
with every "Enter" (not with a Shift Enter) a Div part while I want only a
<br>.). It has much more automatic behaviour.

I don't want that behaviour.

Have you a solution for me with the webbrowser. (I did this in past with the
axWebbrowser and know how to do that).

Cor
 
C

Charles Law

Hi Cor

Are you saying that

AxWebBrowser1.Document.designMode = "On"

and

DirectCast(WebBrowser1.Document.DomDocument,
mshtml.IHTMLDocument2).designMode = "On"

behave differently between the two controls? I haven't noticed that
particularly.

In what way do you mean that the form is "optimized", and what automatic
behaviour are you referring to?

When I press Enter on the old AxWebBrowser1 control I get <P> inserted into
the document, unless I implement GetHostInfo() to change it, in which case I
get <DIV>. I don't know of a setting that causes <br> to be inserted when
the Enter key is pressed, unless you override the behaviour of the Enter
key.

I choose the <DIV> because it has the same appearance as a single-spaced new
paragraph, whereas <P> appears to be a double-spaced new paragraph.

Charles
 
C

Cor Ligthert [MVP]

Charles,

I was in doubt if I would correct that part of the <br> however thought that
you would understand this as a kind of typo from me.

I did mean the said:
When I press Enter on the old AxWebBrowser1 control I get <P> inserted
into the document, unless I implement GetHostInfo() to change it, in which
case I get <DIV>. I don't know of a setting that causes <br> to be
inserted when the Enter key is pressed, unless you override the behaviour
of the Enter key.

This GetHostInfo() is interesting I did not know it so I did not implement
it as well not.

However this standard added (I thought even replaced) in my situation where
I start with the part <Body etc.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META content="MSHTML 6.00.2900.2802" name=GENERATOR></HEAD>
<BODY>Hi Charles </BODY></HTML>

Cor
 
C

Charles Law

Cor
This GetHostInfo() is interesting I did not know it so I did not implement
it as well not.

It is on the IDocHostUIHandler interface. I implement it like this

<code>
Public Function GetHostInfo(ByRef pInfo As DOCHOSTUIINFO) As HRESULT
Implements IDocHostUIHandler.GetHostInfo

With pInfo
.cbSize = Len(pInfo)

.dwFlags = DOCHOSTUIFLAG.DOCHOSTUIFLAG_DIV_BLOCKDEFAULT Or _
DOCHOSTUIFLAG.DOCHOSTUIFLAG_FLAT_SCROLLBAR Or _
DOCHOSTUIFLAG.DOCHOSTUIFLAG_NO3DBORDER

.dwDoubleClick = DOCHOSTUIDBLCLK.DOCHOSTUIDBLCLK_DEFAULT
.pchHostCss = 0
.pchHostNS = 0
End With

Return HRESULT.S_OK

End Function
</code>

I start my documents with an empty body tag (as well as the rest that you
showed), and then every time I press the Enter key it adds
<DIV>&nbsp;</DIV>.

Actually, I should paste this into another post that I spotted, as it turns
off the 3D-border that someone else was trying to do.

Charles
 
D

Doe

Charles,

That did the trick! Boy, is that a relief, been struggling with this
for months. Well, actually, been working on other aspects of the
program, but I kept coming back to find and view source and trying to
figure out what to do. Had almost decided to make my own find box,
which would be a major PITA.

THANKS a lot! Big weight off. Hope you see this post.

Doe :) (aka Marnie)
 

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