1

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?

Milo
  • 99
  • 7
  • LOL, if someone has the same problem. please refer to this question. It might save you some time. https://stackoverflow.com/questions/18316873/replace-text-in-word-document-using-open-xml. This question will be closed already. Thanks – Milo Aug 25 '22 at 07:25

0 Answers0