I'm trying to add a picture to an Excel sheet, using a picture in the resources file, using VB2010. I can do it using: xlDATAWorkSheet.Shapes.AddPicture(...)
with the path coded in, but I want to use it from the resources file.
This is what I've tried:
xlDATAWorkSheet.Shapes.AddPicture(CType(My.Resources.ResourceManager.GetObject("Logo"), Bitmap), False, True, 0, 0, 300, 50)
, but I get an error: file not found (pointing to the Logo word)*
I also tried:
xlDATAWorkSheet.Shapes.AddPicture(My.Resources.Logo, False, True, 0, 0, 300, 50)
, but I get same error: file not found.
It works fine with the path to the picture hard coded, like xlDATAWorkSheet.Shapes.AddPicture("C:/Logo.jpg", False, True, 0, 0, 300, 50),
but I want to use it from the resource file (so it will run on another machine)
Also, this works ok, so Logo image is really there:
frmMain.pic1.Image = CType(My.Resources.ResourceManager.GetObject("Logo"), Image)
It seems though, according to the MSDN docs for Shapes, there does not appear that AddPicture takes anything other than a string to determine what image you want to insert.
Can anyone help insert a picture from the resources to an Excel sheet in VB2010?
You can add an image to excel using the clipboard automatically in your code.
The below code works in vb net 2008.
The argument you have to pass to that excel interop function unfortunately requires a string of the filepath.
You can try the following though, and see if it works for you:
PictureBox1.Image.Save(SaveFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
Note that savefilepath doesn't have to include the .jpeg extension in it's nameLengthy solution, yes. But should work given the restrictions.