0

Apologies if this question is vague.

I have thousands of .docx and .doc files that need to be altered so that the field codes have a changed order. It is a relatively simple job to do manually, however the sheer number of files will take forever to do.

I have been learning the basics of C# recently and I would like some advice and guidance to see if automating the process is possible, and if so, how I would go about doing so.

I would like the program to read a .docx or .doc and search for a sequence of field codes eg.

{ FIELDCODE1 }/{ FIELDCODE2 }/{ FIELDCODE3 }

Then edit the file so the document then reads:

{ FIELDCODE2 }/{ FIELDCODE3 }/{ FIELDCODE1 }

Any help would be much appreciated.

Thanks in advance,

Jon

Jon Wall
  • 1
  • 1
  • It's possible, but it's a non-trivial problem, probably too much to answer in a SO question. You could write something that uses OpenXML to read the file, find those elements within the XML document, and rearrange them. [Here's some info](https://learn.microsoft.com/en-us/office/open-xml/word-processing). – Scott Hannen Jan 07 '22 at 16:19
  • [This archived page](https://web.archive.org/web/20190116000204/https://www.microsoft.com/en-us/download/details.aspx?id=30425) lets you download a tool that takes an existing Word document and creates C# code that generates the document. That gives you some examples of how to manipulate your specific document. – Scott Hannen Jan 07 '22 at 16:20
  • You can get the "OpenXML Productivity Tool" from Microsoft. It used to be easy to find, but Microsoft found an issue in the code and took it off the main site. I believe it's still available from GitHub. It will let you look at DOCX (and other OpenXML files) and see the XML and/or the code that generates them. It also lets you diff two files. It's an essential tool. Note that OpenXML tools are useless for DOC files. The DOC (and XLS and PPT) formats are very hard to work with. If your fields are in different places in your document, it will make it harder to work with – Flydog57 Jan 07 '22 at 16:26
  • As much as I despise VBA, it could help in this case. Maybe. You can open one document, start recording a macro, rearrange the fields, stop recording, and inspect the generated VBA. It's possible that it could reveal an easier way to do this. Then you could write VBA or .NET Interop code (more evil) to open the documents one at a time, manipulate them, and save them. – Scott Hannen Jan 07 '22 at 16:38
  • Thanks for your input. I have started using the Open XML SDK, and so far have managed to search and edit the target sequence. – Jon Wall Jan 11 '22 at 23:00

0 Answers0