VBA to save an image from the web

S

Southern at Heart

How can I end this code so that it will save the image in 'My Documents'
instead of just pasting it into the word document? It is a .gif format on
the
web, but it can be something else if it's easier.
Thanks.
 
D

Doug Robbins - Word MVP

Probably ActiveWorkbook.SaveAs etc.

I assume that you are talking about Excel from the ActiveSheet

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
K

Karl E. Peterson

Southern said:
How can I end this code so that it will save the image in 'My Documents'
instead of just pasting it into the word document?

You really can't. You need to take an entirely different approach.
Well, "can't" and "need" are pretty strong. Let's say I'd *strongly*
recommend an entirely different approach. This is probably the
simplest method:

http://vbnet.mvps.org/code/internet/urldownloadtofilenocache.htm

If you need to display status, or otherwise have finer control, that
too can come with increasing complexity of the code.
 
S

Southern at Heart

....I don't know if that method will work, as the image I'm wanting to save is
not a 'real' image until IE loads it.
....so what I end up with is a .gif file in the clipboard. Is there a way to
save a .gif file that's on the clipboard to a file?
 
K

Karl E. Peterson

Southern said:
...I don't know if that method will work, as the image I'm wanting to save is
not a 'real' image until IE loads it.
...so what I end up with is a .gif file in the clipboard. Is there a way to
save a .gif file that's on the clipboard to a file?

Oh, ick! I really didn't look closely at the code you were using, or
the page you pointed to. I don't even see the GIF in the source for
that, either. I guess it's this, eh?

http://imgsrv.gocomics.com/dim/?fh=83fba46523c3b2f31aeb078a548010a8

Why do you say it's not a "real image" until IE loads it? Is the other
server creating it on demand? That API call will do the same thing,
essentially.

Anyway, yes, it is potentially possible to save a GIF from the
clipboard to file, but it's not a very simple process at all. I would
definitely try everything you can to find another route.
 
K

Karl E. Peterson

Southern said:
Yes, that's the link
Okay, I have looked at that method you mention earlier, but I can't quite
figure out how to make it work. I you could please help me make it work with
this url
http://imgsrv.gocomics.com/dim/?fh=83fba46523c3b2f31aeb078a548010a8
...then I think I could work out how to get the rest of my code to work with
that.
Sorry I'm not a "real" programmer, and I'm a bit blonde!

It's about as straight-forward as they come. Randy did a very good job
of isolating what's necessary. Add the following code to any standard
module in your project:

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 Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10

Public Function DownloadFile(SourceUrl As String, _
LocalFile As String) As Boolean
' Download the file. BINDF_GETNEWESTVERSION forces
' the API to download from the specified source.
' Passing 0& as dwReserved causes the locally-cached
' copy to be downloaded, if available. If the API
' returns ERROR_SUCCESS (0), DownloadFile returns True.
DownloadFile = URLDownloadToFile(0&, _
SourceUrl, _
LocalFile, _
BINDF_GETNEWESTVERSION, _
0&) = ERROR_SUCCESS
End Function

You can then test it with your URLs, something like this:

Public Sub Test()
Debug.Print DownloadFile( _

"http://imgsrv.gocomics.com/dim/?fh=83fba46523c3b2f31aeb078a548010a8",
_
Environ$("tmp") & "\test.gif")
End Sub

Here, I just stashed it in the temp folder. You may want to put it
somewhere else. The DownloadFile routine accepts a source and
destination string, giving you full control of that, and returns a
success or failure code.
 
S

Southern at Heart

Thanks! This looks like something I can handle! I think I CAN do it!
I'll try this tonight.
cheers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top