3

There are 2 SSCCEs for this:

1.

I have added a picture to my VSTO document-level add-in by right clicking the solution -> add -> existing item -> myPic.jpg.

Now I am working with an Excel spreadsheet and want to add a picture to the top left header. Using PageSetup.LeftHeaderPicture.FileName and providing an absolute path to the picture it loads just fine while debugging.

When I am trying to change the path to not-absolute and say something like

ActiveSheet.PageSetup.LeftHeaderPicture.FileName = "\\Assets\\myPic.jpg"

I keep getting a HRESULT: 0x800A03EC exception.

enter image description here

I think I am not getting the correct syntax for to access the Assets\myPic.jpg.

2.

I have added a new resource, selected an existing item and selected the myPic.jpg. I can access it via Resource1.myPic but ActiveSheet.PageSetup.LeftHeaderPicture is read-only...

The PageSetup.LeftHeaderPicture.FileName needs a string type parameter and I am not sure how to retrieve the path to my already embed resource...

Q:

How do I embed a picture in my solution as a resource (or just an existing item) to be able to use it with PageSetup.LeftHeaderPicture.FileName?

Community
  • 1
  • 1
  • @rene the current working directory is `C:\Users\admin\Documents\Visual Studio 2012\Projects\ScreeningTemplate\ScreeningTemplate\bin\Debug` as I am debugging the project. –  Apr 02 '14 at 12:52
  • You are trying `@"Assets\myPic.jpg"` aren't you? – rene Apr 02 '14 at 13:11
  • @rene yes I have tried it. Still getting an exception –  Apr 02 '14 at 14:07
  • I would almost suggest to use ProcessMonitor to see the actual path it is trying. – rene Apr 02 '14 at 14:23

2 Answers2

2

Right, I have solved it.

Added an existing item myPic.jpg to the solution and set Build Action to Content and Copy to Output Directory to Copy Always (but I am sure you can set it to copy if newer)

enter image description here

Note: With this setup your file always gets "copied" to the published directory.

enter image description here

Now all you need in your code is

ws.PageSetup.LeftHeaderPicture.Filename = 
                           AppDomain.CurrentDomain.BaseDirectory + "\\myPic.jpg";
ws.PageSetup.LeftHeader = "&G";

AppDomain.CurrentDomain.BaseDirectory is very well explained here

and the end result as expected

enter image description here

Community
  • 1
  • 1
1

From my experience adding files to a project will embed them into your .exe. That means you can not refer them by using a file path. You can however read them.

If you want your file to placed outside the .exe file you should just add it your project then right click on it and select properties. Set the Copy to output directory property to Always copy. Your file will be copied into the build folder under the same folder structure. Now you can refer your file with a relative path.

If you really need to get that picture from the resource file you could save it as a temporary file (using Path.GetTempFileName) then load it via file path.

dburner
  • 1,007
  • 7
  • 22
  • Oops I deleted my last comment instead of editing... Can you give an example of relative path syntax so that I don't get an exception? –  Apr 02 '14 at 11:59
  • `@"\Assets\myPic.jpg"` or without the @ `"\\Assets\\myPic.jpg"` – dburner Apr 02 '14 at 12:00
  • both fail with the same exception type. –  Apr 02 '14 at 12:02
  • Try the full path then. Use `Path.GetFullPath` also try to look at this: http://stackoverflow.com/questions/12714626/exception-from-hresult-0x800a03ec-error – dburner Apr 02 '14 at 12:04
  • tried it with the `Path.GetFullPath` already. Updated the question with a screenshot of the exception/ –  Apr 02 '14 at 12:06
  • the strange thing is that `Path.GetFullPath("\\Assets\\myPic.jpg")` returns `C:\Assets\myPic.jpg` like if it didn't recognize the current folder structure :/// –  Apr 02 '14 at 12:56
  • Try to combine with `Enviroment.CurrentFolder` then – dburner Apr 02 '14 at 14:34
  • There is no exception where the path is hardcoded, the problem is that neither debug or release actually copy the image to it's destination folder –  Apr 02 '14 at 14:41
  • *`Set the Copy to output directory property to Always copy. Your file will be copied into the build folder under the same folder structure.`* but it's not copied :/ I think there might be a bug within VS –  Apr 02 '14 at 16:01