在Excel表格中插入图片

在我的Office 2010 xlsm工作表中,我创build了一个名为Image1的固定大小的图像ActiveX控件。 在我看来,下面的macros应该设置图像path到单元格B3中指定的图像path,但是它会失败,并显示“运行时错误”424“:对象需要”在Image1.Picture行的错误信息。 我检查了imPath等于"C:\some\valid\image\path.jpg"

 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$B$3" Then imPath = Range("B3") Image1.Visible = True Image1.Picture = imPath Image1.PictureSizeMode = fmPictureSizeModeZoom Image1.BorderStyle = fmBorderStyleNone Image1.BackStyle = fmBackStyleTransparent End If End Sub 

这里有什么问题? 顺便说一句,有没有什么办法来检查指定的文件是否真的存在?

快速查看excel的帮助,寻找图片,build议您使用这一行:

 Image1.Picture = LoadPicture(imPath) 

要检查文件是否存在,请使用FileSystemObject

 Dim fs as variant Set fs = CreateObject("Scripting.FileSystemObject") fs.FileExists(imPath) 

编辑

顺便说一下,你可以使用

 imPath = Target.value 

设置imPath(未testing,但相当肯定这一点)