Inserting an Online Picture to Excel with VBA

后端 未结 2 1082
庸人自扰
庸人自扰 2020-11-30 14:37

I\'m currently working on a project and need to fill in cells with pictures via URLs. All URLs are in one column, and I\'d like to load the images in an adjacent column. I\'

相关标签:
2条回答
  • 2020-11-30 15:02

    This is an almost identical solution that I posted about a month ago:

    Excel VBA Insert Images From Image Name in Column

    Sub InsertPic()
    Dim pic As String 'file path of pic
    Dim myPicture As Picture 'embedded pic
    Dim rng As Range 'range over which we will iterate
    Dim cl As Range 'iterator
    
    Set rng = Range("B1:B7")  '<~~ Modify this range as needed. Assumes image link URL in column A.
    For Each cl In rng
    pic = cl.Offset(0, -1)
    
        Set myPicture = ActiveSheet.Pictures.Insert(pic)
        '
        'you can play with this to manipulate the size & position of the picture.
        ' currently this shrinks the picture to fit inside the cell.
        With myPicture
            .ShapeRange.LockAspectRatio = msoFalse
            .Width = cl.Width
            .Height = cl.Height
            .Top = Rows(cl.Row).Top
            .Left = Columns(cl.Column).Left
        End With
        '
    
     Next
    
     End Sub
    
    0 讨论(0)
  • 2020-11-30 15:04

    I know this thread is 5 years old but just wanted to say it really helped me with a project. I'm using VBA to bring in data from an orders database. When I click on an order from those results it brings in more details about the orders including an image URL. The problem I had was that the code above was designed to add the image in place of the URL. I wanted to replace the image from a previous query with the image from the new query. After some tweaks I got it working but it was just laying a new image on top of the old image. In time my Excel file could get really big so here's my solution. The only problem I have right now is that it deletes my company's logo that I put on the sheet. There may be a way to be more selective, or I could just change the procedure to insert the logo from another sheet in the workbook every time it deletes pictures but that seems a bit cheesy.

    Sub InsertPic()
    
    Dim productImageUrl As String
    Dim productImage As Picture     'Declare image picture object
    Dim productImageUrlRng As Range 'Declare range object to contain image URL
    Dim productImageRng As Range    'Location image will be placed
    'Delete any existing pictures:
    
    
    Set productImageRng = ActiveSheet.Range("J1:J15") 'Where I want to put the image
    Set productImageUrlRng = Range("BA2")  'Cell containing image URL
    productImageUrl = productImageUrlRng
    
    productImageRng.Select
    'productImageRng.Delete --Does not delete pictures in range
    ActiveSheet.Pictures.Delete     'Delete existing images
    Set productImage = ActiveSheet.Pictures.Insert(productImageUrl)
    
    With productImage
        .ShapeRange.LockAspectRatio = msoTrue
        '.Width = productImageRng.Width
        .Height = productImageRng.Height
        ' .Top = Rows(cl.Row).Top
        ' .Left = Columns(cl.Column).Left
    End With
    End Sub
    
    0 讨论(0)
提交回复
热议问题