How can I do it?
Printable View
How can I do it?
Is it possible btw?
I'll investigate.
Thanks for the tip, now I know what I should be looking for.
I have a method, but it involves shelling the "Internet Options" control panel, and then using Sendkeys to check or uncheck the "Show Pictures" option under the Advanced tab.
Actually all that does is change a setting in the registry. You could try changing the setting, I'm not sure what registry key it is. It's under Internet Explorer...that much I know. If I remember right changing this setting in the registry didn't work for me, that's why I had to do it the hard way by shelling "Internet Options" control panel.
If you want the code for shelling the control panel let me know....I'll dig it up.
You are in luck. Take a hatchet to it.
Code:Private Sub WebBrowser1_StatusTextChange(ByVal Text As String)
With WebBrowser1
lblAddress.Caption = Now & " " & " State=" & .ReadyState & " " & Text
End With
Select Case WebBrowser1.ReadyState
Case READYSTATE_INTERACTIVE
Handler.DropImages
End Select
End Sub
'Class already has Document object
Public Sub DropImages()
'*****
'* Kill all IMGs for faster loading.
'* We don't need no stinking pictures.
'*****
Dim objElements As Object
Dim objElement As Object
Dim I As Long
Do
If Len(mstrDownload) = 0 Then Exit Do ' in case manual
If mobjDocument Is Nothing Then Exit Do
On Error Resume Next
Set objElements = mobjDocument.getElementsByTagName("IMG")
If Err.Number <> 0 Then Exit Do
If objElements Is Nothing Then Exit Do
For I = objElements.length - 1 To 0 Step -1 ' Delete backwards
Set objElement = objElements.Item(I)
If Err.Number <> 0 Then Exit Do
If objElement Is Nothing Then Exit Do
If LCase(objElement.tagName) = "img" Then
objElement.outerHTML = ""
End If
Next
Exit Do: Loop
End Sub
John -
Your code looks good, but there are a few things missing. I tried running it as is, and recieved an error: object required - highlighting "Handler.DropImages". I also notice that you didn't declare "mobjDocument", and what is "mstrDownload". One last thing does one need to reference the HTML Object library?
Sorry if it sounds like I'm being picky, I'm not. I'd like to try this code out, but there is just too much missing for me to try and put 2 and 2 together. Could you clean it up a little, I know this would help me an others to understanding and using it.
Thanks...
Ok, I converted it from a class method to an ordinary sub.
mobjDocument was an Instance (Module) level varable already set to the document. Handler was an object containing the class. See, I am using one form for multiple navigations. I have to get through five forms for sign-on before I can ask for payload.
Code:Private Sub WebBrowser1_StatusTextChange(ByVal Text As String)
With WebBrowser1
lblAddress.Caption = Now & " " & " State=" & .ReadyState & " " & Text
End With
Select Case WebBrowser1.ReadyState
Case READYSTATE_INTERACTIVE
DropImages WebBrpwser.Document
End Select
End Sub
Public Sub DropImages(mobjDocument as HTMLDocument)
'*****
'* Kill all IMGs for faster loading.
'* We don't need no stinking pictures.
'*****
Dim objElements As Object ' Use objects instead of specific
Dim objElement As Object
Dim I As Long
Do
If Len(mstrDownload) = 0 Then Exit Do ' in case manual
If mobjDocument Is Nothing Then Exit Do
On Error Resume Next
Set objElements = mobjDocument.getElementsByTagName("IMG")
If Err.Number <> 0 Then Exit Do
If objElements Is Nothing Then Exit Do
For I = objElements.length - 1 To 0 Step -1 ' Delete backwards
Set objElement = objElements.Item(I)
If Err.Number <> 0 Then Exit Do
If objElement Is Nothing Then Exit Do
If LCase(objElement.tagName) = "img" Then
objElement.outerHTML = ""
End If
Next
Exit Do: Loop
End Sub
Here is an example of handing a form of "Redirection" I ran into plus not reacting to documentComplete for frames. I use the event model of the webbrowser rather than DoEvents on webbrowser.busy. Fewer problems in the long-run.
Code:Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
' Process if whole page is done not just a frame
If Not (pDisp Is WebBrowser1.object) Then Exit Sub
'*****
'* Reday State must be interactive or complete
'*****
Select Case WebBrowser1.ReadyState
Case READYSTATE_INTERACTIVE, READYSTATE_COMPLETE
Handler.DocumentComplete
End Select
End Sub
'--------------------------- Class Method
Public Sub DocumentComplete()
Dim objElements As Object
Dim objElement As Object
Dim strBody As String
Dim blnBusy As Boolean
Set mobjDocument = mobjWeb.Document
blnBusy = False
Do
With mobjWeb
Set mobjDocument = mobjWeb.Document
If Err.Number <> 0 Then Exit Do
If mobjDocument Is Nothing Then Exit Do
'*****
'* META http-equiv="Refresh" BYPASS REDIRECTION
'*****
Set objElements = mobjDocument.getElementsByTagName("Meta")
If Err.Number <> 0 Then Exit Do
If objElements Is Nothing Then Exit Do
For Each objElement In objElements
If LCase(objElement.httpEquiv) = "refresh" Then
'* We are still busy
blnBusy = True
Exit Do
End If
Next
If Err.Number <> 0 Then Exit Do
'*****
'* CallByName Page Processor
'*****
Dim strDownLoad As String
strDownLoad = mstrDownload
mstrDownload = ""
If Len(strDownLoad) > 0 Then
CallByName Me, strDownLoad, VbMethod
'*****
'* Are we still running
'****
If Len(mstrDownload) > 0 Then
blnBusy = True
ElseIf mblnAutoRunMode = True Then
Quit
End If
End If
End With
Exit Do: Loop
mblnBusy = blnBusy
End Sub
This looks like good stuff.....unfortunately I can't get it to work. I have made a reference to the HTML object library, but your code for removing images doesn't seem to work for me. Here's what I have. You'll notice I have corrected your typo where you call the DropImages Sub. As far as your other code, The name of the Sub "DocumentComplete" has me a little confused. Is this Sub the DocumentComplete Event of a WBC, or is this a Sub that gets called, and if it does where would you call it?
One last thing, I have looked all over for information on the various Classes of the MSHTML library, but have found very little info. What is it that you use for reference (a book perhaps) for this library?
VB Code:
Private Sub Form_Load() WebBrowser1.Navigate "http://www.yahoo.com" End Sub Private Sub WebBrowser1_StatusTextChange(ByVal Text As String) With WebBrowser1 lblAddress.Caption = Now & " " & " State=" & .ReadyState & " " & Text End With Select Case WebBrowser1.ReadyState Case READYSTATE_INTERACTIVE DropImages WebBrowser1.Document End Select End Sub Public Sub DropImages(mobjDocument As HTMLDocument) '***** '* Kill all IMGs for faster loading. '* We don't need no stinking pictures. '***** Dim objElements As Object ' Use objects instead of specific Dim objElement As Object Dim I As Long Do If Len(mstrDownload) = 0 Then Exit Do ' in case manual If mobjDocument Is Nothing Then Exit Do On Error Resume Next Set objElements = mobjDocument.getElementsByTagName("IMG") If Err.Number <> 0 Then Exit Do If objElements Is Nothing Then Exit Do For I = objElements.length - 1 To 0 Step -1 ' Delete backwards Set objElement = objElements.Item(I) If Err.Number <> 0 Then Exit Do If objElement Is Nothing Then Exit Do If LCase(objElement.tagName) = "img" Then objElement.outerHTML = "" End If Next Exit Do: Loop End Sub
I removed the reference to mstrDownload. What does not work?
As far as DocumentComplete, yes it is called from DocumentComplete in the Webbrowser. You are going to have learn enough to pull out of examples what you need. If you do not have a lblAddress then put it on your form in so you can see what is happenning. I am stll "in process" after two weeks of full-time learning and doing. Somethings have to be tried without full knowledge. I didn't think or learn about redirection until I saw it.
Use your View Source. or at documentcomplete have a multiline textbox with scrollbars and do text1.text = webbrowser.document.documentelement.outerHTML and look at it.
I use
http://www.w3.org/TR/REC-DOM-Level-1...-one-html.html
(read it all)
http://msdn.microsoft.com MSDN / Web Development / Programming and Reusing the Browser and and
O'REILLY Dynamic HTML. (but not much).
Code:Private Sub Form_Load()
WebBrowser1.Navigate "http://www.yahoo.com"
End Sub
Private Sub WebBrowser1_StatusTextChange(ByVal Text As String)
With WebBrowser1
lblAddress.Caption = Now & " " & " State=" & .ReadyState & " " & Text
End With
Select Case WebBrowser1.ReadyState
Case READYSTATE_INTERACTIVE
DropImages WebBrowser1.Document
End Select
End Sub
Public Sub DropImages(mobjDocument As HTMLDocument)
'*****
'* Kill all IMGs for faster loading.
'* We don't need no stinking pictures.
'*****
Dim objElements As Object ' Use objects instead of specific
Dim objElement As Object
Dim I As Long
Do
If mobjDocument Is Nothing Then Exit Do
On Error Resume Next
Set objElements = mobjDocument.getElementsByTagName("IMG")
If Err.Number <> 0 Then Exit Do
If objElements Is Nothing Then Exit Do
For I = objElements.length - 1 To 0 Step -1 ' Delete backwards
Set objElement = objElements.Item(I)
If Err.Number <> 0 Then Exit Do
If objElement Is Nothing Then Exit Do
If LCase(objElement.tagName) = "img" Then
objElement.outerHTML = ""
End If
Next
Exit Do: Loop
End Sub
Hey Thanks!, it's working now. I didn't notice mstrDownload was still in there. That's what the problem was, but now it is working. Thanks for the links, I'm familar with some of them. I thought you might be working out of, or referencing a book that's why I asked.
This gives me a few ideas. One of them is removing IFRAMES, which are usually used for advertising. I should be able to adapt your "removing images" code for this. Thanks again for the code.
It's a bit for answering to this topic, but I want to kill the images and I get the message "User defined type not defined" with VB highlighting "Public Sub DropImages(mobjDocument As HTMLDocument)". How do I get past this error?
Thanks,
You need to make a reference to the MSHTML.tlb
(Microsoft HTML Object Library)
Er, thanks, but how do I do that (in code)?
Thanks again,
It's not something you do in code. It's a setting that you make in VB. I can't remember off-hand what menu it is under. It's under the same menu....where you add a component, if that helps. Find it in the menu at the top, then click on References, and scroll down until you find "Microsoft HTML Object Library". Then check the box......you've just made a reference.
Hope that helps...:)
You can can change As HTMLDocument to As Object. This will then work regardless of the IE version i.e. use "late binding".
Thanks Bloodeye, it did help and now it works, it was under the Project Menu.
And thanks John for replying,