none
Applying Chart Template from Project Resource RRS feed

  • Question

  • Hey guys,

    the following situation:

    I'm developing a excel addin (Office Excel 2013) and would like to save a chart template within my project ... as embedded resource or whatever and would like to apply these template to a chart (easy command: chartObj.Chart.ApplyChartTemplate(path)) ... but the path is my problem. I have no idea where to get it ...

    Any suggestions?

    Tuesday, April 29, 2014 11:15 AM

Answers

  • Hi,

    In fact, all Chart templates should be stored in the Charts folder under Templates to be managed in Excel. To apply a chart template, if a chart template “.crtx” file is located in a folder other than the Charts folder, click Manage Templates, locate the chart template, and then copy or move it to the Charts folder under Templates. For example, in my Excel 2013, all Chart templates are stored under “C:\Users\Luna\AppData\Roaming\Microsoft\Templates\Charts”.

    More details please refer to: Reuse a favorite chart by using a chart template (Though it is for Excel 2007, but the feature is fit for Excel 2013)

    Since you said you save a Chart template within your project, I think you may need to move the template to the Charts folder under Templates, and then you could use the Chart.ApplyChartTemplate Method to achieve the goal.

    private void ApplyChartTemplateTest()
    {
        Excel.Application app = Globals.ThisAddIn.Application;
        Excel.Worksheet ws = app.ActiveSheet;
        ws.Shapes.AddChart2(201, Excel.XlChartType.xlColumnClustered).Select();
        Excel.Chart cha = app.ActiveChart;
        cha.ApplyChartTemplate(@"C:\Users\Luna\AppData\Roaming\Microsoft\Templates\Charts\Chart1.crtx");
        cha.SetSourceData(ws.Range["Sheet1!$A$1:$D$4"]);
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by trdsolutions Friday, May 2, 2014 10:10 AM
    Wednesday, April 30, 2014 6:28 AM
    Moderator

All replies

  • Hi,

    In fact, all Chart templates should be stored in the Charts folder under Templates to be managed in Excel. To apply a chart template, if a chart template “.crtx” file is located in a folder other than the Charts folder, click Manage Templates, locate the chart template, and then copy or move it to the Charts folder under Templates. For example, in my Excel 2013, all Chart templates are stored under “C:\Users\Luna\AppData\Roaming\Microsoft\Templates\Charts”.

    More details please refer to: Reuse a favorite chart by using a chart template (Though it is for Excel 2007, but the feature is fit for Excel 2013)

    Since you said you save a Chart template within your project, I think you may need to move the template to the Charts folder under Templates, and then you could use the Chart.ApplyChartTemplate Method to achieve the goal.

    private void ApplyChartTemplateTest()
    {
        Excel.Application app = Globals.ThisAddIn.Application;
        Excel.Worksheet ws = app.ActiveSheet;
        ws.Shapes.AddChart2(201, Excel.XlChartType.xlColumnClustered).Select();
        Excel.Chart cha = app.ActiveChart;
        cha.ApplyChartTemplate(@"C:\Users\Luna\AppData\Roaming\Microsoft\Templates\Charts\Chart1.crtx");
        cha.SetSourceData(ws.Range["Sheet1!$A$1:$D$4"]);
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by trdsolutions Friday, May 2, 2014 10:10 AM
    Wednesday, April 30, 2014 6:28 AM
    Moderator
  • Hey Luna,

    thank you very much!

    I guess this will help much. I'll report after setting up.

    Tobias


    • Edited by trdsolutions Friday, May 2, 2014 9:16 AM Spelling
    Friday, May 2, 2014 9:16 AM
  • Hey Luna,

    thank you very much! It worked perfect!

    For all interests:

    1. Store the .ctrx-File in your Properties Resources

    2. I choose to copy the Template when the neccessary report was called with:

    string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    string templatePath = appDataPath + @"\Microsoft\Templates\Charts\Chart1.crtx";
    File.WriteAllBytes(templatePath, Properties.Resources.Chart1);
    
    while (FileIsLocked(templatePath)) ;
    //For more details:
    // => http://stackoverflow.com/questions/10982104/wait-until-file-is-completely-written
    
    cObj.Chart.ApplyChartTemplate(templatePath);
    3. Run it and everything works fine!



    • Edited by trdsolutions Friday, May 2, 2014 10:17 AM Syntax
    Friday, May 2, 2014 10:15 AM