使用VBA将图像保存在excel中



我在工作中遇到过一种情况,人们必须手动在excel的某个页面中介绍图片,并手动调整其大小。作为一个完全的初学者,我设法找到了一些VBA代码,通过单击按钮并将其插入特定范围的单元格来帮助介绍图片。我遇到的问题是(在搜索了很多帖子后(,我不知道如何在没有链接的情况下正确引入保存图像的功能,这样其他人就可以看到报告,而不会得到图片不存在的错误。

你能帮我完成该功能应该介绍的地方吗?

Private Sub CommandButton3_Click()
Dim strFileName As String
Dim objPic As Picture
Dim rngDest As Range
strFileName = Application.GetOpenFilename( _
FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
Title:="Please select an image...")
If strFileName = "False" Then Exit Sub
Set rngDest = Me.Range("B24:C26")
Set objPic = Me.Pictures.Insert(strFileName)
With objPic
.ShapeRange.LockAspectRatio = msoFalse
.Left = rngDest.Left
.Top = rngDest.Top
.Width = rngDest.Width
.Height = rngDest.Height
End With
End Sub

提前感谢!

试试这个:

Private Sub CommandButton3_Click()
Dim strFileName As String
Dim objPic As Shape '<<<
Dim rngDest As Range
strFileName = Application.GetOpenFilename( _
FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
Title:="Please select an image...")
If strFileName = "False" Then Exit Sub
Set rngDest = Me.Range("B24:C26")
Set objPic = Me.Shapes.AddPicture(Filename:=strFileName, _
linktofile:=msoFalse, _
savewithdocument:=msoCTrue, _
Left:=rngDest.Left, Top:=rngDest.Top, _
Width:=rngDest.Width, Height:=rngDest.Height)
End Sub

最新更新