Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image in cell

Status
Not open for further replies.

ravi369

Technical User
Sep 10, 2018
4
SE
Hi,
Can we paste an image in a particular cell like text?

Thanks in advance.

Regards,
Ravi
 
Hi,

Excel is an object based application. A object inherits some of it’s properties from the parent object.

For instance the Workbook Object inherits from the Excel Application. The Worksheet inherits properties from the Workbook. The cell Range inherits properties from the Worksheet.

An Image would inherit properties from the Worksheet, just as a Range does.

So an Image Object cannot be assciated with a specific Range (cell) directly, but only by way of that specific Range’s position properties (Top, Left, Height, Width)
Code:
With Img.ShapeRange
   .Top = YourCell.Top
   .Left = YourCell.Left
   .Height = YourCell.Height
   .Width = YourCell.Width
End With
You may need to chsnge the LockAspectRatio property your image prior to positioning & sizing.

You might also check out FAQ707-4594

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Yes, we can. Sort of. What you can do is anchor it to the cell that the image's top left corner is over

In code, that would simply be something like
Code:
[blue]    Range("E2").Select
    ActiveSheet.Pictures.Insert("<pathtomyimage>").Select
    Selection.Placement = xlMove[/blue]

but this doesn't prevent it accidentally being moved by sloppy mouse clicking ... (although that can be prevented by Protecting the spreadsheet as by default the anchor is locked)


 
A different approach: add comment and fill its background with image. For hidden comments you will get hover effect - images will be visible when you move mouse over cell, the comment can be bigger than cell without limiting functionality.
Code:
Sub AddCommentWithImage()
With Range("A1").AddComment
    .Visible = False
    .Text Text:=""
    With .Shape
        .Fill.UserPicture "D:\x\test.jpg"
        .ScaleWidth 2.5, msoFalse
        .ScaleHeight 3.66, msoFalse
    End With
End With
End Sub

combo
 
Hi all,
Thanks for the reply.
Our requirement is to insert image in a cell and should stick to that particular cell. Which seems to be not possible.

/ravi
 
How about this?
Code:
'
    Dim oIMG As Object

    With ActiveSheet
        .Pictures.Insert("<pathtomyimage>").Select
        Set oIMG = .Shapes(.Shapes.Count)
        With .Cells(2, "E")      'your cell referenced here
            oIMG.Placement = xlMoveAndSize
            oIMG.LockAspectRatio = msoFalse
            oIMG.Height = .RowHeight
            oIMG.Width = .Width
            oIMG.Top = .Top
            oIMG.Left = .Left
        End With
    End With

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Hi @SkipVought,
it's not working. invalid outside procedure error.


/ravi
 
On what statement?

And please post your entire code, as I gave you merely a code snippet.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
You need to put Skip's code in a sub or function … however, it won't meet your "insert image in a cell and should stick to that particular cell" requirement, as Excel doesn't actually have the capability to put an image in a cell. It can put it over the cell, and anchor it to that cell, as previously advised (and Skip's code heightens the illusion by scaling the image to the size of the cell it is over)
 
Actually, the image/picture can initially be anywhere on the sheet. The position and size at the instant of creation is irrelevant.

It is sized to fit and positioned to fit virtually immediately. And the image’s Placement property is assigned to allow it to both move and size as the cell may move or change size.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Is that aimed at me or the OP?

If it is me, then not quite sure what point you are making. The only real initial difference between your example and mine is how we ensure the top left corner of the picture is over the cell we want it to anchor to - in my simple example this is the currently selected cell, in your case to a cell you nominate via code. In neither your code nor my code can we make the image actually stick with the cell, we can only give a pseedo-appearance of that - but it is quite easy to accidentally knock the image to a different cell (to which it will then appear to be anchored instead). We can get closer to the OPs requirements if we lock the image and then Protect the spreadsheet, but that causes some problems of its own.
 
Hi,
Thank you all for your replies.
We are trying to do something with OCR for text conversion from image.

/Ravi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top