How do I assign a relative path of an embed image to a ActiveSheet.PageSetup.LeftHeaderPicture.FileName? RRS feed

  • Question

  • Hello all.

    I have been searching and researching this for hours now but I am unable to solve this mystery. 

    I have created an Excel 2010 Workbook project in Visual Studio Pro 2012. ( document-level add-in ).

    In the add-in ThisWorkbook_StartUp event I am instantiating a custom class responsible for applying formatting to Sheet1.

    CostRequest format = new CostRequest();

    I have obviously added a class to my solution and a few lines to start the formatting

    ws.PageSetup.LeftHeaderPicture.Filename = 
        Path.GetFullPath(@"C:\Users\michal.krzych\documents\visual studio 2012\Projects\ScreeningTemplate\ScreeningTemplate\Graphics\gsmLogo.jpg");
    ws.PageSetup.LeftHeader = "&G";

    I have created a Graphics folder in the solution and added an existing item gsmLogo.jpg to it. I have modified the properties of gsmLogo.jpg and set Build action to Embedded Resource, and Always copy to output directory.

    The above works fine when I am debugging the project from my machine

    However, when I publish the solution and try to run in on a different machine I get an exception

    I think it's because the path is absolute.... When I get inside the app.publish folder and look at  the folders structure I can't see the Graphics folder nor the gsmLogo.jpg....

    I would like to permanently attach the image to my project and assign its path to the ws.PageSetup.LeftHeaderPicture.Filename but I haven't been able to get that to work :/

    I have tried adding a new resource, I can access the resource Resource1.gsmLogo just fine but I can't assign it to ws.PageSetup.LeftHeaderPicture because it's read only. 

    I don't know how to "retrieve the path of a resource" in a string form so I can just assign if to the .FileName property.

    I am kind of out of options now.. Not sure how to make this work so any help is much appreciated.

    PS. if any more detail is needed please see

    Wednesday, April 2, 2014 2:03 PM


  • Right, I have solved it

    see solution here

    • Marked as answer by Michal Krzych Thursday, April 3, 2014 9:19 AM
    Thursday, April 3, 2014 9:19 AM