Reputation: 13
I've found/written a macro in excel which allows me to insert a picture in a cell so that excel automatically fits the picture to the size of the cell.
Sub InsertAndSizePhoto()
Dim sFileName As String
Dim oShape As Shape
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
Dim myPath As String
Dim folderPath As String
folderPath = Application.ActiveWorkbook.Path
sFileName = Application.GetOpenFilename( _
FileFilter:="Images (*.gif;*.jpg;*.png), *.gif;*.jpg;*.png", _
FilterIndex:=1, _
Title:="Insert Picture", _
ButtonText:="Insert", _
MultiSelect:=False)
If sFileName = "False" Then Exit Sub
With ActiveCell.MergeArea
ActiveSheet.Shapes.AddPicture _
Filename:=sFileName, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=.Left, _
Top:=.Top, _
Width:=.Width, _
Height:=.Height
End With
End Sub
Sometimes multiples pictures are inserted and fitted but they are not visible. Only the sizing window/border becomes visible when I click on the picture. They are also not visible in print preview. When I print the document in PDF of on paper they are visible.
When I reopen the workbook the pictures are visible again. What causes this issue? How can it be resolved? In options all objects are visible.
Upvotes: 1
Views: 319
Reputation: 916
Here's a sample code for using Pictures.Insert:
With ActiveSheet.Pictures.Insert(Filename:=sFileName, LinkToFile:=False, SaveWithDocument:=True)
.Placement = xlMoveAndSize
With .ShapeRange
.Height = ActiveCell.MergeArea.Height
.Width = ActiveCell.MergeArea.Width
.Left = ActiveCell.MergeArea.Left
.Top = ActiveCell.MergeArea.Top
End With
End With
Upvotes: 0