How can I capture the paragraph numbering as a 'part' of the text and export it to a DOCX?
I have a document that's split into sections and sub-sections that reads similarly to a set of state statutes (Statute 208
, with subsections Statute 208.1
, Statute 208.2
, etc.). We created this by modifying the numbering.xml
file within the .docx zip.
I want to export a 'sub-section' (208.5
) and its text to a separate .docx file. My VSTO add-in exports the text well enough, but the numbering resets to 208.1
. This does make some sense as it's now the first paragraph with that <ilvl>
in the document.
Funnily enough, I'm able to call Word.Range
's ExportAsFixedFormat
function and export this selection to PDF just fine - even retaining the numbering. This led me down a path of trying to 'render' the selection, possibly as it would be printed, in order to throw it into a new .docx file, but I haven't figured that out, either.
Range.ExportFragment()
using both wdFormatStrictOpenXMLDocument
and wdFormatDocumentDefault
as the wdSaveType
values.
Document.PrintOut()
using PrintToFile = true
and a valid filename. I realize now that this, quite literally, generates 'printout instructions' and won't inject a new file at path filename
with any valid file structure.
Application.Selection.XML
to a variable content
and calling Document.Content.InsertXML(content)
on a newly added Document object.
using Word = Microsoft.Office.Interop.Word;
Word.Range range = Application.ActiveDocument.Range(startPosition, endPosition);
range.Select();
//export to DOCX?
Application.Selection.Range.ExportFragment(
filename, Word.WdSaveFormat.wdFormatDocumentDefault);
You could use ConvertNumbersToText(wdNumberAllNumbers) before exporting, then _Document.Undo()
or close without saving after the export.