Prevent Word document fields from updating when opened

I wrote a utility for another command that recursively scans folders and converts Word documents found in PDF using Word Interop with C #.

The problem we are facing is that the documents were created with date fields that are updated until today before they are saved. I found a way to disable updating fields before printing, but I need to prevent the fields from updating when open.

Is it possible? I would like to make a fix in C #, but if I need to make a Word macro, I can.

+7
source share
3 answers

Well, I did not find a way to do this with Interop, but my company bought Aspose.Words and I wrote a utility for converting Word documents to TIFF images. The Aspose tool will not update fields unless you explicitly specify this. Here is a sample code that I used with Aspose. Keep in mind that I had a requirement to convert Word documents to single-page TIFF images, and I hard-coded many options, because it was just a useful project for me.

private static bool ConvertWordToTiff(string inputFilePath, string outputFilePath) { try { Document doc = new Document(inputFilePath); for (int i = 0; i < doc.PageCount; i++) { ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Tiff); options.PageIndex = i; options.PageCount = 1; options.TiffCompression = TiffCompression.Lzw; options.Resolution = 200; options.ImageColorMode = ImageColorMode.BlackAndWhite; var extension = Path.GetExtension(outputFilePath); var pageNum = String.Format("-{0:000}", (i+1)); var outputPageFilePath = outputFilePath.Replace(extension, pageNum + extension); doc.Save(outputPageFilePath, options); } return true; } catch (Exception ex) { LogError(ex); return false; } } 
+1
source

As described in the endless labyrinth of Microsoft documentation , you can block the field code. For example, in VBA, if I have one date field in the body in the form

 {DATE \@ "M/d/yyyy h:mm:ss am/pm" \* MERGEFORMAT } 

I can run

 ActiveDocument.Fields(1).Locked = True 

Then, if I made changes to the document, save and then open it again, the field code will not be updated.

An example of using C # Office Interop:

 Word.Application wordApp = new Word.Application(); Word.Document wordDoc = wordApp.ActiveDocument; wordDoc.Fields.Locked = 1; //its apparently an int32 rather than a bool 

You can put the code in the DocumentOpen event. I assume you have an add-in that subscribes to this event. If not, please clarify, as this may be a battle in itself.

EDIT: In my testing, lock fields in this way block them in all StoryRanges , so there is no need to get field instances in headers, footers, footnotes, text boxes, etc. This is an amazing pleasure.

+4
source

I think a new question about SO is appropriate then because it will require XML processing, not just Office Interop. If you have both types of .doc and .docx files to convert, you may need two separate solutions: one for WordML (Word 2003 XML format) and the other for OpenXML format (Word 2007/2010/2013 XML), because you cannot open the old file format and save as new without updating the fields.

Checking the OOXML of the locked field shows us this w:fldLock="1" attribute. This can be inserted using appropriate XML processing against the document, for example, through the OOXML SDK or through the standard XSLT transform.

May be useful: this question how-do-i-unlock-a-content-control-using-the-openxml-sdk-in-a-word-2010-document may be similar, but for Content Controls. You can apply the same solution to fields if the Lock and LockingValues apply the same to fields. However, I am not sure about that.

To increase confidence that this is the way to do this, see the example of this provider solution for the problem. If you need to develop this inside, then openxmldeveloper.org is a good place to start - find Eric White's examples of manipulating fields like this .

0
source

All Articles