VBA Excel 将图片添加到工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5797500/
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
VBA Excel Adding pictures to a worksheet
提问by Jesse Smothermon
I found a bit of code that helps insert a picture to a worksheet, but the user has to choose which picture is inserted. I was hoping that it would, instead, choose a specific picture (specifically the company logo) and insert it. Here's the code:
我发现了一些有助于将图片插入工作表的代码,但用户必须选择插入的图片。我希望它会选择一张特定的图片(特别是公司徽标)并插入它。这是代码:
PicLocation = Application.GetSaveAsFilename("C:\Work\test_Project\", "CA_Value_AVM, *.jpg", 1)
If PicLocation <> "" Then
ActiveSheet.Pictures.Insert(PicLocation).Select
Else
Exit Sub
End If
This code is executed when a button is clicked. The user clicks it and then the path "C:\Work\test_Project\" is opened and the user has to click the picture. I need this to change so that the user doesn't click anything.
此代码在单击按钮时执行。用户单击它,然后打开路径“C:\Work\test_Project\”,用户必须单击该图片。我需要对此进行更改,以便用户不会单击任何内容。
采纳答案by jonsca
It should work as:
它应该是这样工作的:
ActiveSheet.Pictures.Insert("C:\Work\test_Project\myjpgfile.jpg").Select
as that's all the function is returning anyway.
因为这就是函数返回的所有内容。
回答by jaysoncopes
I know this was asked 4 years ago, but I wanted to go ahead and update the answer here so that op and future searchers (such as myself) can have a more complete answer.
我知道这是 4 年前提出的问题,但我想继续在这里更新答案,以便操作员和未来的搜索者(例如我自己)可以获得更完整的答案。
jonsca's answer is appropriate and will work, but your picture will not work if the spreadsheet is moved.Essentially, ActiveSheet.Pictures.Insert
embedsan image, meaning that if the source image or the spreadsheet are moved, the link will break and the image will not show (not very good for company spreadsheets as OP originally asked about).
jonsca 的答案是合适的并且可以使用,但是如果移动电子表格,您的图片将无法使用。本质上,ActiveSheet.Pictures.Insert
嵌入图像,这意味着如果移动源图像或电子表格,链接将断开并且图像不会显示(对于 OP 最初询问的公司电子表格不太好)。
However, if you want a picture to work when moved/sent/etc. you can use:
但是,如果您希望图片在移动/发送/等时工作。您可以使用:
ActiveSheet.Shapes.AddPicture("C:\Work\test_Project\myjpgfile.jpg", False, True, 1, 1, 1, 1)
See this postfor a little more explanation on positioning and stuff.
有关定位和内容的更多解释,请参阅这篇文章。
(Thanks to PaulStock for the original post)
(感谢 PaulStock 的原始帖子)