I have several contentcontrols in a word document that I am trying to fill using c#. My code works fine when the contentcontrol in the word document is empty. However, when I enter text in the contentcontrol, save, then try to edit or update this text using
c#, the update does not happen! Here is my update code:
control.SetPlaceholderText(null, null, newText);
Is there a way to change the text in a contentcontrol that already has text in it? How?
This is because you are changing the placeholder text and not the text of the content control. Placeholder text is a text that only shows up when the content of a content control is empty (e.g. "Insert text here").
Instead, do the following:
control.Range.Text = newText;
Just to alert you: Using Office application automation using COM in ASP.NET applications is
not recommended. For your scenario you should consider either creating an
Open XML solution or getting a 3rd party solution.
Marked as answer by george_d37 on Nov 16, 2012 11:26 AM
In my case, I have users who create a word template with specific fields (contentcontrols) to be populated from the database.
I am creating a button in the ribbon that once clicked, opens a textbox in which the user inserts the name of an employee and clicks ok. Some code will then execute to fetch the data related to this employee and populate the word template with these data.
I have looked at using xml but it appears that xml stopped working due to court ruling if im not mistaken. Please guide me as to whether what I am doing is the best option or if there is a better way to doing this.
george_d37
Member
5 Points
6 Posts
Word 2010: updating text of contentcontrol using c#
Nov 13, 2012 12:13 PM|LINK
Hi,
I have several contentcontrols in a word document that I am trying to fill using c#. My code works fine when the contentcontrol in the word document is empty. However, when I enter text in the contentcontrol, save, then try to edit or update this text using c#, the update does not happen! Here is my update code:
control.SetPlaceholderText(null, null, newText);
Is there a way to change the text in a contentcontrol that already has text in it? How?
Any help much appreciated.
Thanks,
George
Jure.Les
Member
376 Points
69 Posts
Re: Word 2010: updating text of contentcontrol using c#
Nov 15, 2012 08:40 AM|LINK
This is because you are changing the placeholder text and not the text of the content control. Placeholder text is a text that only shows up when the content of a content control is empty (e.g. "Insert text here").
Instead, do the following:
control.Range.Text = newText;
Just to alert you: Using Office application automation using COM in ASP.NET applications is not recommended. For your scenario you should consider either creating an Open XML solution or getting a 3rd party solution.
george_d37
Member
5 Points
6 Posts
Re: Word 2010: updating text of contentcontrol using c#
Nov 16, 2012 11:43 AM|LINK
Thanks for the help.
In my case, I have users who create a word template with specific fields (contentcontrols) to be populated from the database.
I am creating a button in the ribbon that once clicked, opens a textbox in which the user inserts the name of an employee and clicks ok. Some code will then execute to fetch the data related to this employee and populate the word template with these data.
I have looked at using xml but it appears that xml stopped working due to court ruling if im not mistaken. Please guide me as to whether what I am doing is the best option or if there is a better way to doing this.
Thanks again!