How to prevent inserting spell checking tags in Word XML?

Viewed 387

I've got a file saved as the 'Word XML Document' format. I now need to replace some placeholders in it. I know there are better ways to make word documents a template, like shown here, but I can't choose. The variables use the ${var} format. Word regularly destroys the variable names due to spell checking. It splits them as follows:

<w:r>
    <w:t>${</w:t>
</w:r>
<w:proofErr w:type="spellStart" />
<w:r>
    <w:t>name</w:t>
</w:r>
<w:proofErr w:type="spellEnd" />
    <w:r>
        <w:t>}</w:t>
</w:r>

The first save usually is okay but the variables certainly will get torn apart a few saves later or instantly when I insert tables or headings anywhere in the document. The only way to get rid of it seems to rewrite all variables inside the document.

I've tried disabling spell checking of a document in the Word options which adds

<w:hideSpellingErrors />
<w:hideGrammaticalErrors />
<w:proofState w:spelling="clean" w:grammar="clean" />

to the settings tag inside the XML file. This looks good but does not stop the spellcheck tag generation.

I've also tried a tool which someone said was capable of removing the tags but I don't think it can. There must be some method of disabling the generation of those tags, in the end they just unnecessarily increase the file size. All other so questions had different prerequisites and/or lead to nothing.

Edit June/2020

I did a bit of trial and error and found out that the following code gets rid of any text-breaking tags:

fileContent.replace("</w:t></w:r><w:proofErr w:type=\"spellStart\"/><w:r><w:t>", "")
                .replace("</w:t></w:r><w:proofErr w:type=\"spellEnd\"/><w:r><w:t>", "")
                .replace("<w:proofErr w:type=\"spellStart\"/>", "").replace("<w:proofErr w:type=\"spellEnd\"/>", "");

The XML is still intact afterwards, everything worked for me. I did not manage to destroy any variables after adding the code. Not a nice solution but the whole thing is pretty dirty anyway. Either apply the replaces to your edited and saved XML file (like a Word XML file sanitizer) or - probably better, apply them when you read the document's content.

0 Answers
Related