Can't save jpg to file

EasyEd

New Member
Joined
Oct 23, 2010
Messages
4
I have a jpg picture from the internet that I'm trying to save with VBA, using a file name that is in a cell. I'm using Excel 2003. I have:
Set hpic = ie.Document.getElementsByTagName("IMG")
hpic.Application.SaveAsFilename = "C:\pictures\" & a$

Set hpic - grabs the picture from my browser
a$ contains the file name for the saved file
I know that I actually have a picture object in hpic because I can put the jpg picture into the spreadsheet, but I can't seem to save it. When I try to save there is no visible error, the macro runs without any dialog boxes showing. I've tried several different commands.

Thanks in advance for any help with this.
 
Hello EasyEd,

Here is macro that will download a file from the web. The example code downloads the Google logo (png file) to the "C" drive.

Download Web File Macro
Code:
'Written: October 02, 2010
'Author:  Leith Ross (www.excelforum.com, www.thecodecage.com)

Private Declare Function URLDownloadToFile _
  Lib "urlmon.dll" _
    Alias "URLDownloadToFileA" _
      (ByVal pCaller As Long, _
       ByVal szURL As String, _
       ByVal szFileName As String, _
       ByVal dwReserved As Long, _
       ByVal lpfnCB As Long) As Long
  
Function DownloadFilefromWeb(ByVal URL As String, ByVal SavePath As String, ByVal NewFileName As String) As Variant

  Const E_OUTOFMEMORY As Long = &H8007000E
  Const E_DOWNLOAD_FAILURE As Long = &H800C0002
  Const E_INVALID_LINK As Long = &H800C000D
  
  Dim InitialName As String
  Dim Msg As String
  Dim RegExp As Object
  Dim RetVal As Long
  
    If URL = "" Or SavePath = "" Then Exit Function
    
    If NewFileName = "" Then
       NewFileName = InputBox("Please enter a name for the file and it's extension.")
       If NewFileName = "" Then Exit Function
    End If
    
    Set RegExp = CreateObject("VBScript.RegExp")
      RegExp.IgnoreCase = True
      RegExp.Pattern = "^(.*\/)(.+)$"
      InitialName = RegExp.Replace(URL, "$2")
    Set RegExp = Nothing
      
    If InitialName = "" Or InitialName = URL Then
      MsgBox "Error - Missing File Name"
      Exit Function
    End If
    
    RetVal = URLDownloadToFile(0&, URL, SavePath & NewFileName, 0&, 0&)

    Select Case RetVal
      Case 0
        DownloadFilefromWeb = True
      Case E_OUTOFMEMORY
        DownloadFilefromWeb = URL & vbCrLf & "Error - Out of Memory"
      Case E_DOWNLOAD_FAILURE
        DownloadFilefromWeb = URL & vbCrLf & "Error - Bad URL or Connection Interrupted"
      Case E_INVALID_LINK
        DownloadFilefromWeb = URL & vbCrLf & "Error - Invalid Link or Protocol Not Supported"
      Case Else
        DownloadFilefromWeb = URL & vbCrLf & "Error - Unknown = " & Hex(RetVal)
    End Select
    
End Function
Download File Example Macro
Code:
Sub DownloadTestA()

  picURL = "http://www.google.com/images/logos/ps_logo2.png"
  SavePath = "C:\"
  SaveName = "Google Logo.png"
  
  DownloadFilefromWeb picURL, SavePath, SaveName
  
End Sub
Sincerely,
Leith Ross
 
Upvote 0

Forum statistics

Threads
1,226,848
Messages
6,193,318
Members
453,790
Latest member
yassinosnoo1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top