I discovered that you can use the Application Settings feature to persist data between application execution sessions for VSTO addins/smart documents. For those not familiar with the Settings object see MSDN’s Using Settings in C#, this is a good place to start. I discovered that a settings file can co-exist with the assemblies installed.

Below is an example where a generic VSTO Excel document is created. A user setting is created and named ColdStorage. It is important in Visual Studio to have that as a user setting so the value can be propagated between user sessions; Otherwise it is an application setting value which is readonly and will fail for our purposes. So set that value.

Steps

  1. Create a VSTO Excel document in VS2005.
  2. Create a user setting named ColdStorage as type string.
  3. Copy the below code into the Workbook startup and shutdown method

Startup

private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{

// Write current value from storage to a sheet

Excel.Range rangeB2 = Globals.Sheet1.get_Range("B2", missing);

rangeB2.Value2 = ExcelWorkbookTestSettings.Properties.Settings.Default.ColdStorage;

}

Shutdown

private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
{
    // Read from the sheet and save
    Excel.Range rangeB = Globals.Sheet1.get_Range("B2", missing);

    ExcelWorkbookTestSettings.Properties.Settings.Default.ColdStorage = rangeB.Value2.ToString();

    ExcelWorkbookTestSettings.Properties.Settings.Default.Save();

}
Share