Sophie Altair
Sophie Altair

Reputation: 263

Is there a way to store an image in an .xlam file?

I'm currently working on a userform-heavy Excel Add-In which uses an image as a UI element within the userform. I am currently storing the image binary on a network share, but I'd like to have the addin be self contained.

I know I can temporarily disable IsAddin and paste the image directly into a worksheet, but from there I'm not sure how to pull the image out of the worksheet and into the picture attribute of the image object in my form.

Is there any way to store this image in the .xlam itself and then reference it within VBA space?

Thanks in advance!

Upvotes: 0

Views: 429

Answers (1)

Darrell H
Darrell H

Reputation: 1886

Here's what I did. I put the picture file in a UserForm and then called the image from the UserForm, made a temporary file in the directory, inserted the picture, then deleted the temporary file. This has worked well for me and allowed me to insert a logo in sheets that were created from scratch.

In my subroutine I have the following code:

Dim UF as Object
Set UF = Application.Run("'Your Add-in Name.xlam'!GetUserForm1")
tempfile = ActiveWorkbook.Path & "\mypic.bmp"
SavePicture UF.Image1.Picture, tempfile

In my case I am using in the PageSetup by inserting the file in the header.

ws1.PageSetup.LeftHeaderPicture.FileName = tempfile

To insert in a sheet you can use

ActiveSheet.Pictures.Insert(tempfile)

Later in my subroutine I delete the temporary file with

Kill tempfile

This function needs to be in the module for this to work

Public Function GetUserForm1() as UserForm1
     Set GetUserForm1 = UserForm1
End Function

Upvotes: 1

Related Questions