I have a word file that have multy Rich Text Content Control
I want to change text of it. I Use this code .
using (WordprocessingDocument theDoc = WordprocessingDocument.Open(docName, true))
{
MainDocumentPart mainPart = theDoc.MainDocumentPart;
foreach (SdtElement sdt in mainPart.Document.Descendants<SdtElement>())
{
SdtAlias alias = sdt.Descendants<SdtAlias>().FirstOrDefault();
if (alias != null)
{
string sdtTitle = alias.Val.Value;
var t = sdt.Descendants<Text>().FirstOrDefault();
t.Text="Atul works at Microsoft as a .NET consultant. As a consultant his job is to design, develop and deploy";
}
}
}
It is add new text with old text. But i want to replace
this!!!
You only retrieve and update the first Text of your sdtContent. To replace all of it, the simples way is:
Update your code with:
edit: I forget to add the paragraph and the run
You can see how is build a SdtContent here https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.sdtcontentblock(v=office.14).aspx