kunal singh
asked on
button on being saved in word VSTO
i have this pieace of code
before i save the word doc:
After i save the Word Doc: After i save the Word Doc:
private void button2_Click(object sender, RibbonControlEventArgs e)
{
Microsoft.Office.Tools.Word.Controls.Button salesButton;
Document vstodoc = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
Microsoft.Office.Interop.Word.Application objApplication = Globals.ThisAddIn.Application;
Microsoft.Office.Interop.Word.Selection objSelection = objApplication.Selection;
Microsoft.Office.Interop.Word.Range objRange = objSelection.Range;
salesButton = vstodoc.Controls.AddButton(objRange, 20, 20, "salesButton");
salesButton.Text = "Calculate Total Sales";
}
when i click on the button a button is inserted inside word doc but when i save it and try to reopen the word document the button is not therebefore i save the word doc:
After i save the Word Doc: After i save the Word Doc:
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
1. all dynamically created host controls are disconnected from their events and they lose their data binding functionality. You can add code to your solution to re-create the host controls when the document is reopened (Dynamic MsControl)
Step 1 create a word-addsin
Step 2Add reference to the project for
Open in new window
Step 3 Add a ribbon to the project
Step 4go to your ThisAddsIn.cs file
Open in new window
create a new method in the above class name it WhenRibionBtnIsClicked()Open in new window
Step 5Create a button click event for the button that has been generated in the word fileOpen in new window
Step 6and now in the Ribbon.cs file get the click event of the button created in the ribbon and point it to the above created classOpen in new window
so now when you run it you should be able to see the following:however the problem you will face after this is when you save the document this things are removed the so what you must do is re-create this buttons when the word starts and use the properties you have stored to get the button state or other info about the button
Step 7go to the function ThisAddIn_Startup
Open in new window
the above code should be able to get all your info from the custom property that you made and re-create the button. if for some reason when you start the document and the adds-in tab does not show up in word that means there is an error in your ThisAddIn_Startup function -to debug try putting everything in ThisAddIn_Startup function into a separate button then test and see the error code. Please Note: you can basicaly do everything you would do in a normal C# form (opening forms closing forms,reading text files, and etc)i have provided a sample in github with a debug button you can check
Persist Dynamic Controli found a solution to this problem.
1. all dynamically created host controls are disconnected from their events and they lose their data binding functionality. You can add code to your solution to re-create the host controls when the document is reopened (Dynamic MsControl)
Step 1 create a word-addsin
Step 2Add reference to the project for
Open in new window
Step 3 Add a ribbon to the project
Step 4go to your ThisAddsIn.cs file
Open in new window
create a new method in the above class name it WhenRibionBtnIsClicked()Open in new window
Step 5Create a button click event for the button that has been generated in the word fileOpen in new window
Step 6and now in the Ribbon.cs file get the click event of the button created in the ribbon and point it to the above created classOpen in new window
so now when you run it you should be able to see the following:however the problem you will face after this is when you save the document this things are removed the so what you must do is re-create this buttons when the word starts and use the properties you have stored to get the button state or other info about the button
Step 7go to the function ThisAddIn_Startup
Open in new window
the above code should be able to get all your info from the custom property that you made and re-create the button. if for some reason when you start the document and the adds-in tab does not show up in word that means there is an error in your ThisAddIn_Startup function -to debug try putting everything in ThisAddIn_Startup function into a separate button then test and see the error code. Please Note: you can basicaly do everything you would do in a normal C# form (opening forms closing forms,reading text files, and etc)i have provided a sample in github with a debug button you can check
Persist Dynamic Control