VBA Excel Adding pictures to a worksheet
I found a bit of code that helps insert a picture to a worksheet, but the user has to choose whic开发者_JAVA百科h 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.
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.
jonsca's answer is appropriate and will work, but your picture will not work if the spreadsheet is moved. Essentially, ActiveSheet.Pictures.Insert
embeds an 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).
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 post for a little more explanation on positioning and stuff.
(Thanks to PaulStock for the original post)
It should work as:
ActiveSheet.Pictures.Insert("C:\Work\test_Project\myjpgfile.jpg").Select
as that's all the function is returning anyway.
精彩评论