+ Reply to Thread
Results 1 to 25 of 25

Insert picture into cell, resize (keep aspect ratio) help

  1. #1
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Exclamation Insert picture into cell, resize (keep aspect ratio) help

    Hello all,

    First time poster so please be gentle
    I am trying to make an intelligent inspection report template which takes a lot of user error out of data entry. One of the features i am attempting is a photogrpahs page where the user can simply double click to add a photo into eight photo "frames" (merged cells).
    Despite having zero experience with VBA i have attempted to piece-meal the code together myself from internet sources but it never comes out right!

    Heres what i've got so far.
    Im using this code in the photographs spreadsheet "view code" section to trigger the photo insert script:

    Please Login or Register  to view this content.
    Then i have piece-mealed the photo insertion code together below:

    Please Login or Register  to view this content.
    The problem is the photo insert code doesnt work as i want it.
    I basically just need landscape and portrait photographs to be inserted into the merged cells of the first code but resize themselves to fit whilst maintaining aspect ratio

    Can anyone help please?

  2. #2
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    This is the outcome........
    Attached Images Attached Images
    Last edited by sadsack5000; 06-08-2017 at 09:02 PM.

  3. #3
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Insert picture into cell, resize (keep aspect ratio) help

    yucky merged cells....

    how is your merged cells setup in the 8 cells?
    if it based on whether the picture is landscape or portrait?
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  4. #4
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    See my screenshot above showing how the photo page of my report template looks and the outcome of the code

    I don't like merged cells either which is why I made the width of every report page 34 columns of 1.78 - this was to avoid merged cells on the front page but I found that a single cell for a photo (i.e. merged) looked more pleasing than lots of little ones

    I'd like for the user to be able to add any photo - portrait or landscape to the photo page and it fits them in suitably. This makes the photo page an easy one template rather than different templates for different orientated photos which could go on forever

  5. #5
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Insert picture into cell, resize (keep aspect ratio) help

    your settings was upside down
    try this

    Please Login or Register  to view this content.
    also your double click code could be shortened so its easier to read

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    Hmmmmm no theres something still not right.
    Now it seems the first two photo boxes fit in landscape photos but break the aspect ratio
    Meanwhile on all other photo boxes landscape photos overlap in width
    All photo boxes spit the portrait photos off to the side as shown....

    Broken Photo Code2.jpg
    Last edited by sadsack5000; 06-08-2017 at 09:27 PM.

  7. #7
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Insert picture into cell, resize (keep aspect ratio) help

    must be all the pictures i have to test with
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by humdingaling; 06-08-2017 at 09:50 PM. Reason: attached file

  8. #8
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    Ok that seems to have improved it but its still not right I'm afraid
    Now the first two photo boxes limit landscape photos on width but they overlap on height
    All other photo boxes take regular landscape photos well except for wide angle shots - those which are wider than a normal landscape photo it does not preserve aspect ratio (see the photos at the bottom of my screenshot below, they are wide angle. the one on the left is imported with code and the one on the right is imported and resized manually)
    As for portrait there is still a bug. In the first two boxes the photos seem to maintain aspect but are not resized to fit the frames
    All other photo boxes portrait photos are resized but aspect ratio is lost... plus they are not stretched to the height limits of the cell

    Really appreciate you helping me like this I'm sure you've got better things to do than help some Excel pleb

    Broken Photo Code3.jpg

  9. #9
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Insert picture into cell, resize (keep aspect ratio) help

    added another post check
    Please Login or Register  to view this content.
    i dont get the losing of locked ratio thing happening on my examples
    did you try it on the sample file i attached last post

    All other photo boxes take regular landscape photos well except for wide angle shots - those which are wider than a normal landscape photo it does not preserve aspect ratio (see the photos at the bottom of my screenshot below, they are wide angle. the one on the left is imported with code and the one on the right is imported and resized manually)
    when you say you resize manually is the ratio still locked?
    it looks to me you aren't keeping ratio in this case

  10. #10
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    My apologies I didn't see your sample xls in your last post. Have tried again now on your sample xls inserting the new code in your last post
    Landscape now appears perfect!!!
    For some strange reason in my file the photo frames on photos 5, 6, 7 and 8 are doing something weird with the height of landscape photos - that's why when I manually imported a photo and put it next to it the code imported one was not correct. I was keeping ratio by the way. I've now imported your spreadsheet into mine and binned my original photos page.

    As for portrait there's still an issue. Here's what it looks like using your sample and newest code:

    Broken Photo Code4.jpg

  11. #11
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Insert picture into cell, resize (keep aspect ratio) help

    must be missing something here
    do you know how to debug code?
    http://www.excel-easy.com/vba/examples/debugging.html

    can you run it thru step by step to see what it is doing to your pictures
    if the portrait pictures going to the IF Portrait part of the code?

  12. #12
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Insert picture into cell, resize (keep aspect ratio) help

    also try this

    Please Login or Register  to view this content.
    added in the two debug lines
    after the code is run...go into VBE and press CTRL +G
    paste what you get for when you run it with portrait picture

  13. #13
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    Ok I added the replacement code starting at With...
    Ran it and heres the debug it spits out:

    Please Login or Register  to view this content.
    Doesn't look a lot to me but I did follow your instructions correctly
    EDIT
    i ran it on two of those purple coloured portrait photos you've seen above

  14. #14
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    Hmmmmmm so I tried again using some other portrait photos. The first three I tried worked fine but the last one didn't

    Please Login or Register  to view this content.
    This time I've attached the photographs for your analysis. It doesn't seem to be restricted to a single photo as around 25% of those ive tried have the problem
    Attached Images Attached Images
    Last edited by sadsack5000; 06-09-2017 at 01:16 AM.

  15. #15
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    Ahhhh the last photo I attached above there was one of the oddball portrait images that doesn't fit correctly into the frame
    It looks as though the pic was taken as a landscape (horizontally) but windows 10 is automatically rotating them

    Do you think this is the case? I wonder how I can have my inspectors work around this when they use the report template

  16. #16
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Insert picture into cell, resize (keep aspect ratio) help

    good thing i didnt have windows 10

    the numbers i asked you to paste is basically the "height - width" before the code and after the resizing
    so
    on the last pic
    height = 580
    width = 807
    so it thinks its landscape

    does the picture insert the right way round? or is it rotating itself after the code is run?

  17. #17
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    The picture inserts portrait after the code, it also inserts portrait when I manually insert the same photo so I guess however the photo appears in windows explorer is how excel takes

    Any ideas on how to overcome this? Actually the PC's we are using in work run W7 which doesn't include the auto rotate "feature" but I hear my company will be upgrading to W10 so

    Really appreciate all the help you've given me on this

  18. #18
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Insert picture into cell, resize (keep aspect ratio) help

    now that's tricky as i dont have W10 to test any solutions i might want to test

    my first question is how does it know which ones are meant to be rotated and which aren't

    then my second one would be when you insert the picture at what point does it rotate?
    click into the picture...is rotation set to 0% if not then maybe we can just force rotation to 0% for everything

  19. #19
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    Yes I see your point about knowing whether something has to be rotated or not. I don't think there is a solution really but the cameras we have onsite are brand new... they look like an iPhone and most likely have a gyroscope in them to know which way they are being orientated. I would hope when an inspector takes a portrait photo with these cameras windows will treat them as "official" portrait photos rather than a landscape photo that needs rotating (and vice versa with ordinary landscape photos). In other words tere might not be any of these oddball portrait photos coming up

    I think i'll live with how it is now and do some trials with our digital cameras to see what sort of results come out in the report template
    Fantastic help, hope this also helps anyone else who is trying to build the same function into one of their spreadsheets

    All the best!

    (FYI rotation for oddball portrait photos is set to 90deg when I import through the code or manually, so a rotation is picked up)

  20. #20
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Insert picture into cell, resize (keep aspect ratio) help

    hmmm interesting
    i might be able to work with this

  21. #21
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Insert picture into cell, resize (keep aspect ratio) help

    it seems shaperange.rotation =90 would be the key to making it work

    without having w10 myself i cannot tweak the code
    let me consult other experts with w10 to see if i can enlist them to you tweak it

  22. #22
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    Just so you know I am seeing some files rotated at 270deg
    Reading up its all about how windows and other programs interprets the EXIF data contained within photographs.
    If we could somehow read whether the EXIF rotation data before we make our resizing that would be the winner

    Just googling around these things pop up:
    Someone who has written a load of VB code to read the EXIF data (looks crazy complicated): http://www.vb-helper.com/howto_net_r...ientation.html
    A little program I could use to rotate all photographs before import somehow: https://www.slimjet.com/jpeg-rotate/...g-pictures.php

    I logged onto my windows 7 PC and looked through all my photographs - there are a lot which are now auto rotated in W10 which obviously now ahs the capability to read EXIF and rotate direct in explorer
    Unfortunately I suspect even our brand new digital cameras onsite will only embed EXIF data and wont pre-rotate photos
    If this is the case I may have to resort to using manual means for importing photos into the reports

  23. #23
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Insert picture into cell, resize (keep aspect ratio) help

    seems like its a little rabbit hole there
    the EXIF code you posted looks promising and actually not that crazy however it looks like VB.net and not VBA so you cant use it in excel
    though i did some googling and it looks like some other people have vba version

    without being able to test any solution i give
    many may just be wild stabs in the dark

    i've put forward to other forum experts to see if any of them have work around for it

  24. #24
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    Ok thanks let me know what you hear

    I just checked on my W7 machine... running the code or importing manually (insert-pictures) rotates photographs to portrait. In other words it doesn't matter whether I run the report on a W7 or W10 OS.... excel has the ability to read EXIF data, identify pictures that were taken portrait and rotate on import
    We need code to take dimensions after the rotation and then do our resizing to them rather than the original in-rotated dimensions. The question is how...

    if anyone has experience here please chime in

  25. #25
    Registered User
    Join Date
    06-07-2017
    Location
    Perth
    MS-Off Ver
    2013
    Posts
    33

    Re: Insert picture into cell, resize (keep aspect ratio) help

    FYI I'm running excel 2013

+ 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. Excel VBA to insert & resize picture based on cell url
    By shabeersa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-14-2016, 03:54 AM
  2. Picture Aspect Ratio for Comments
    By jholiday78 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-18-2014, 06:01 PM
  3. Replies: 5
    Last Post: 09-11-2014, 02:30 PM
  4. Insert Images, scale to fit a cell size, maintain aspect ratio?
    By monet60707 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-27-2013, 02:02 PM
  5. Insert picture and auto rotate and resize to cell range dimensions
    By jwagman1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-01-2012, 03:49 PM
  6. Insert picture changes uses smaller aspect ratio
    By lakecityransom in forum Excel General
    Replies: 2
    Last Post: 11-01-2011, 06:18 PM
  7. Replies: 0
    Last Post: 02-12-2006, 02:15 AM

Tags for this Thread

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