Solved

Changing url in webbrowser control

Posted on 2004-10-03
6
1,421 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
  • 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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