Link to home
Start Free TrialLog in
Avatar of kristofer
kristofer

asked on

webbrowser object

Hi

I'm using the webbrowser object in my program, and I'm wondering if you can block the user from taking certain actions. For example I don't want the user to be able to use backspace to go to the previous page. I want to disable all links and buttons except one on a specific html page.

all help welcome
Avatar of AzraSound
AzraSound
Flag of United States of America image

1) you can store the URL history as you go along.  if you only want to trap the last page, store each URL visited into a public variable, and in the BeforeNavigate event, check if the URL is the same as the last one visited.  in the NavigateComplete event you can store the URL.

2) you will need to use the Microsoft HTML Object Library for this (Project -> References and select Microsoft HTML Object Library)

i dont have the time to offer a cut-paste solution, but the idea should be this:

Private WithEvents htmlWin As HTMLWindow
Private withEvents htmlDoc As HTMLDocument

Private Sub WebBrowser_DocumentComplete(...)
   If pDisp Is WebBrowser.Object Then
      Set htmlDoc = webBrowser.Document
      Set htmlWin = WebBrowser.Document.parentWindow
   End If
End Sub

Private Sub htmlDoc_onclick() As Boolean
   If htmlWin.event.srcElement.name <> "name of element you are allowing" Then
      ...
   End If
End Sub
Avatar of kristofer
kristofer

ASKER

thanks a lot, I don't however have time to try it out now since my workday is over, but I'll try it on monday and post a new post then. It sounds like that would work, but if anyone else has another solution then lets hear it.

BTW, "If htmlWin.event.srcElement.name <> "name of element you are allowing" Then" what "name" do you mean?
in the html source code, the element should have some characteristic so that you can distinguish it from others, e.g.,

<input type="button" name="btnSubmit">

and the name would be "btnSubmit".  it may also be referred to as id, or it may not have either.  but youll need something to discern it from other elements on the page...
Avatar of Richie_Simonetti
hearing...
aha I get it, it's for buttons, I can get it to do something when pressed, but how do i prevent it from sending whatever it's told to send when pressed?

Also what about hyperlinks? I don't have to block specific ones, all of the links should be blocked.

thx a lot
well for hyperlinks, i would just use the beforenavigate event.  it has a parameter called 'Cancel'.  set it to true inside the event and it will cancel navigation.  if you do navigation yourself via code, just set a flag that only allows navigation when you yourself are explicitly performing it.

to cancel a button press, building on my example above:

Private Sub htmlDoc_onclick() As Boolean
   If htmlWin.event.srcElement.name <> "name of element you are allowing" Then
      'cancel the action
      htmlDoc_onclick = False
   Else
      'do whatever you want to here
   End If
End Sub
When I add the the sub "Private Sub htmlDoc_onclick() As Boolean" I get a compile error: expected: End of statement and it marks the "As". Why do I get that error msg?
I got error msg:es with almost every part of that code...

Private WithEvents htmlWin As HTMLWindow -> User defined type not defined
It works with "HTMLWindow2" however...

Private Sub WebBrowser_DocumentComplete(ByVal pDisp As Object, URL As Variant)

    If pDisp Is WebBrowser.Object Then
   
        Set htmlDoc = WebBrowser.Document -> run time error '13': Type Mismatch
        Set htmlWin = WebBrowser.Document.parentWindow -> run time error '13': Type Mismatch


    End If
   
End Sub

uhmm, now what?
i have doubled the points, please help!
>>It works with "HTMLWindow2" however...

ok use that, i wrote all the code from memory and could not remember the exact class names for every object


>>Set htmlDoc = WebBrowser.Document -> run time error '13': Type Mismatch

do you have htmlDoc declared as HTMLDocument?
I'm sorry, I probably wasn't clear enough. When I said that HTMLWindow2 works, I meant the declaration works, not the use of it i.e. assigning an object to it. I have both htmlWin and htmlDoc declared as public in the general declaration, and I still get a run time error: type mismatch from both.

Exact code I'm using:

General declaration:
Public WithEvents htmlWin As HTMLWindow2
Public WithEvents htmlDoc As HTMLDocument

Subs:
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
   
    If pDisp Is WebBrowser1.Object Then
   
        Set htmlDoc = WebBrowser1.Document
        Set htmlWin = WebBrowser1.Document.parentWindow
       
    End If
   
End Sub
I have used similar code earlier in the program to predefine the text in textboxes on a HTML page.

My declarations allready used and working are:

Dim Html2 As IHTMLDocument
Dim Inputstag As IHTMLElementCollection
Dim Elementtag1 As IHTMLElement
Dim InputElementtag1 As IHTMLInputTextElement

Set Html2 = WebBrowser1.Document
Set Inputstag2 = Html2.All.tags("INPUT")
Set Elementtag4 = Inputstag2.Item("NAME OF TEXTBOX")
Set InputElementtag4 = Elementtag4
InputElementtag4.Value = "Hello"

I don't know what the difference between the HTMLDocument and IHTMLDocument is, but it might help you figure out what's wrong.

If you try to use
Public WithEvents htmlWin As IHTMLWindow2 or
Public WithEvents htmlDoc As IHTMLDocument
you get the error msg: 'Compile error: Object does not source automation events'

I suppose it's the "WithEvents" that creates  that error, what does that part of the code do? Is it that you can't use htmlDoc_onlick() without it?

thanks
Ok, I've figured out how to disable submit buttons:

Public htmlDoc As IHTMLDocument
Public Inputs As IHTMLElementCollection
Public Element As IHTMLElement
Public Button1 As IHTMLInputButtonElement

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)  
 
  Set htmlDoc = WebBrowser1.Document
  Set Inputs = htmlDoc.All.tags("INPUT")
  Set Element = Inputs.Item(0)
  Set Button1 = Element
  Button1.disabled = True

End Sub

I think it's possible to do similar thing with the links, if you could get each link into an IHTMLLinkElement, and then set object.href ="javascript:void(null)". But when I follow the same procedure with a link:

Public htmlDoc As IHTMLDocument
Public Links As IHTMLElementCollection
Public Link1 As IHTMLElement
Public LinkEl1 As IHTMLLinkElement

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)  
 
  Set htmlDoc = WebBrowser1.Document
  Set Links = htmlDoc.All.tags("A")
  Set Link1 = Links.Item(2)
  Set LinkEl1 = Link1 'here I get a run time error '13': Type mismatch
  LinkEl1.href = "javascript:void(null)"

I think the run time error might have something to do with what it says in MSDN under IHTMLLinkElement: "This element can be used only within the HEAD tag". So your first solution is probably preferable, if you can make it work!

thanks a lot
strange...you are using the webbrowser control correct?  and not automating IE?

after doing the Set htmlDoc = WebBrowser.Document, in the Immediate window, try both of these:

?htmlDoc Is Nothing
?htmlDoc.parentWindow Is Nothing

neither should return true, or otherwise you should be getting a different error.  i'm not sure whats wrong, to tell you the truth.  i've offered almost identical solutions before and people have gotten it to work.  just out of curiosity, what version of IE are you using?  and VB?  any service packs?
I'm pretty sure I'm using the webbrowser object correctly. Automating IE what does that mean? (I'm not english).

They result in:
?htmlDoc Is Nothing -> TRUE
?htmlDoc.parentWindow Is Nothing -> Compile Error: can't find project or library

I'm using IE 4.0, VB 6.0 and I don't know of any service packs (may have installed 4.0, don't really remember).

Could it be that I have the wrong versions of the dll's and tlb's? Which have you added when it works?

thanks
I'm pretty sure I'm using the webbrowser object correctly. Automating IE what does that mean? (I'm not english).

They result in:
?htmlDoc Is Nothing -> TRUE
?htmlDoc.parentWindow Is Nothing -> Compile Error: can't find project or library

I'm using IE 4.0, VB 6.0 and I don't know of any service packs (may have installed 4.0, don't really remember).

Could it be that I have the wrong versions of the dll's and tlb's? Which have you added when it works?

thanks
I'm pretty sure I'm using the webbrowser object correctly. Automating IE what does that mean? (I'm not english).

They result in:
?htmlDoc Is Nothing -> TRUE
?htmlDoc.parentWindow Is Nothing -> Compile Error: can't find project or library

I'm using IE 4.0, VB 6.0 and I don't know of any service packs (may have installed 4.0, don't really remember).

Could it be that I have the wrong versions of the dll's and tlb's? Which have you added when it works?

thanks
I'm pretty sure I'm using the webbrowser object correctly. Automating IE what does that mean? (I'm not english).

They result in:
?htmlDoc Is Nothing -> TRUE
?htmlDoc.parentWindow Is Nothing -> Compile Error: can't find project or library

I'm using IE 4.0, VB 6.0 and I don't know of any service packs (may have installed 4.0, don't really remember).

Could it be that I have the wrong versions of the dll's and tlb's? Which have you added when it works?

thanks
automating IE means launching an instance of Internet Explorer itself, and controlling it from your VB application.  

well, for some reason htmlDoc is not being set to the Document object of your webbrowser control (it is returning that it is in fact, Nothing).  i dont see how this can be...you are loading plain html files correct?
If you are using a page with with frames, we are in trouble here since documentcomplete event is fired for every frame.
The first Azra comment take care of this but in kristofer last posted comment i didn't see it.
Hey, there is a problem with EE. I can't catch new pages, my browser always returns what is in its cache.
I'm loading plain HTML, no frames!
I tried the code in VB at home and it suddenly works... The problem I get now is with the next part of the code.
just creating this causes a compile error: expected end of statement:

Private Sub htmlDoc_onclick() As Boolean
End Sub

Are you sure that HTMLDocuments support the _onclick() part?

I looked through what I can use after htmlDoc, i.e. htmlDoc.something, and I discovered that it has an onclick. How do you use this and what is it used for?
its an event of the HTMLDocument object.  just like you would add a command button to your form, open up the code pane, and select the command button from the dropdown list and select its relevant events...you can do the same once you declare this variable in your code module:

Private WithEvents htmlDoc As HTMLDocument

one of the events is onclick which will fire everytime the user clicks somewhere inside the document.
How do I use it then? As I said, I get an error msg when I try to type:

Private Sub htmlDoc_onclick() As Boolean
End Sub

It marks the "As" and: expected end of statement
sorry, it should be a function:

Private Function htmlDoc_onclick() As Boolean

End Function

and in there you should be able to test for things like i stated above:

If htmlWin.event.srcElement.name = ....
Thanks I'll try that as soon as I get home!

Why do you think it works differently on the two computers? Do you think it's because mshtml.tlb aren't the same versions on the two computers, it should be that file that causes the problem... am I right? I even tried to copy the project from my home computer here and when I launched it, it gave the same error msg as before.
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh ok, if that's the problem, it won't be a problem for my application, because all of my users will be running Internet explorer 5.0 =)

I'll try to check if it works as soon as possible and give you the points.
I'm pretty sure I worked it all out now...

in htmlWin.event.srcElement.name there is no .name attribute, so I used htmlWin.event.srcElement.outerHTML instead, and it works. Thanks for all your help =)
well, thats due to the fact that a generic HTML element isnt gauranteed to have a "name" attribute attached to it...but if they srcElement that fired the event was a button, for example, and it did have a "name" attribute, it would still function ok.

glad you got it working...let us know if you run into any other snags.