views:

196

answers:

1

I'm using the OpenXML SDK to programatically replace some <w:sdt/> elements with chunks of OpenXML (WordProcessingML) markup.

For example, I have a paragraph with these contents:

<w:p>
  <w:run><w:text> Text before </w:text></w:run>
  <w:sdt><w:sdtPr> ...</w:sdtPr><w:sdtContent>...</w:sdtContent></w:sdt>
  <w:run><w:text> Text after </w:text></w:run>
</w:p>

And a table with a structure like this:

<w:tbl>
  <w:tblPr>...</w:tblPr>
  <w:tblGrid> ... gridCol elements ...</w:tblGrid>
  <w:tr>
    <w:trPr>...</w:trPr>
    <w:tc>
   <w:tcPr>...</w:tcPr>
   <w:p> Cell contents </w:p>
    </w:tc>
    <w:tc>
   <w:tcPr>...</w:tcPr>
   <w:p> Cell contents </w:p>
    </w:tc>
  </w:tr>
 </w:tbl>

Basically I want to replace the <w:sdt/> element in the paragraph with the table markup. The problem is that I cannot just replace it because it will create an invalid document (a table with paragraphs inside another paragraph element isn't valid).

The result that I want to get is this:

<w:p>
  <w:run><w:text> Text before </w:text></w:run>
</w:p>
<w:tbl>
  <w:tblPr>...</w:tblPr>
  <w:tblGrid> ... gridCol elements ...</w:tblGrid>
  <w:tr>
    <w:trPr>...</w:trPr>
    <w:tc>
   <w:tcPr>...</w:tcPr>
   <w:p> Cell contents </w:p>
    </w:tc>
    <w:tc>
   <w:tcPr>...</w:tcPr>
   <w:p> Cell contents </w:p>
    </w:tc>
  </w:tr>
</w:tbl>
<w:p>
  <w:run><w:text> Text after </w:text></w:run>
</w:p>

I was thinking on creating a method that created a new paragraph with the elements before the <w:sdt/> element, and another one with the elements after the sdt element, but it seems like such a method would be error-prone.

Is there a method that can do what I want without having to do it manually (a method that does what Word does when I try to insert a new table inside a text line)?

Any help will be greatly appreciated.

+1  A: 

I totally think you should go down the path of splitting the the runs before and after the Content Control that you want to replace with a table into seperate paragraphs. It's only really error prone if you give Word a reason to have weird runs though. Like I wants a new thing will be three runs because wants is syntactically incorrect in this sentence and Word will flag that as dirty and unless/until you choose in Word to correct it or to ignore it, it will remain three runs. That's the only issue I would foresee.

However, if you've got a strategy around that (like forcing users to spell-check their documents), the rest is certainly doable. If you're not familiar with Eric White's blog, it will be your best friend when developing with the Open XML SDK in Word. For example, these articles alone should answer most/all of your questions about how to use functional transformation to parse, insert and replace.

  1. Processing all Content Parts in an Open XML WordprocessingML Document
  2. Inserting / Deleting / Moving Paragraphs in Open XML Wordprocessing Documents
  3. Splitting Runs in Open XML Word Processing Document Paragraphs
Otaku
Thanks for your answer, I'm not sure that I made myself clear, I just updated the question with the expected behavior.Another thing to take into account is that there might be nested things inside the original paragraph (like an sdt element inside the paragraph) and I'd have to take that into account when splitting the existing paragraph and creating the new one.
willvv
@willvv: Yeah, the first time around you were pretty clear. The advice still stands - you'd search, probably via Linq, on each paragraph, location any first-level `<sdt/>` element, take all runs before that, create a paragraph element out of them, same with all the ones after and then with the `<sdt/>` you would replace it with a `<tbl/>`. Or you could process the information therein more, changes and using it as needed. When you have all of those, you write over to simply replace the original `<p/>`. The links above will help you with this task.
Otaku