Wednesday, January 5, 2022

Insert Picture From Web URL Excel VBA Macro

In this post we learn to insert pictures from the web in Excel using VBA macros. The macro inserts the picture in a given range and resizes the picture accordingly, so mind to select an appropriate range. Otherwise, specific position coordinates for the picture can be set instead. We can also decide whether to insert the source information or just make an independent copy of the picture. 

Learn all about Microsoft Excel and VBA macros in Excel Macro Class with plenty of Excel macro examples and VBA learning materials. Find useful Excel macros to automate your daily tasks and work in Microsoft Excel.

Macro code:

 
  Sub InsertPictureFromURL()
 
      Dim url As String, x As Integer, y As Integer, w As Integer, h As Integer
 
      url = "https://logodix.com/logo/701195.jpg"
 
      x = Selection.Left
      y = Selection.Top
      w = Selection.Width
      h = Selection.Height
 
      ActiveSheet.Shapes.AddPicture url, msoFalse, msoTrue, x, y, w, h
 
  End Sub
 

 

Macro explained:

  • First we declare a string variable to store the url, and four integers for the position coordinates of range where the picture is inserted.
  • The url must correspond to only the image location (usually with ending with the file and extension). It must be public and accessible, and should be shared for free use.
  • We assign the position coordinates of the selected range to the variables x, y, w, and h, that determine the left and top positions, and width and height of the range.
  • Then we add the picture with the AddPicture method of the Shapes object. It accepts the following parameters:
    • The FileName parameter or source url for the picture in this case
    • The LinkToFile parameter that can either be msoTrue to link the picture to its source location, or msoFalse to make an independent copy of the picture
    • The SaveWithDocument parameter that can either be msoTrue to save the linked picture with the document or msoFalse to store only the link information.
    • The four position coordinates that determine the area where the picture is inserted


This is how we insert pictures from the web in Excel using VBA macros.


Other examples:

 

No comments:

Post a Comment

Popular Posts