3

Is it possible to add (thru the MS Word object model? or by editing file) some custom data (another file maybe?)

Will be that data preserved after opening, changing and saving document in MS Word ?

(I need to put some tracking data in docx during some report export, custom data will be used again during import)

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
razor
  • 2,727
  • 6
  • 33
  • 45
  • you mean data that is not actually part of the document? That seems counterintuitive. I wouldn't expect a word processor to process, generate or preserve data not related to my document. – jalf Jun 22 '10 at 15:20
  • data should be attached to file/document, but not accessible (easly) by user in Word application – razor Dec 12 '13 at 15:01

5 Answers5

13

Yes. It's rather straightforward, but as far as I know, not documented (at all or very well). Say you want to add an .ini file to your your Office doc (.docx, .xslx, .pptx) to be "carried around" with your document (i.e. you send your .docx to someone over email and want the yourfile.ini you've added to stay with the yourdoc.docx even if the person opening the DOCX makes some changes and then saves it and sends it back to you).

  1. Rename your document's extention to .zip. Unzip it.
  2. In the root folder, find the [Content_Types].xml file and open it.
  3. Right under the <Type/> root element, add <Default Extension="ini" ContentType="custom/ini"/> (note - for another file, just make sure the extention matches the file you're going to add and the attribute Extention matches that. For the ContentType attribute, that can be anything at all).
  4. Now add your filename.ini file to the package. You can add it anywhere - the root, the /word path, a new folder like /word/MyIniFiles.
  5. Next, set up a relationship between your file and the document. If you don't, the file won't carry with the document. Navigate to the \word_rels\document.xml.rels file, and add under <Relationships/> something like <Relationship Id="rd1" Type="http://schemas.microsoft.com/office/2007/relationships/ini" Target="abcpy.ini" /> where the attribute Id is a unique name in your Type (best to stick with rIDX or something like that), Type reflects a URI (of just about anything) and Target is the relative path of the file you added. I added abcpy.ini to the /word folder which is in the same root as document.xml, so my relative path is just the file name. Had I added it to a folder under /word that was called myIniFiles, the Target would be myIniFiles/abcpy.ini
  6. Save everything. Rezip your file from the root and name the document back to your original name and the extention back to .docx. Copy/paste over the original file. Done.

If you have the Open XML Package Editor (part of VSTO Power Tools for VS2008 or recently released stand-alone for VS 2010), you can use VS to do some of this work for you, but it's essentially the same. Different terminology though like Target equals Name, etc.

Note that this isn't some kind of hack. Microsoft themselves use this very same technique in Office 2010 for images modified with the new "Artistic Effects" - the original image file is ported to an HDPhoto type and modified images are saved as png or jpg (and those are the ones that are displayed in the client). The hdphoto (with a .wdp extension) travels with the document, but is never actually used in display in Word, PowerPoint, etc.

Todd Main
  • 28,951
  • 11
  • 82
  • 146
  • Nice idea, but unfortunately this no longer works, if it ever did, for Excel documents. The custom information is removed when the file is saved with Excel. – Chazg76 May 23 '23 at 08:36
  • 1
    @Chazg76, it worked just fine for me based on the instructions above in Excel, including after making changes in Excel to a cell value and saving again. Only thing I did differently was in step #5 above was use an updated value as the `Type`, i.e. ``. Version: Microsoft® Excel® for Microsoft 365 (Version 2306) 64-bit. I then sent it to a different computer with Excel, opened it, changed a cell/saved. Unzipped. My .ini file is still there. – Todd Main Jul 15 '23 at 02:37
  • Thanks @ToddMain for the info. I'll give it another go and let you know how I get on. – Chazg76 Jul 25 '23 at 12:09
  • OK just tried it and it works! Apologies for my initial comment saying that it did not work! Many thanks for your help on this. – Chazg76 Jul 25 '23 at 12:35
3

Yes, this is possible. Word has a feature that allows you to embed an arbitrary XML document into your Word document. This feature is called Custom XML.

The embedded document is retained when the document is edited and can also be accessed from within the document, either programmatically using VBA, or using the Content Controls introduced with Word 2007.

Note that Word requires the embedded content to be XML, but that doesn't restrict you to pure XML data as you can use base64 encoding to embed binary content into XML.

MSDN has a sample in C# that shows you how to add a Custom XML part to a .docx file:

How to: Add Custom XML Parts to Documents Without Starting Microsoft Office

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • Didn't Microsoft have some legal problems around the technology you are describing? As far as I can remember (from somewhere), they were required to either disable it with a patch, or pay some reimbursements. Do you have any information of this kind, or am I mistaken? – Shade Jun 27 '10 at 19:00
  • 1
    @Shade: I think you're referring to http://blogs.technet.com/b/gray_knowlton/archive/2010/01/11/regarding-custom-xml-patch-distribution-and-availability.aspx. Lots of good reading from that link. – Todd Main Jun 27 '10 at 19:07
  • @razor: If you're looking for adding an XML file, this answer is the correct one. If you're looking at any other file type, see above. +1 for 0xA3. – Todd Main Jun 27 '10 at 19:19
  • 2
    @Shade: Just to make Otaku's remark a bit clearer: This way of adding custom, arbitraty XML parts to a document is *not* the same technology as the custom XML *markup* that Microsoft had to remove. It's very confusing as the names used are very similar. – Dirk Vollmar Jun 27 '10 at 21:21
  • Thanks for the clarifications. I see the difference now. – Shade Jun 28 '10 at 07:13
2

Thank you for all responses. I used custom properties to add some UserID data to generated documents, it's enough to 'trace' document and be able to import it later.

http://msdn.microsoft.com/en-us/library/dhxe2d75.aspx

razor
  • 2,727
  • 6
  • 33
  • 45
  • This is an acceptable solution. The risk is that end-users have access to this and may delete your custom properties. So, its best your code does not break if properties you use do not exist. – Phil Jun 08 '15 at 18:29
  • they couldn't just upload again document with changed values (or without them). i don't remember know how it worked. – razor Jun 09 '15 at 19:31
2

No, if you add anything to this zip file Word will start complaining about file corruption. But don't reinvent the wheel. Word documents can have multiple different document properties. Just use that. http://office.microsoft.com/en-gb/word-help/add-property-information-to-a-document-HA010163766.aspx

DmitryK
  • 5,542
  • 1
  • 22
  • 32
  • Thanks for all information ! There are very useful. For now, I've used Custom Properties to mark document with some UserID data. – razor Jun 28 '10 at 13:30
2

For the sake of being complete, you can also use the field DocVariable. This is actually a bookmark in the same way the document properties but a more open ended, at least for naming. These are best for relatively small bits of information. If you want to add a complete file, the other methods might be better.

See:

What is a DOCVARIABLE in word

Community
  • 1
  • 1
ForEachLoop
  • 2,508
  • 3
  • 18
  • 28