5

I'm using open xml SDK 2.0 and i'm kind off new to this.

I have actually created a quickpart (containg content control) in my word 2007 document named "hello.docx". Now I need to copy the quickpart into the other location of the same document named "hello.docx". I was very thank full for this post http://www.techques.com/question/1-3448297/Replacing-Content-Controls-in-OpenXML and same thing is posted on stack overflow forum for which i was very thank full :)...This post just deletes the content control but keeps the content in Content control.

With the help of the above link I was able to modify the code to clone the content control and append to the same document (This part of my code is working). But i have problem in innerText. Though i replace the innerText in the open Xml element, it is not geting reflected in the doucument.

public static void AddingSdtBlock(string filename, string sdtBlockTag)
{
    using (WordprocessingDocument doc = WordprocessingDocument.Open(filename,true))
    {
        MainDocumentPart mainDocumentPart = doc.MainDocumentPart;
        List<SdtBlock> sdtList = mainDocumentPart.Document.Descendants<SdtBlock>().ToList();
        SdtBlock sdtA = null;

        foreach (SdtBlock sdt in sdtList)
        {
            if (sdt.SdtProperties.GetFirstChild<Tag>().Val.Value == sdtBlockTag)
            {
                sdtA = sdt;
                break;
            }
        }
        SdtBlock cloneSdkt = (SdtBlock)sdtA.Clone();



        OpenXmlElement sdtc = cloneSdkt.GetFirstChild<SdtContentBlock>();
      //  OpenXmlElement parent = cloneSdkt.Parent;

        OpenXmlElementList elements = cloneSdkt.ChildElements;

       // var mySdtc = new SdtContentBlock(cloneSdkt.OuterXml);

        foreach (OpenXmlElement elem in elements)
        {
          string innerxml=  elem.InnerText ;
          if (innerxml.Length>0)
          {
              string modified = "Class Name : My Class.Description : mydesc.AttributesNameDescriptionMy Attri name.my attri desc.Operations NameDescriptionmy ope name.my ope descriptn.";
             string replace= elem.InnerText.Replace(innerxml, modified);
            // mainDocumentPart.Document.Save();
          }
           // string text = parent.FirstChild.InnerText;
           // parent.Append((OpenXmlElement)elem.Clone());
        }

        mainDocumentPart.Document.Body.AppendChild<SdtBlock>(cloneSdkt);

        //sdtA.Remove();
    }
}

The Replaced string in the openXML element is not geting reflected in the document. Any help would be really appreciated.

Thanks in advance,

Ahmad Khan
  • 2,655
  • 19
  • 25
shishi
  • 107
  • 1
  • 2
  • 8

6 Answers6

2

Your "string replace= " line does nothing. By creating a variable you seem aware that string.Replace() is not an in-place replacement, but then you are not doing anything with the variable.

2

Instead of replacing InnerText, you can replace InnerXML

elem.InnerXml = elem.InnerXml.Replace(innerxml, modified);

and then call

mainDocumentPart.Document.Save();
1

This's a pretty old question but just want to share with guys using OpenXML 2.9, so that instead of replace innerText or InnerXML idea, I recommend you remove all child elements of that XmlElement and add a new paragraph with new content, for example:

var tableCell = mainPart.Document.Body.Elements<TableCell>().First();
tableCell.RemoveAllChildren();
tableCell.AppendChild<Paragraph>(new Paragraph(new Run(new Text("This is new text"))));
mainPart.Document.Save();

Or Update existing Text element

var tableCell = mainPart.Document.Body.Elements<TableCell>().First();
tableCell.Descendants<Text>().FirstOrDefault(e => e.Text == "This is old text");
if (text != null)
{
    text.Text = "This is new text";
}
mainPart.Document.Save();
Nick Hoàng
  • 417
  • 2
  • 6
0

You need to call mainDocumentPart.Document.Save(); as the last line at the end of your using statement that opens the file. This will save any changes you made to the document while it has been opened.

amurra
  • 15,221
  • 4
  • 70
  • 87
  • Thanks Amurra for your suggestion, – shishi Aug 09 '11 at 05:39
  • Amurra, I tried doing that but in vain ( see my above post mainDocumentPart.Document.Save(); is already commented because it was already tried by me ) – shishi Aug 09 '11 at 05:42
  • @shishi - Did you try calling the save after you do the `mainDocumentPart.Document.Body.AppendChild(cloneSdkt); ` call? Where you where calling save will not alter the document since you have not appended the cloned elements back into the document. – amurra Aug 09 '11 at 10:46
0

i had same problem. my solution is first do the replacement and then cloning.

ehsan
  • 787
  • 3
  • 13
  • 26
0

You must use for loop instead of foreach loop and access to each element using it's indexer.

Vahid Ghadiri
  • 3,966
  • 7
  • 36
  • 45