Solved

Changing url in webbrowser control

Posted on 2004-10-03
6
1,480 Views
Last Modified: 2008-02-01
Hi,
In order to avoid loading images for "some" urls that I go to the the webbrowser control, I've tried the following code.  The problem is that none of the relative links work in the webbrowser when the page loads.  I think that's because the locationurl is still about:blank.  I try below using the putproperty method of the webbrowser control to set the locationurl property manually however it doesn't seem to work.  Any ideas?


Private Sub Form_Load()
'First time round, make sure
'there is a document in the webbrowser

WebBrowser1.Navigate ("about:blank")

'Navigate to a page without images like this:
NoImageNavigate ("http://www.google.com")

End Sub

Private Sub NoImageNavigate(myURL As String)
Dim myhtml As String
  myhtml = Inet1.OpenURL(myURL)
  'Writing this string to the webbrowser will be enough
  'to lose the relative references to images.
  'But the following 'replaces' will kill
  'any absolute references also. The browser
  'will ignore the 'bad syntax' that this creates
  myhtml = Replace(myhtml, "<img ", "<p ")
  myhtml = Replace(myhtml, "<IMG ", "<p ")
  myhtml = Replace(myhtml, "< img ", "<p ")
  myhtml = Replace(myhtml, "< IMG ", "<p ")

 
  WebBrowser1.Document, "LocationURL", myURL
 
  With WebBrowser1.Document
    .open
    .write myhtml
    .Close
  End With
End Sub
0
Comment
Question by:jodyglidden
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 12213141
You can change the image source in this way:

Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
   
    Dim i As Long

    For i = 0 To WebBrowser1.Document.images.length - 1
   
        WebBrowser1.Document.images.Item(i).src = "file://c:\repl.jpg" '<< Point to Replacement image
   
    Next
   
End Sub

Hope it helps!
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12213266
And another way:

Dim iTag As IHTMLElement 'Reference the Microsoft HTML Object Library

For Each iTag In WebBrowser1.Document.All.tags("img")
    iTag.src = "c:\repl.jpg"
Next
0
 
LVL 1

Author Comment

by:jodyglidden
ID: 12213324
Hi,
Actually, what I was trying to do is download without downloading the images.  That's why I used the inet control to download the html, then took out the old not working image tags.  My code works but then none of the links work.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 32

Accepted Solution

by:
Erick37 earned 500 total points
ID: 12214346
You could write your modified HTML to a file, then navigate to it.  Note also I modified the tags so they are retained as "images" - that way the image placeholder and alt text will display.

Dim sHTML As String

sHTML = Inet1.OpenURL("http://www.weather.com")

sHTML = Replace(sHTML, "img src", "img alt")
sHTML = Replace(sHTML, "IMG SRC", "img alt")

'Just as illustration - perhaps use a system generated temp file name
Open "c:\myhtml.html" For Output As #1
Print #1, sHTML
Close #1

WebBrowser1.Navigate2 "file:///c:/myhtml.html"

Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Loop

0
 
LVL 1

Author Comment

by:jodyglidden
ID: 12214360
Good thinking.  I hadn't thought of that approach.  How would we retain the necessary cookies though so that links would work?  By the way, the code for some reason seems to be loading images anyway.  Not sure why that would be.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12293954
Hi jodyglidden,

I've been following your quest to control what the Webbrowser downloads.  Unfortunately there does not seem to be a good answer for VB6.

I downloaded the Emorcillo project and got the same error you reported in your other question.  What little documentation there was stated the code was tested with Windows Me (not NT, 2000, XP).

So far the only viable solution seems to be wrapping the Webbrowser control inside a C++ ATL project in order to catch the request for DISPID_AMBIENT_DLCONTROL.  But this is beyond me, and the ones capable of doing so probably aren't using VB anyway!

I'll keep my eyes open for you.

Best of luck.

Some reading:
See the section "The Good, the Bad, and the Heart-Thumping News"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui10232001.asp
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

734 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