我在excel中找到/写了一个宏,它允许我在单元格中插入一张图片,以便excel自动将图片调整到单元格的大小。
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
有时会插入并安装多个图片,但它们不可见。单击图片时,只有大小调整窗口/边框变得可见。它们在打印预览中也不可见。当我用纸上的PDF打印文档时,它们是可见的。
当我重新打开工作簿时,图片再次可见。是什么导致这个问题?怎么解决?在选项中,所有对象都是可见的。
答案 0 :(得分:0)
以下是使用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