none
Adding A Workbook As An Existing Item RRS feed

  • Question

  • Hi everyone,

    In one of my VSTO add-ins I need to add an existing workbook which I have done so via Ctrl+D and locating the same and marking the same to be copied to the Output Folder upon change, so as to be available for use by end-user as well.

    However, can't find any way to refer to such a workbook or its worksheets or some named or specific range in the same, by assigning the same to some Excel.Workbook, Excel.Worksheet, Excel.Range type variables, respectively, and carryout the procedures like complex calculations (which I seek to be made available for changes in rates and factors if and when required etc.) as per the models in the same.

    Although a document level applications suggested in an earlier similar question did cater the needs appropriately the same does not fit in the picture this time as the said question pertains to only a part of the said application level add-in.

    Please see if one can help.

    Thanks in advance.


    Best Regards, Faraz A Qureshi

    Wednesday, September 17, 2014 11:29 AM

Answers

  • Hi FARAZ,

    No, what I mean is that using the excel workbook project, we could easy to select an existing document.

    We could add a workbook to the excel add-in project and referred it.

    For example:

    1. Create a new folder (e.g. Files)
    2. Create or copy an excel file to that folder.
    3. Change the properties of that file: Copy to Output Directory=>Copy if newer

    After that we could refer to it directly by using this code below:

    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Files\\MyExcel.xlsx");
    this.Application.Workbooks.Open(path);

    If you want to show a dialog to select a file, you could refer to this code below:

     OpenFileDialog openFileDialog1 = new OpenFileDialog();
    
                openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
                openFileDialog1.Filter = "excel files (*.xlsx)|*.xls|All files (*.*)|*.*";
                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;
                openFileDialog1.Multiselect=false;
    
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        if(openFileDialog1.CheckFileExists)
                        {
                            string path = openFileDialog1.FileName;
                            this.Application.Workbooks.Open(path);
                        }
                        
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                    }
                }

    Best Regards

    Starain Chen


    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.

    Friday, September 19, 2014 1:49 AM
    Moderator

All replies

  • Hi FARA,

    For this requirement, you could create excel workbook project and select copy an existing document. For this way, you could select a exist file.

    Best Regards

    Starain Chen


    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.

    Thursday, September 18, 2014 10:01 AM
    Moderator
  • In simple words you mean a workbook can't be added to an Existing Excel add-in project and be referred to??? Didn't expect such a limitation from VSTO.

    Best Regards, Faraz A Qureshi

    Friday, September 19, 2014 1:32 AM
  • Hi FARAZ,

    No, what I mean is that using the excel workbook project, we could easy to select an existing document.

    We could add a workbook to the excel add-in project and referred it.

    For example:

    1. Create a new folder (e.g. Files)
    2. Create or copy an excel file to that folder.
    3. Change the properties of that file: Copy to Output Directory=>Copy if newer

    After that we could refer to it directly by using this code below:

    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Files\\MyExcel.xlsx");
    this.Application.Workbooks.Open(path);

    If you want to show a dialog to select a file, you could refer to this code below:

     OpenFileDialog openFileDialog1 = new OpenFileDialog();
    
                openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
                openFileDialog1.Filter = "excel files (*.xlsx)|*.xls|All files (*.*)|*.*";
                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;
                openFileDialog1.Multiselect=false;
    
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        if(openFileDialog1.CheckFileExists)
                        {
                            string path = openFileDialog1.FileName;
                            this.Application.Workbooks.Open(path);
                        }
                        
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                    }
                }

    Best Regards

    Starain Chen


    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.

    Friday, September 19, 2014 1:49 AM
    Moderator
  • Thanx!

    An excellent example!


    Best Regards, Faraz A Qureshi

    Saturday, September 20, 2014 7:43 PM