Solved

Changing url in webbrowser control

Posted on 2004-10-03
6
1,429 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

867 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

19 Experts available now in Live!

Get 1:1 Help Now