+ Reply to Thread
Results 1 to 22 of 22

Get The Dimensions of an Imported Image

Hybrid View

  1. #1
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Get The Dimensions of an Imported Image

    I was googling for a VBA code that allows me to get the dimension in "cm" of the imported image in the an Excel sheet. So the process I import the image and once imported it registers the dimensions Width & Height in a certain cell unit = cm. And if I move the image, the dimensions follow the image moving cells.

    I found below links:
    https://stackoverflow.com/questions/...age-dimensions
    https://social.msdn.microsoft.com/Fo...e?forum=isvvba

    Those links didn't work with me for the imported image in Excel plus it get info of a static file name while it should be variable.

    Any help please.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Get The Dimensions of an Imported Image

    Hello webiscore,

    Define "import" as it applies to this question. I can think of several definitions that could apply in this case.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Re: Get The Dimensions of an Imported Image

    Thank you for your questions, I meant insert instead of import. The insert picture in excel at the INSERT Tab.

    The process is as following:
    1- We'll insert images in Excel, now the images are saved and embedded within the excel thus the link and where it comes from isn't important. As we can delete the image folder but the image remains working should be.
    2- After the insert automatically for example the dimensions are inserted in cells like in A10 = 120cm Width B10 = 120cm Height.
    3- And if we move the image let's say from cells A-B-C to F-G-H the dimensions will move from A10 and B10 to F10 and G10


    Hope this clarifies everything

  4. #4
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Re: Get The Dimensions of an Imported Image

    The link below for the file that includes the pictures I'm referring to, what I'm trying is to retrieve the dimensions of those images in cells of the excel worksheet in CM. Any help please?

    https://files.fm/u/t8fpwczb

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Get The Dimensions of an Imported Image

    The width in cm is myPic.Width / 72 * 2.54
    Last edited by shg; 07-06-2018 at 08:10 PM.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Re: Get The Dimensions of an Imported Image

    Why "72 * 2.54" Does that refer as static measure. Please not images dimensions might differ from one another. I want to print the values into cells. Please advice me with a complete code, thank you!

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,500

    Re: Get The Dimensions of an Imported Image

    Remove the Calculate-event the try this one.
    Private Sub Worksheet_Change(ByVal Target As Range)
        Const numpix = 37.7952755905511 'pixels per cm
        If Target.Address = "$P$3" Then
            Dim mypic As Picture
            Me.Pictures.Visible = False
    
            With Range("e2")
                For Each mypic In Me.Pictures
                    If mypic.Name = .Text Then
                        mypic.Visible = True
                        mypic.Top = .Top
                        mypic.Left = .Left
                        Exit For
                    End If
                Next mypic
            End With
            Dim wpicture As String, s As Double, y As Double
            With Sheet2
                wpicture = .Cells(Application.Match(Target.Value, .Columns(1), 0), 2)
            End With
            With Sheet1
                s = Round(.Shapes(wpicture).Height / numpix, 2)
                y = Round(.Shapes(wpicture).Width / numpix, 2)
            End With
        
            MsgBox "Picture dimensions are " & vbLf & vbLf & _
                "Height: " & s & " cm" & vbLf & vbLf & _
                "Width: " & y & " cm"
        End If
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  8. #8
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Re: Get The Dimensions of an Imported Image

    Quote Originally Posted by bakerman2 View Post
    Remove the Calculate-event the try this one.
    Private Sub Worksheet_Change(ByVal Target As Range)
        Const numpix = 37.7952755905511 'pixels per cm
        If Target.Address = "$P$3" Then
            Dim mypic As Picture
            Me.Pictures.Visible = False
    
            With Range("e2")
                For Each mypic In Me.Pictures
                    If mypic.Name = .Text Then
                        mypic.Visible = True
                        mypic.Top = .Top
                        mypic.Left = .Left
                        Exit For
                    End If
                Next mypic
            End With
            Dim wpicture As String, s As Double, y As Double
            With Sheet2
                wpicture = .Cells(Application.Match(Target.Value, .Columns(1), 0), 2)
            End With
            With Sheet1
                s = Round(.Shapes(wpicture).Height / numpix, 2)
                y = Round(.Shapes(wpicture).Width / numpix, 2)
            End With
        
            MsgBox "Picture dimensions are " & vbLf & vbLf & _
                "Height: " & s & " cm" & vbLf & vbLf & _
                "Width: " & y & " cm"
        End If
    End Sub
    Thank you I think it's working now but 2 things:

    1- I need to print the values in cells rather a message box
    2- When I compare the message box for example it says
    Picture dimensions are:
    Height: 8.99 cm
    Width: 12.74cm

    When double clicking on this pic and go to the format section /picture tools it's giving me height 11.99 cm and width 16.99cm. There are big difference why?

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Get The Dimensions of an Imported Image


    Hi !

    Have a look to CentimetersToPoints method …
    Last edited by Marc L; 07-07-2018 at 08:10 AM. Reason: typo …

  10. #10
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Re: Get The Dimensions of an Imported Image

    Quote Originally Posted by Marc L View Post

    Hi !

    Have look to CentimetersToPoints method …
    Can you elaborate please

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Get The Dimensions of an Imported Image

    Quote Originally Posted by webiscore View Post
    Can you elaborate please
    To see in VBA inner help …

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,500

    Re: Get The Dimensions of an Imported Image

    Try this way then.
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim mypic As Picture
        Dim wpicture As String, s As Double, y As Double
        
        If Target.Address = "$P$3" Then
            Me.Pictures.Visible = False
            With Range("e2")
                For Each mypic In Me.Pictures
                    If mypic.Name = .Text Then
                        mypic.Visible = True
                        mypic.Top = .Top
                        mypic.Left = .Left
                        Exit For
                    End If
                Next mypic
            End With
            
            With Sheet2
                wpicture = .Cells(Application.Match(Target.Value, .Columns(1), 0), 2)
            End With
            
            With Sheet1
                s = Round(.Shapes(wpicture).Height / 72 * 2.54, 2)
                y = Round(.Shapes(wpicture).Width / 72 * 2.54, 2)
                
            End With
        
            Range("P5") = s & "cm x " & y & "cm"
        End If
    End Sub

  13. #13
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Re: Get The Dimensions of an Imported Image

    Private Sub Worksheet_Change(ByVal Target As Range)
        Const numpix = 37.7952755905511 'pixels per cm
    
            Dim mypic As Picture
            Me.Pictures.Visible = False
    
            With Range("e2")
                For Each mypic In Me.Pictures
                    If mypic.Name = .Text Then
                        mypic.Visible = True
                        mypic.Top = .Top
                        mypic.Left = .Left
                        Exit For
                    End If
                Next mypic
            End With
            Dim wpicture As String, s As Double, y As Double
            With Sheet2
                wpicture = .Cells(Application.Match(Target.Value, .Columns(1), 0), 2)
            End With
            With Sheet1
                s = Round(.Shapes(wpicture).Height / 72 * 2.54, 2)
                y = Round(.Shapes(wpicture).Width / 72 * 2.54, 2)
            End With
        
            MsgBox "Picture dimensions are " & vbLf & vbLf & _
                "Height: " & s & " cm" & vbLf & vbLf & _
                "Width: " & y & " cm"
                
                
            Range("Q5") = s & "cm"
            Range("Q6") = y & "cm"  /// BTW IT's not Printing Width Only the Height
    End Sub

    I'm trying the following code and thanks a lot to you now I'm getting the accurate size but it's keep giving me
    Run-time error '13":
    Type mismatch

    wpicture = .Cells(Application.Match(Target.Value, .Columns(1), 0), 2)

    I checked the cells are correct and there's no mismatch but why?

    Range("Q6") = y & "cm" /// BTW IT's not Printing Width Only the Height

  14. #14
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,500

    Re: Get The Dimensions of an Imported Image

    This works just fine for me.
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim mypic As Picture
    
        If Target.Address = "$P$3" Then
            Me.Pictures.Visible = False
            With Range("e2")
                For Each mypic In Me.Pictures
                    If mypic.Name = .Text Then
                        mypic.Visible = True
                        mypic.Top = .Top
                        mypic.Left = .Left
                        Exit For
                    End If
                Next mypic
            End With
    
            With ActiveSheet
                .Range("Q5") = Round(.Shapes(.Range("e2").Value).Height / 72 * 2.54, 2) & "cm"
                .Range("Q6") = Round(.Shapes(.Range("e2").Value).Width / 72 * 2.54, 2) & "cm"
            End With
        End If
    End Sub

  15. #15
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Re: Get The Dimensions of an Imported Image

    Thanks man I appreciate and really sorry.

    But why when I use If Target.Address = "$P$3" Then it doesn't load the images and dimensions and once I comment it I get the info needed but the excel keeps looping and stops responding.

  16. #16
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,500

    Re: Get The Dimensions of an Imported Image

    Did you put the code in the worksheetmodule of Sheet1 ?

    Replace P3 with the Range your Datavalidation is in.

    Can't upload your file because it's too big.

  17. #17
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Re: Get The Dimensions of an Imported Image

    Quote Originally Posted by bakerman2 View Post
    Did you put the code in the worksheetmodule of Sheet1 ?

    Replace P3 with the Range your Datavalidation is in.

    Can't upload your file because it's too big.
    This is the code and now working fine thanks.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim mypic As Picture
    
       If Target.Address = "$A$4" Then
            Me.Pictures.Visible = False
            With Range("e2")
                For Each mypic In Me.Pictures
                    If mypic.Name = .Text Then
                        mypic.Visible = True
                        mypic.Top = .Top
                        mypic.Left = .Left
                        Exit For
                    End If
                Next mypic
            End With
    
            With ActiveSheet
                s = Round(.Shapes(.Range("e2").Value).Height / 72 * 2.54, 2) & "cm"
                y = Round(.Shapes(.Range("e2").Value).Width / 72 * 2.54, 2) & "cm"
            
            
            MsgBox "Picture dimensions are " & vbLf & vbLf & _
                "Height: " & s & vbLf & vbLf & _
                "Width: " & y
                
                .Range("Q5") = s
                .Range("Q6") = y
            
            End With
        End If
    End Sub
    Please can you advise me how to auto update the values if I'm resizing the image without going back and forth or closing the excel sheet.

    Thank you so much!

  18. #18
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Re: Get The Dimensions of an Imported Image

    Please use https://files.fm

    Didn't get your point

  19. #19
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Re: Get The Dimensions of an Imported Image

    Thank you I have managed that but now i'm trying to autoupdate the values if the image was resized

  20. #20
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,500

    Re: Get The Dimensions of an Imported Image

    A shape only knows the click event so resizing a shape won't activate any code.

    The only workaround I can think of is putting an Active-X label on the worksheet so you can use the mouseover- event to recalculate the dimensions.

    You could place that label in a convenient place, remove the caption and set backstyle to transparent so it's invisible but present.

  21. #21
    Registered User
    Join Date
    05-10-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    23

    Re: Get The Dimensions of an Imported Image

    Quote Originally Posted by bakerman2 View Post
    A shape only knows the click event so resizing a shape won't activate any code.

    The only workaround I can think of is putting an Active-X label on the worksheet so you can use the mouseover- event to recalculate the dimensions.

    You could place that label in a convenient place, remove the caption and set backstyle to transparent so it's invisible but present.
    Thanks a lot for the information and so much appreciated for your support. Can you please elaborate and show me an example i'm a newbie

  22. #22
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,500

    Re: Get The Dimensions of an Imported Image

    An example file won't tell you much so I'll try to guide you through it.

    1. Go to Developers Tab and select Insert.
    2. Click on ActiveX-label and draw the shape on your worksheet.
    3. Right-Click on the label to show properties.
    4. Remove the text next to Caption and close Properties.
    5. Double-Click on the label to open VBA-Editor.
    6. In the right window you'll see 2 combobox like objects. The left is showing Label1 and the right is showing Change.
    7. In the right Combobobox select MouseOver.
    8. Put this between Private Sub and End Sub
        With ActiveSheet
            .Range("Q5") = Round(.Shapes(.Range("e2").Value).Height / 72 * 2.54, 2) & "cm"
            .Range("Q6") = Round(.Shapes(.Range("e2").Value).Width / 72 * 2.54, 2) & "cm"
        End With
    so it looks like this
    Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        With ActiveSheet
            .Range("Q5") = Round(.Shapes(.Range("e2").Value).Height / 72 * 2.54, 2) & "cm"
            .Range("Q6") = Round(.Shapes(.Range("e2").Value).Width / 72 * 2.54, 2) & "cm"
        End With
    End Sub
    9. Close the VBEditor and move and resize the label to the place of your choice.
    10. On Developer tab goto DesignMode and reopen Properties.
    11. For BackStyle select BackStyleTransparent.
    12. Close Properties, Leave DesignMode.
    13. Now resize the picture and hoover over the 'invisible label' to check dimensions.

+ 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. Get image dimensions from online image
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-30-2015, 10:52 AM
  2. VBA save image from webpage, change dimensions
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-12-2015, 08:05 PM
  3. [SOLVED] Macro to copy the image attributes eg. name, size, type, dimensions from folder containing
    By busybee235 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-31-2014, 11:32 AM
  4. Resize the image according to the cell dimensions.
    By yeshwant_sur in forum Excel General
    Replies: 1
    Last Post: 02-28-2013, 03:05 AM
  5. [SOLVED] Macro to return JPG image from URL (2 URL's in an IF statement determined by dimensions
    By Rob Broggi in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-27-2013, 04:00 PM
  6. Adding Text over Imported Image
    By rinwiggrein in forum Excel General
    Replies: 1
    Last Post: 01-08-2013, 01:24 PM
  7. Excel limitation on number of imported image objects?
    By iterature in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-03-2007, 01:30 PM

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