Pasting Pictures into a Comment

Written by Allen Wyatt (last updated April 11, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


16

Andres has a picture in the Clipboard. He would like, for the selected cell, to place the picture into a comment. If the cell has no comment, then one would need to be created. If there is a comment already, then the picture would need to be added to it. Andres knows this would take a macro, but he's not sure how to work with comments in a macro to do what he needs.

Microsoft, in the latest Office 365 version, now refers to comments as notes. For the purposes of this tip, however, I'll continue to refer to them by the traditional comments name.

What Andres wants to do is nowhere near as easy as one might desire. The first problem is that there seems to be no way in VBA to use the Clipboard as the source of an image destined for a comment. It is possible to get around this by changing the source to be an image in a file—in other words, to have the macro allow the user to select an image file that is then placed in the comment.

Sub AddCommentPicture()
    Dim PicChoice As Variant

    If ActiveCell.Comment Is Nothing Then
        ActiveCell.AddComment
    End If
    PicChoice = Application.GetOpenFilename("JPEGs *.jpg,*.jpg")

    If PicChoice = False Then
        MsgBox "No file was selected."
    Else
        ActiveCell.Comment.Shape.Fill.UserPicture PicChoice
        ActiveCell.Comment.Shape.LockAspectRatio = True
    End If
End Sub

The macro tests the active cell to see if it has a comment. If not, it will add one. It then displays an Open dialog box that shows only JPG files. (You can change the GetOpenFilename function's parameter to indicate what types of files should be displayed.) The file you pick is then assigned to the comment.

Note that the code does nothing to resize the image. You can, if desired, add the code necessary to do the resizing. You'll want to add that code directly after the line that locks the aspect ratio of the image, near the end of the macro.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5489) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Displaying a Result as Minutes and Seconds

When you use a formula to come up with a result that you want displayed as a time, it can be tricky figuring out how to ...

Discover More

Fixing the Decimal Point

Don't want to always type the decimal point as you enter information in a worksheet? If you are entering information that ...

Discover More

Selecting to the Next Punctuation Mark

Writing macros often involves selecting different parts of your document so that some sort of processing can be ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Moving Comment Background Pictures to Cells

When formatting comments, you can use a graphic as a background for the comment box. If you later want to move this ...

Discover More

Copying Comments to Cells

Need to copy whatever is in a comment (a "note") into a cell on your worksheet? If you have lots of comments, manually ...

Discover More

Changing the Comment Font

When you add a comment to a worksheet, Excel uses a default font and size for the text. If you want to make changes to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is nine minus 5?

2021-12-17 18:04:10

J. Woolley

For more discussion, see https://www.thespreadsheetguru.com/the-code-vault/vba-insert-image-into-cell-comment


2021-12-10 10:03:12

J. Woolley

@Andrés
See https://sites.google.com/view/myexceltoolbox/Comment for continued discussion.


2021-12-09 12:05:45

Andrés

Hello Woolley,

I forgot to mention, the file is not blocked.

KR

Andrés


2021-12-09 12:00:12

Andrés

Hello Woolley,

Unfortunatelly, I'm not able to see the "new" ribbon after the add-on is installed.
The add-on is also active, but not present somehow. I'm using Office 365.

Any ideas?.

Thanks.

KR
A. López


2021-12-08 10:16:18

J. Woolley

For more on this subject, see https://excelribbon.tips.net/T013280_Placing_a_Picture_in_a_Comment.html

My Excel Toolbox includes the following function that will add a background image to a comment:
=ImageInComment(ImageFile,[Target],[ScaleFactor],[RotateAngle],[NoAuthor])
See https://sites.google.com/view/MyExcelToolbox/


2021-12-07 09:09:33

Andrés

Hello There,

Here is the code that I use, I'm not able to check if there is a picture in the clipboard yet. Maybe someone can help me. BTY, the code is not mine unfortunatelly, credits to the author:

Option Explicit
Sub PictureExport()
Dim TempChart As String, Picture2Export As String
Dim PicWidth As Long, PicHeight As Long
Dim ActSheet As String

ActSheet = ActiveSheet.Name

ActiveSheet.Paste
Selection.Name = "PrintScreen"
Picture2Export = Selection.Name


'Store the picture's height and width in a variable
With Selection
PicHeight = .ShapeRange.Height
PicWidth = .ShapeRange.Width
End With

'Add a temporary chart in sheet1
Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:=ActSheet
Selection.Border.LineStyle = 0
TempChart = Selection.Name & " " & Split(ActiveChart.Name, " ")(2)

With ActiveSheet
'Change the dimensions of the chart to suit your need
With .Shapes(TempChart)
.Width = PicWidth
.Height = PicHeight
End With

'Copy the picture
.Shapes(Picture2Export).Copy

'Paste the picture in the chart
With ActiveChart
.ChartArea.Select
.Paste
End With

'Finally export the chart
.ChartObjects(1).Chart.Export Filename:=ThisWorkbook.Path & "\tmp.jpg", FilterName:="jpg"
.Shapes(TempChart).Cut
End With

ActiveCell.AddComment
ActiveCell.Comment.Shape.Fill.UserPicture ThisWorkbook.Path & "\tmp.jpg"
ActiveCell.Comment.Shape.Width = PicWidth
ActiveCell.Comment.Shape.Height = PicHeight
ActiveSheet.Shapes("PrintScreen").Delete
Kill ThisWorkbook.Path & "\tmp.jpg"
Application.ScreenUpdating = True
End Sub


2021-09-11 07:30:23

Kurt Müller

@ J. Woolley

Or you could put this formula in a worksheet cell:

Does not work fine (comma):

=ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg",H7)

Works fine (semicolon):

=ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg";H7)


2021-09-11 06:38:14

Kurt Müller

@ J. Woolley

Thank you very much.

Works fine:

Sub Image_In_Comment()
ImageInComment "D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", Range("H7")
End Sub

Does not work fine (Compile error: Variable not defined)

Sub Image_In_Comment()
Result = ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", Range("H7")) '** Compile error: Variable not defined
End Sub

What am I doing wrong?

Works fine in the Immediate Window:

Result = ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", Range("H7"))


2021-09-06 10:06:55

J. Woolley

@Kurt Müller
As described in my earlier comments, ImageInComment is a Function, not a Sub, so it can be used in a cell formula. If you want to use a Function in VBA instead of a cell formula and ignore the returned result, your example with a Call statement will work. The Application.Evaluate shortcut [H7] is OK, but Range("H7") would be better. Here are two other ways to use the Function in VBA:
ImageInComment "D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", Range("H7")
result = ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", Range("H7"))
Or you could put this formula in a worksheet cell:
=ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg",H7)


2021-09-05 13:50:48

Kurt Müller

@ J. Woolley

I found the solution now:

Sub ImageInComment()
Call ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", [H7])
End Sub

I had to enter the above macro in a modul by myself.


2021-09-05 07:08:00

Kurt Müller

@ J. Woolley

I can find Register ImageInComment function (Private Sub ImageInComment_Register()), Image in Target's unthreaded Comment (Public Function ImageInComment(ImageFile, [Target}, [ScaleFactor], [RotateAngle], [NoAuthor])) but I can not find a "Sub ImageInComment".

What am I probably doing wrong?


2021-08-26 18:12:22

J. Woolley

For similar discussion, including the ImageInComment(FilePath,[Target]) function, see:
https://excelribbon.tips.net/T013280_Placing_a_Picture_in_a_Comment.html#comment-form-hd


2021-08-25 11:35:09

Kurt Müller

If a picture is pasted into a comment, the picture takes the size of the comment shape. After that the picture is already distorted.

If distortion of the picture shall be prevented, the following procedure is necessary:

- After choosing the pic it is necessary to calculate the scale of the picture
- With the scale of the picture the size of the comment shape has to be changed to the scale of the picture
- If the picture is pasted into the comment shape - having the same scale as the picture - then the picture is not distorted
- Just now the aspect ratio of the comment shape has to be locked. It is not possible to lock the aspect ratio of the picture pasted into the comment shape

Sub insert_userpicture_in_comments()

'** Dimensionierung der Variablen
Dim rngZelle As Range
Dim strFilename As Variant
Dim strFilter As String
Dim ScaleValue As Single
Dim ScaleValue2 As Single
Dim objPic As IPictureDisp
Dim Source As String
Dim i As Integer

If ActiveSheet.Comments.Count = 0 Then
MsgBox "No comments in entire sheet"
Exit Sub
End If

'Dateiauswahl filtern
strFilter = "JPG Files (*.jpg), *.jpg" _
& ", GIF Files (*.gif), *.gif" _
& ", Bitmaps (*.bmp), *.bmp" _
& ", WMF Files (*.wmf), *.wmf"

' Dialogfenster zur Auswahl eines Bildes öffnen
strFilename = Application.GetOpenFilename(strFilter)
DoEvents

' Wenn kein Bild ausgewählt wurde, Prozedur beenden
If strFilename = False Then GoTo LabelA

Source = (CStr(strFilename))
DoEvents

' Set objPic = LoadPicture(Bild)
Set objPic = LoadPicture(Source)
DoEvents

With objPic
ScaleValue = .Width / .Height
End With

If MsgBox(ScaleValue, vbOKCancel) = vbCancel Then GoTo LabelA

Application.CutCopyMode = False
DoEvents

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'** Alle markierten rngZellen durchlaufen
For Each rngZelle In Selection.Cells
With rngZelle
If Not .Comment Is Nothing Then
'Insert The Image and Resize
With .Comment.Shape
.LockAspectRatio = msoFalse
.Width = 150
' .Width = ScaleValue * .Height
.Height = .Width / ScaleValue
.Fill.UserPicture strFilename
DoEvents
.LockAspectRatio = msoTrue

ScaleValue2 = .Width / .Height
i = i + 1
Debug.Print i; rngZelle.Address; ScaleValue; ScaleValue2

End With
End If
End With
Next rngZelle

LabelA:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
On Error GoTo 0
End Sub


2020-04-12 13:40:37

J. Woolley

@Ronmio
For example, to make the height of the comment shape 100 pixels, change the code after Else as follows:

ActiveCell.Comment.Shape.Fill.UserPicture PicChoice
ActiveCell.Comment.Shape.LockAspectRatio = True
ActiveCell.Comment.Shape.Height = 100

When you use VBA to change the comment shape's height, its width will also change because aspect ratio is locked. In this case, the JPG image resizes to match the comment's shape because it was inserted as a background (Fill).

Notice you can manually change the comment shape's height or width independently by dragging a side-handle, but dragging a corner-handle will change both height and width to maintain aspect ratio.


2020-04-11 14:00:28

Ronmio

What might the VBA code that sizes the JPG look like?


2020-04-11 08:15:42

Cliff Raymond

It never occurred to me that an image could be pasted into a comment, or why I would want to, but I still found this fascinating. Thanks!


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.