Using the WebBrowser control, I want to save all the images existing in a web page in a folder (not in the Temporary Internet Files folder). This is how I am doing it:
VB Code:
  1. Private Sub wWeb_DownloadComplete()
  2.     On Error Resume Next
  3.     Dim x As Integer
  4.     Dim i As Integer
  5.     Dim iFile As Integer
  6.     Dim filename As String
  7.    
  8.     iFile = FreeFile
  9.     If (wWeb.Document.All.tags("img").Length > 0) Then
  10.         For x = 0 To wWeb.Document.All.tags("img").Length
  11.             [color=red]filename = App.Path & "\" & Right(wWeb.Document.All.tags("img")(x).src, Len(wWeb.Document.All.tags("img")(x).src) - InStrRev(wWeb.Document.All.tags("img")(x).src, "/"))[/color]
  12.             Open filename For Output As #iFile
  13.             Write #iFile, wWeb.Document.All.tags("img")(x).src
  14.             Close #iFile
  15.         Next
  16.     End If
  17. End Sub
When I execute the above code, all the images existing in a web page does get saved in the specified folder but when I open the images, none of the images can be seen (just a blank screen).

Assume that one of the images is named MyImage.gif.

When I compared this image with its counterpart in the Temporary Internet Files folder, I found that there is considerable difference in the sizes of the 2 images. Like for e.g. the size of MyImage.gif which was saved in the folder I specified turned out to be 67 bytes where as the size of the same file in the Temporary Internet Files folder turned out to be 3KB!

Note the On Error Resume Next line in the DownloadComplete event function. If this line is commented, then VB throws the Object variable or With block variable not set error pointing to the line which is red colored in the above-mentioned code.

Now why is this error coming up & what changes are needed in the above-mentioned code to save the images in the specified folder successfully?

Please note that the string functions Len, Right & InStrRev have been used since wWeb.Document.All.tags("img")(x).src returns the entire path of the image (for e.g. http://www.abcd.com/myfolder/MyImage.gif) which would throw the Bad file name or number error. The string functions get rid of the entire path & return only the name of the image along with its extension (for e.g. MyImage.gif).

Thanks,

Arpan