使用 VBA 将图片插入(然后引用)到 Excel 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25400943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert (and then reference) a picture into Excel using VBA
提问by Jonny
I'm trying to insert a picture into a worksheet using VBA, that I can later reference by name (to for example delete, or hide).
我正在尝试使用 VBA 将图片插入到工作表中,稍后我可以按名称引用(例如删除或隐藏)。
I can insert the image using code similar to the below:
我可以使用类似于下面的代码插入图像:
ActiveSheet.Shapes.AddPicture Filename:="image.jpg", linktofile:=msoFalse, _
savewithdocument:=msoCTrue, Left:=10, Top:=20, width:=100, Height:=50
However, I'm struggling to assign the picture to a shape (or other object) so that I can name it and later refer to it. e.g.
但是,我正在努力将图片分配给一个形状(或其他对象),以便我可以命名并稍后引用它。例如
Dim shp As Shape
set shp = ActiveSheet.Shapes.AddPicture Filename:="image.jpg", linktofile:=msoFalse, _
savewithdocument:=msoCTrue, Left:=10, Top:=20, width:=100, Height:=50
shp.name = "myPicture"
... some code ...
ActiveSheet.Shapes("myPicture").Delete ' or similar code to later delete the image
However, I'm getting a syntax error on the set shp = ...
line
不过,我对得到一个语法错误set shp = ...
行
I've also tried with: Dim shp As Excel.Shape
, Dim shp As Object
as per the comments in SO post: VBA to insert embeded picture excelbut still get the syntax errors.
我也试过:Dim shp As Excel.Shape
,Dim shp As Object
根据 SO post: VBA to insert embeded picture excel 中的评论,但仍然出现语法错误。
Where am I going wrong?
我哪里错了?
回答by Rory
You need parentheses when returning a value from a method:
从方法返回值时需要括号:
set shp = ActiveSheet.Shapes.AddPicture(Filename:="image.jpg", linktofile:=msoFalse, _
savewithdocument:=msoCTrue, Left:=10, Top:=20, width:=100, Height:=50)