"Revision tracking is turned on for document" error in OpenXmlPowerTools

36 views Asked by At

I'm trying to replace text in a word document using OpenXmlPowerTools. The original file is hosted in OneDrive, then a copy is downloaded to the local computer. I'm tying to run OpenXmlPowerTools.WmlDocument's SearchAndReplace function on the local copy.

I'm getting an exception that "Revision tracking is turned on for document". I've loaded the original document in Office365's web-based word editor. On the review tab, Track Changes shows as "Off". If I click the next change or previous change button beside it; I get an error "There aren't any tracked changes in your document."

I don't want revision tracking. I just want to replace the text [CaseNo] with a number then save the document. How can I either get rid of revision tracking, or get OpenXmlPowerTools to ignore it and make my changes permanently?

My code is fairly simple:

OpenXmlPowerTools.WmlDocument doc = new OpenXmlPowerTools.WmlDocument(FileName);

foreach (MergeCode mc in mergeCodes)
    doc = doc.SearchAndReplace(mc.Code, mc.Value, true);
1

There are 1 answers

0
Brian Heward On

As far as I can tell Microsoft has changed the XML they save in their online editor and it's breaking OpenXmlPowerTools.

In Open-Xml-PowerTools/OpenXmlPowerTools/TextReplacer.cs line 230 (SearchAndReplace function for Word docs) It checks if the document has a tag for trackRevisions and throws an error if it does. When I make a new document in Ofice365’s browser-based editor and track revisions is set to “Off” this tag shows up in the document:

<w:trackRevisions w:val="false" xmlns:w=http://schemas.openxmlformats.org/wordprocessingml/2006/main />

This erroneously causes Open Xml PowerTools to think it can’t edit the document, even though trackRevisions is false.

I made my own version of TextReplacer with the line:

if (xDoc.Descendants(W.trackRevisions).Any())

Replaced with this one:

if (xDoc.Descendants(W.trackRevisions).Any(x => !x.Attributes().Any(x => x.Name.LocalName == "val" && x.Value == "false")))

Also on line 152 of the same file it’s checking for textValue[0], but my newly made document has an element with the following:

<r xmlns=http://schemas.openxmlformats.org/wordprocessingml/2006/main>
  <rPr />
  <t></t>
</r>

This causes an index out of range exception as there is no character at index 0. Changing the if statement to the following seems to fix it:

if (textValue.Length != 0 && (textValue[0] == ' ' || textValue[textValue.Length - 1] == ' '))

I've emailed the team at openxmldeveloper.org so hopefully they will test my patches and release a new version soon.