Solved

webbrowser object

Posted on 2001-06-21
31
250 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:kristofer
  • 18
  • 10
  • 3
31 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 6214806
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
0
 
LVL 2

Author Comment

by:kristofer
ID: 6214865
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?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6215090
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...
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6215593
hearing...
0
 
LVL 2

Author Comment

by:kristofer
ID: 6215870
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
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6215894
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
0
 
LVL 2

Author Comment

by:kristofer
ID: 6223504
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?
0
 
LVL 2

Author Comment

by:kristofer
ID: 6223590
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?
0
 
LVL 2

Author Comment

by:kristofer
ID: 6224228
i have doubled the points, please help!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6224569
>>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?
0
 
LVL 2

Author Comment

by:kristofer
ID: 6226662
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
0
 
LVL 2

Author Comment

by:kristofer
ID: 6226730
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
0
 
LVL 2

Author Comment

by:kristofer
ID: 6227125
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
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6227585
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?
0
 
LVL 2

Author Comment

by:kristofer
ID: 6227661
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
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Author Comment

by:kristofer
ID: 6227675
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
0
 
LVL 2

Author Comment

by:kristofer
ID: 6227692
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
0
 
LVL 2

Author Comment

by:kristofer
ID: 6227696
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
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6227736
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?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6227762
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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6227889
Hey, there is a problem with EE. I can't catch new pages, my browser always returns what is in its cache.
0
 
LVL 2

Author Comment

by:kristofer
ID: 6230050
I'm loading plain HTML, no frames!
0
 
LVL 2

Author Comment

by:kristofer
ID: 6230083
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?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6230103
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.
0
 
LVL 2

Author Comment

by:kristofer
ID: 6230216
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
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6231415
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 = ....
0
 
LVL 2

Author Comment

by:kristofer
ID: 6231451
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.
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 100 total points
ID: 6231475
my guess is that its related to your IE version yes...i guess you cant do all of this fun stuff with IE4?   cant say for sure though.
0
 
LVL 2

Author Comment

by:kristofer
ID: 6231502
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.
0
 
LVL 2

Author Comment

by:kristofer
ID: 6233280
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 =)
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6233363
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now