Solved

Resizing the comments box to fit the embedded image

Posted on 2011-02-15
10
194 Views
Last Modified: 2012-05-11
Hi all

I have been making a spreadsheet to collect images from a location on the computer and input them as a image in a comments box. Im looking for a way to lock the aspect ratio for the picture within the macro, and have the image be a certain size. This is the code I have been using (which someone on here previous helped me with):

Sub insert_pic_in_comment()
Range("D14").AddComment
    Range("D14").Comment.Visible = True
    Range("D14").Comment.Text Text:="" & Chr(10) & ""
    Range("D14").Comment.Shape.Fill.UserPicture _
        "C:\Patient\FFS.jpg"
    Range("D14").Comment.Shape.Select True
    Selection.ShapeRange.ScaleWidth 5#, msoFalse, msoScaleFromTopLeft
    Selection.ShapeRange.ScaleHeight 6#, msoFalse, msoScaleFromTopLeft
    Selection.ShapeRange.IncrementTop -125#
End Sub

This works fine as long as the image ratio is 4x6, but when someone needs to use a portrait picture, it gets stretched.

Any and all solutions would be fantastic

Cheers

Ian
0
Comment
Question by:Rogit85
  • 4
  • 4
10 Comments
 
LVL 45

Expert Comment

by:patrickab
ID: 34898285
Try:

Sub insert_pic_in_comment()
    Range("D14").AddComment
    Range("D14").Comment.Visible = True
    Range("D14").Comment.Text Text:="" & Chr(10) & ""
    Range("D14").Comment.Shape.Fill.UserPicture _
        "C:\Patient\FFS.jpg"
    Range("D14").Comment.Shape.Select True
    Selection.ShapeRange.Width 'try a number here
    Selection.ShapeRange.Height 'try a number here
End Sub

Patrick
0
 
LVL 45

Expert Comment

by:patrickab
ID: 34898485
This woks on my machine:

Sub insert_pic_in_comment()
    Range("A1").AddComment
    Range("A1").Comment.Visible = True
    Range("A1").Comment.Text Text:="Sunset" & Chr(10) & ""
    Range("A1").Comment.Shape.Fill.UserPicture _
        "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg"
    Range("A1").Comment.Shape.Width = 200 'try a number here
    Range("A1").Comment.Shape.Height = 120 'try a number here
End Sub

Patrick
0
 

Author Comment

by:Rogit85
ID: 34900446
Hi Patrickab

Thank you for the tips!

I need a way for it to adjust depending on the picture used. It needs to automatically change depending if its a portrait or landscape. The people who will be using this spreadsheet have no VB knowledge and the picture's cant be distorted in any way.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 45

Expert Comment

by:patrickab
ID: 34900540
Try this instead:

Sub insert_pic_in_comment()
    Range("A1").AddComment
    Range("A1").Comment.Visible = True
    Range("A1").Comment.Text Text:="Sunset" & Chr(10) & ""
    Range("A1").Comment.Shape.Fill.UserPicture _
        "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg"
     Range("A1").Comment.Shape.TextFrame.AutoSize = True
End Sub

Patrick
0
 

Author Comment

by:Rogit85
ID: 34901472
That only wraps it to the text inside the comment, and I wont be using text in mine
0
 
LVL 45

Expert Comment

by:patrickab
ID: 34901867
>That only wraps it to the text inside the comment, and I wont be using text in mine

As pictures do not have a predetermined size there is a problem making anything fit an indeterminately sized object. In other words I don't believe you can do what you want.

Patrick
0
 

Author Comment

by:Rogit85
ID: 34904413
Damn

Well thank you very much for your help, its a shame that it cant be done
0
 

Accepted Solution

by:
Rogit85 earned 0 total points
ID: 34906119
Hi Patricklab

I managed to solve this myself in the end, It opens the image in the spreadsheet, measures it, deletes it then adds it back as a comment image :)

Dim rngTarget As Range
Dim lHeight As Long, lWidth As Long

ActiveSheet.Pictures.Insert("C:\Patient\mao.jpg").Select
With Selection
    lHeight = .Height
    lWidth = .Width
    .Delete
End With
Set rngTarget = ActiveSheet.Range("C7")
With rngTarget
    .AddComment
With .Comment.Shape
    .Width = lWidth
    .Height = lHeight
    .IncrementTop -90#
    .Fill.UserPicture ("C:\Patient\mao.jpg")
End With
End With
    Range("C7").Comment.Visible = False
0
 
LVL 24

Expert Comment

by:broomee9
ID: 35225317
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Standard Deviation Excel 13 28
Excel formula needed 7 19
VBA Help 18 42
locking multiple column ranges 10 22
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now