Search code examples
c#excel-2007compatibilityexcel-2003excel-interop

Creating Workbooks from templates programmatically


I have an excel template which was created in "Office 2003",That being said, I need to create a new workbook from this existing template programmatically. Also if there are any things that would cause an error, such as locked cells, hidden formulas, hidden sheets, etc. please mention such.

A few things that might be help full to know

  1. I have upgraded from Office 2003 -> Office 2010, however, the file format is Excel 2007.

  2. There are over 303 rows and 26 columns, each one contains its own unique formula (that is just on one sheet), total there are probably over 700 rows, and 100 columns (before adding any additional worksheets), each containing their own relatively unique formulas.##

  3. Certain cells and worksheets are locked as to prevent user-induced errors, or user tampering with in-cell formulas.

  4. This template is going to be used by several people, all of whom have their own computers. I cannot guarantee that the files will be in the same location on every single users computer.

Software Information:

Visual Studio 2013 Office 2010 Office 2003


Solution

  • Found a solution!

    within vs2013, they offer excel 2007 addin, within that predefined interoperability, there is a way to create a file from a template, the part that was throwing me off was the part of the parameters which shows:

    "Type.missing". an example of this code structure would be as follows:

    //the #region is simply so that if copied and pasted, the code will make sense, and there will be a label encapsulating the specific code lines. *Note it does not affect the results
    #region add workbook from template file
    Excel.Application f;
    //the following line is written assuming that you would have already made sure that excel was in the Running Objects Table (ROT)
    f=(Excel.Application)Marshal.GetActiveObject("Excel.Application");
    f.visible=true;
    f.Workbooks.Add("C:\\...");
    \if there are any questions please comment, I will do my best to explain my own answer
    #endregion