How to save data in an Office Addin?
Task:
I want to store user-specific data from an input field in a Word add-in.
As soon as the user changes in the input field, the value should be changed in the input field next time.
Solution:
Use a settings file
You add the Word project under Project-> Add-> New Item .. Add a Windows Forms Settings file
Add New Item
In General-> Settings File select
Settings Enter values
The settings file can be opened by double-clicking and entering data as values
Here: Name: Photos_Tablename and Value: Image documentation.
Scope means:
User: the value can be read and changed at runtime.
Application: the value is read only
Read values ββat runtime
At runtime the text box input fields is loaded in the example.
Simply read the value from Settings1.Default.Variablename.
private void Ribbon1_Load(object sender, RibbonUIEventArgs e) { tbxPhotos_Tablename.Text = Settings1.Default.Photos_Tablename; tbxExcel_Filename.Text = Settings1.Default.Excel_Filename; tbxExcel_Sheet.Text = Settings1.Default.Excel_Sheet; }
|
Change values ββand save
Important: do not forget Save ().
Example Change and save.
As soon as the text field is changed and exited, the text is taken from the text box and saved under Settings1.Default.VariableName.
private void tbxPhotos_Tablename_TextChanged(object sender, RibbonControlEventArgs e) { Settings1.Default.Photos_Tablename = tbxPhotos_Tablename.Text; Settings1.Default.Save(); }
private void tbxExcel_Filename_TextChanged(object sender, RibbonControlEventArgs e) { Settings1.Default.Excel_Filename = tbxExcel_Filename.Text; Settings1.Default.Save(); } private void tbxExcel_Sheet_TextChanged(object sender, RibbonControlEventArgs e) { Settings1.Default.Excel_Sheet = tbxExcel_Sheet.Text; Settings1.Default.Save(); }
|
Subject: Word Excel Access Outlook VSTO Add to Ribbonbar