Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

.net - Is it possible to add some data to a Word document?

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_relsdocument.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...