Can I use VBA to import images (gifs) from the web into Excel?

前端 未结 2 1941
半阙折子戏
半阙折子戏 2020-12-18 10:55

I have a list of URLs to .gif files in an Excel sheet. I want to use VBA to query these websites and insert these images into the same worksheet. I am using Excel 2007.

相关标签:
2条回答
  • 2020-12-18 11:25

    First you'll want to download the image. There are several methods for doing this. I use the URLDownloadToFile API.

    Use the URLDownloadToFile API function to download a file from a URL into a file*:

    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
    

    (*visit link for full code)

    Then a code sample like this will import them onto the worksheet wherever you like:

    From Insert pictures using VBA in Microsoft Excel*:

    Sub TestInsertPicture()
        InsertPicture "C:\FolderName\PictureFileName.gif", _
            Range("D10"), True, True
    End Sub
    

    (*visit link for full code)

    To be fair to the original authors, I won't repost the code in its entirety here. Apologies for a mostly link-based answer.

    0 讨论(0)
  • 2020-12-18 11:31

    Yes, you can do so. It is actually quite easy. Excel has a AddPicture function. From Excel VBA Help:

    expression.AddPicture(Filename, LinkToFile, SaveWithDocument, Left, Top, Width, Height)

    Use like this:

    Sheets(1).Shapes.AddPicture "http://www.mywebsite.com/images/map.gif" _
                              , msoFalse, msoTrue, 100, 100, 500, 600
    

    Left, Top, Width, & Height are all required and in points.

    0 讨论(0)
提交回复
热议问题