none
How to create a new Excel workbook by pressing a button on the ribbon from a pre-created template file. RRS feed

  • Question

  • Hello VSTO experts,
    I created a Ribbon with VSTO for Excel and a Excel template (extension name is xltx). On the ribbon, I have a button. I want when I click this button, it will create a Excel workbook from my template.
    I know that can use: Globals.ThisAddin.Application.Workbooks.Add(Template). But I want when my end users deploy my add in on their computer, they can choose the Install Forlder, and my template will be copied on this forlder.
    My add in will load template from here and create new workbook. What can I do?
    Thanks you very much!

    • Edited by Hung Dau Sunday, March 25, 2018 10:16 AM
    Sunday, March 25, 2018 10:15 AM

All replies

  • Hi Hung Dau,

    You can try to save the Excel Template on desired location then you can try to use "Workbook.Open" method to create new Excel file based on Template.

    Below is example code:

     private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Excel.Application excelApp = new Excel.Application();
                string myPath = @"C:\Users\v-padee\Desktop\Book1.xltm";
    
                excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value);
                excelApp.Visible = true;
            }

    You can modify it as per your requirement.

    Reference:

    Workbooks.Open Method (Excel)

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 26, 2018 3:11 AM
    Moderator
  • Thanks you. I know that but it is not solution for my problem. I want to have some code, it will help me copy file A.xltx to a path (installation path) when I install add in to machine.<o:p></o:p>

    A.xltx have same location with VSTO file after publish.<o:p></o:p>

    Saturday, April 21, 2018 3:12 PM
  • Hi Hung Dau,

    You had mentioned that,"I want to have some code, it will help me copy file A.xltx to a path ."

    You can try to refer link below which can give you some examples to copy file.

    How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)

    Further, You can try to use Open file dialog to get the destination path.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 24, 2018 1:33 AM
    Moderator