|
-
Dec 5th, 2005, 03:10 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Saving Web Page Images In Temp Folder!
Using the WebBrowser control, the following code ought to save 'all' images existing in a web page in the user's hard disk (please note that this isn't my code):
VB Code:
Option Explicit
' Use component M$ HTML Object Library
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Function [b]GetFile(URL As String) As String[/b]
'Purpose: returns file title of a URL or local path
GetFile = Right$(URL, Len(URL) - InStrRev(URL, "/"))
End Function
Private Sub [b]Form_Load()[/b]
WebBrowser1.Navigate2 "http://www.yahoo.com"
Dim objDoc1 As HTMLDocument
Dim objDoc2 As HTMLDocument
Dim i As Integer
Set objDoc1 = New HTMLDocument
'Create document element from url
Set objDoc2 = objDoc1.createDocumentFromUrl("http://www.yahoo.com", "")
'Wait till document has loaded
Do While objDoc2.readyState <> "interactive"
DoEvents
Loop
'Loop through images
For i = 0 To objDoc2.images.length - 1
'download images and save them in app.path
URLDownloadToFile 0, objDoc2.images.Item(i).href, App.Path & "\" & GetFile(objDoc2.images.Item(i).href), 0, 0
'MsgBox i
Next i
Set objDoc1 = Nothing
Set objDoc2 = Nothing
Beep
End Sub
There are 2 problems with this code. First, not all the images get saved in the user's hard disk. For e.g. if you navigate to www.yahoo.com, under normal circumstances, 18 images will be saved in IE's Temporary Internet Files folder but the above code only saves 7 images in the folder specified in the code. Any idea how do I overcome this?
Secondly, have a look at the DoEvents line. Because of this line, it takes about 4-5 seconds for the browser to open up but if the DoEvents line is commented, then the browser opens up in a jiffy but not a single image gets saved in the specified folder. Any idea how do I make the above code work without using DoEvents?
BTW, what does the Beep in the above code do?
Thanks,
Arpan
Last edited by arpan_de; Dec 5th, 2005 at 08:06 PM.
-
Dec 5th, 2005, 04:02 PM
#2
Hyperactive Member
Re: Someone Please Help!
Hello arpan_de,
Try to change this:
VB Code:
Do While objDoc2.readyState <> "interactive"
DoEvents
Loop
to this:
VB Code:
DoEvents
Do While objDoc2.readyState <> "interactive"
Loop
The Beep is just to Sound a tone through the computer's speaker
...maybe to let you know the proccess is done...
Best Regards,
ERAN
Eran Fox
ASSEMBLER,C,C++,VB6,SQL...
-
Dec 5th, 2005, 04:21 PM
#3
Save images from Webbrowser
I've slightly modified your code, and it seems to work.
I have used DocumentComplete event instead if a waiting loop.
VB Code:
'Place this code in a form and add a WebBrowser Control and a Command button
Option Explicit
Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
'======================================================================
Private Sub Form_Load()
WebBrowser1.Navigate "about:blank"
End Sub
'======================================================================
Private Sub Command1_Click()
WebBrowser1.Navigate "http://www.yahoo.com"
End Sub
'======================================================================
Private Function GetFile(URL As String) As String
'Purpose: returns file title of a URL or local path
GetFile = Right$(URL, Len(URL) - InStrRev(URL, "/"))
End Function
'======================================================================
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, _
URL As Variant)
'Wait till document has loaded
Dim i As Integer
Dim strImageFileName As String 'stores ONLY the filename of the image
Dim strImageAddress As String 'stores the address of the image
'Loop through images
For i = 0 To WebBrowser1.Document.Images.length - 1
strImageAddress = WebBrowser1.Document.Images.Item(i).src
strImageFileName = GetFile(strImageAddress)
'Download images and save them in app.path --->
URLDownloadToFile 0, strImageAddress, App.Path & "\" & strImageFileName, 0, 0
Next i
End Sub
'======================================================================
Last edited by iPrank; Apr 6th, 2006 at 12:06 AM.
Reason: Re-formatted code
-
Dec 5th, 2005, 05:48 PM
#4
Thread Starter
Frenzied Member
Re: Someone Please Help!
Thanks, mates, for your help. This is how I finally did it:
VB Code:
Private Sub [b]wWeb_DocumentComplete(ByVal pDisp As Object, URL As Variant)[/b]
Dim TempFolder
Dim collImages As IHTMLElementCollection
Dim img As IHTMLImgElement
Dim strFileName As String
Dim strExtension As String
Dim lResult As Long
Dim i As Long
Dim nAnomalies As Long
TempFolder = App.Path & "\Temp Files\"
Set collImages = pDisp.Document.getElementsByTagName("IMG")
For i = 0 To collImages.Length - 1
Set img = collImages.Item(i)
strFileName = Right(img.src, Len(img.src) - InStrRev(img.src, "/"))
strExtension = LCase(Right(strFileName, Len(strFileName) - InStrRev(strFileName, ".")))
If (strExtension = "gif" Or strExtension = "jpg" Or strExtension = "jpeg" Or strExtension = "png") Then
lResult = URLDownloadToFile(0, img.src, TempFolder & strFileName, 0, 0)
Else
If (InStr(1, img.mimeType, "GIF", vbTextCompare)) Then
lResult = URLDownloadToFile(0, img.src, TempFolder & "anomaly" & nAnomalies & ".gif", 0, 0)
ElseIf (InStr(1, img.mimeType, "JPG", vbTextCompare)) Then
lResult = URLDownloadToFile(0, img.src, TempFolder & "anomaly" & nAnomalies & ".jpg", 0, 0)
End If
End If
Set img = Nothing
Next i
Set collImages = Nothing
End Sub
& this works fine. Got the code from Microsoft.
Thanks once again,
Regards,
Arpan
-
Dec 5th, 2005, 08:04 PM
#5
Thread Starter
Frenzied Member
Re: [RESOLVED] Saving Web Page Images in Temp Folder!
Arpan, could you please change the thread title, just for easier searching ?
Oh! sure.........absolutely no problem.......
Arpan
-
Mar 27th, 2008, 04:47 PM
#6
Banned
Re: [RESOLVED] Saving Web Page Images in Temp Folder!
how can i use webbrowser...
i just wanna to know how ppl get pic and save it as pic
like
here pic for expln
http://brightdays.files.wordpress.co...-wilson-16.jpg
webbrowser1.nvt("http://brightdays.files.wordpress.com/2007/08/owen-wilson-16.jpg")
its goto the page
i wanna to know how can i use it as img
in webbrowser
when i hit with webbrowser it show in frame just pic not all page...
and its save as jpg img
can someone expln me plsss
Make 2 button
and then jpg pic show it in frame when i hit the link and its save as pic
thanks
-
Jan 7th, 2010, 02:23 PM
#7
New Member
Re: [RESOLVED] Saving Web Page Images In Temp Folder!
I have one possible answer to this problem. This code downloads webpage and saves both html and image files. Hope it helps.
vb Code:
'Code generated by Marcelo Boczko
' Date: 2010/01/06
' This code saves Webpage (.html file) and pictures. Pay attention to path of the files
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Sub CommandButton1_Click()
If TextBox2.Text <> "" Then
Call SaveWebPage(TextBox1, TextBox2) 'passing Path and Archive name
End If
End Sub
Private Sub SaveWebPage(sURLPath As String, sURLArchive As String)
'Saving text information (.html file)
Dim Scr_hDC As Long
Scr_hDC = GetDesktopWindow()
Label2.Caption = "Loading WebPage... ( " & sURLArchive & " )"
'WebBrowser1 is a WebBrowser control, enabled when some Tools/References are enabled:
' - Microsoft HTML Object Library
' - Microsoft Internet Controls
'Include WebBrower1 at your form
WebBrowser1.navigate (sURLPath & "/" & sURLArchive)
Dim e As IHTMLElement
Dim txtTemp As String
Dim iAux1 As Integer, iAux2 As Integer
Open ThisWorkbook.Path & "/" & sURLArchive For Output As #1
'Waiting till webpage loaded
Start = Now()
Do
DoEvents
Loop While WebBrowser1.readyState <> READYSTATE_COMPLETE
Label2.Caption = "Saving Text"
'Saving text
For Each e In WebBrowser1.document.All
If e.innerHTML <> "" Then 'Any condition to choose which part of the page to save
txtTemp = e.innerHTML
Print #1, txtTemp
Exit For
End If
Next
Close #1
'Saving images
Label2.Caption = "Saving Images"
Dim tmp_imageName As String
Dim HTML_img_element As MSHTML.HTMLImg
Dim strFolder As String, strImageName As String, iCounter As Integer
For Each HTML_img_element In WebBrowser1.document.images
'this following line is to discover name of the file, it may change according to the webpage structure
tmp_imageName = Mid(HTML_img_element.src, InStr(1, HTML_img_element.src, "/images/", vbTextCompare) + 8)
If InStr(1, txtTemp, tmp_imageName, vbTextCompare) Then
'Finding where starts the name of the picture
For iCounter = Len(tmp_imageName) To 1 Step -1
If InStr(iCounter, tmp_imageName, "/", vbTextCompare) Then
Exit For
End If
Next
strImageName = Mid(tmp_imageName, iCounter + 1)
If iCounter = 0 Then iCounter = 1
Label2.Caption = "Saving Images to /images/" & Left(tmp_imageName, iCounter - 1)
strFolder = Left(tmp_imageName, iCounter - 1)
'Path to save image
strFolder = ThisWorkbook.Path & "/images/" & strFolder
URLDownloadToFile 0, HTML_img_element.href, strFolder & "/" & strImageName, 0, 0
End If
Next
Label2.Caption = "idle ---> File " & sURLArchive & " sucessfuly saved."
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|