+ Reply to Thread
Results 1 to 2 of 2

Resize and Center inserted image

  1. #1
    Registered User
    Join Date
    05-11-2018
    Location
    Nederland
    MS-Off Ver
    2016
    Posts
    2

    Resize and Center inserted image

    Hi There,

    i've managed to import an image from an url in my sheet with the following code:

    ' Code inserts picture from link within 1. with offset from 2.
    Sub InsertImage()
    Dim Pic As Picture
    Application.ScreenUpdating = False
    With ActiveSheet.Range("C1") '1
    Set Pic = .Parent.Pictures.Insert(.Value)
    With .Offset(, -2) '2.
    Pic.Top = .Top
    Pic.Left = .Left
    Pic.Height = 250
    End With
    End With
    Application.ScreenUpdating = True
    End Sub


    Works fine. But i need to resize the image with max height 250 and the ratio between height en wide need to be true. I searched the internet but can't find anything that works for me.
    Also the image must be centered in A1. (From left to right and from top to bottom.

    I'm very new to VBA so probally im doing something worng with Dim Pic as Picture (should be shape i guess?) Can someone please help me out, would be much appricieted.

  2. #2
    Registered User
    Join Date
    05-11-2018
    Location
    Nederland
    MS-Off Ver
    2016
    Posts
    2

    Re: Resize and Center inserted image

    Have updated my code:

    Sub InsertImageFullName()

    Application.ScreenUpdating = False

    Dim pic As String ' File path of a picture
    Dim cl As Range

    Set Rng = Range("A1") ' Defining input range

    For Each cl In Rng


    pic = cl.Offset(0, 2) ' Full path of the picture file:
    ' Located in the same row, third column from A, i.e. column C

    Set myPicture = ActiveSheet.Pictures.Insert(pic) ' Inserting picture from address in C column
    ' into column A
    With myPicture ' Setting picture properties
    .ShapeRange.LockAspectRatio = msoTrue ' Keep aspect ratio
    .Height = 250 ' Set your own size
    .Top = Rows(cl.Row).Top
    .Left = Columns(cl.Column).Left
    End With
    Next ' Looping to the Nth row, defined in:
    ' " Set Rng = Range("A13:A20") "

    Set myPicture = Nothing

    Application.ScreenUpdating = True

    End Sub

    After that i found an centering code:

    Sub aTest()

    CenterMe ActiveSheet.Shapes(1), Range("A1")

    End Sub

    Sub CenterMe(Shp As Shape, OverCells As Range)

    With OverCells
    Shp.Left = .Left + ((.Width - Shp.Width) / 2)
    Shp.Top = .Top + ((.Height - Shp.Height) / 2)
    End With
    End Sub

    Works when i run them. But how can i get them in one Macro?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 03-29-2021, 03:01 AM
  2. VBA to cut image file path from word bookmark and replace with inserted image
    By JayRay1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2018, 07:31 PM
  3. VBA to cut image file path from word bookmark and replace with inserted image
    By JayRay1 in forum Word Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2018, 12:02 AM
  4. VBA Code to resize sheets works fine except for inserted controls
    By PFDave in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-22-2016, 05:21 AM
  5. Replies: 1
    Last Post: 07-14-2016, 04:59 AM
  6. [SOLVED] How do I resize an inserted image in a cell?
    By tigerbenn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-17-2012, 10:55 AM
  7. Center Align Image
    By shapper in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-20-2008, 04:52 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1