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)
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).
<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 attributeExtention
matches that. For theContentType
attribute, that can be anything at all).<Relationships/>
something like<Relationship Id="rd1" Type="http://schemas.microsoft.com/office/2007/relationships/ini" Target="abcpy.ini" />
where the attributeId
is a unique name in yourType
(best to stick with rIDX or something like that),Type
reflects a URI (of just about anything) andTarget
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, theTarget
would be myIniFiles/abcpy.iniIf 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
equalsName
, 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.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
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:
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
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