Modify docx file using OpenXML c#

Viewed 27

MIGHT BE DUPLICATE

Well, first of all I tried and already search over the internet of the solution. But, I found nothing.

I am trying to change some text inside the word document but it is weird.

I have this code :

            using (WordprocessingDocument doc =
                WordprocessingDocument.Open(documentName, true))
            {
                var document = doc.MainDocumentPart.Document;

                foreach (var text in document.Descendants<Text>()) //PROBLEM IS HERE 
                {
                    if (text.Text.Contains("[COMPANYNAME]"))
                    {
                        text.Text = text.Text.Replace("[COMPANYNAME]", "TEST INC");
                    }                   
                }
            }

to change the boxed word [COMPANY NAME] to other text. But is wasn't altered. Yet, when I tried to debug it and make a list of string referring to text in the loop, I saw that the [COMPANY NAME] was split into 4 strings. The list contains [, COMPANY, NAME and ] so my condition wasn't met that is why is wasn't altered.

How can I fixed this problem?

0 Answers
Related