Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Changing url in webbrowser control

Posted on 2004-10-03
6
Medium Priority
?
1,525 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Accepted Solution

by:
Erick37 earned 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

916 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