1

I am trying to figure out how to properly add custom properties to .docx files programmatically in ruby. I also have an office-js add in which reads these custom properties - but more on that later.

Here are the steps I am currently taking to add custom properties to a .docx file in ruby:

  1. Opening the document using the Docx gem and reading the contents of the archive using Docx:Document.open(filepath).zip
  2. Adding in a docProps/custom.xml file with the custom property elements which I want eg:
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="matter.id">
    <vt:lpwstr>518</vt:lpwstr>
</property>
  1. Adding the following Override element to the [Content_Types].xml file:
<Override PartName="/docProps/custom.xml" ContentType="application/vnd.openxmlformats-officedocument.custom-properties+xml"/>
  1. Adding the following Relationship element to the _rels/.rels file:
<Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" Target="docProps/custom.xml"/>

Note: The Id attribute value is worked out by the number of <Relationship> elements present.

After these steps: After running this code over a blank / newly created document I can open the file in Word on my computer and my office-js add in can successfully read the custom properties which I have set in my ruby application.

The issue: When I upload & open the document in Word Online my office-js add in fails to recognised the custom properties which have been set.

My office-js add in reads and write the custom properties using Microsoft's office-js apis and when setting the properties this way my add in is able to recognise them in both Word and Word Online so I am pretty sure I'm missing something when trying to set the properties manually in my ruby function.

I've tried to compare the archives of both a .docx file which has had the custom properties set by my ruby function and the same file with the custom properties set using my add in and I just can't see any noticeable differences which would be causing Word Online to fail to recognise the custom properties set using my ruby code.

Unfortunately there doesn't seem to be any ruby gems which would allow me to edit the custom properties on a docx file like with the office-js apis and that is why I have resorted to doing it manually.

braX
  • 11,506
  • 5
  • 20
  • 33
FrostyOnion
  • 856
  • 7
  • 10
  • Please provide some details of "fails to recognize": what's your code and what goes wrong and when? Also, try experiment: Save 2 copies of a file A and B. Add the props programmatically to A. Open both as zip files. Use copy-paste to make B identical to A. Then see if Word Online will read the properties in B. – Rick Kirkham Mar 06 '20 at 17:33
  • In terms of experimenting I have attempted this as you have mentioned but haven't quite nailed down what it is I need to add or modify when using my ruby function. There are a few differences which I have identified and copy pasted but can't seem to get it to work thought this method. Have a feeling it might be something to do with the /word/webextensions folder which is present in my file edited by the office-js add in but I am not really sure what the details inside this folder are for – FrostyOnion Mar 07 '20 at 07:21
  • Try using the Open XML SDK Productivity Tool (it's a free download). It has a Compare feature that will show differences in the Word Open XML between two files. Open the "bad" file, then use Compare on the "good" file and see how they differ. You mention the relationships, so you know they could be a potential issue - take a careful look at that. It might be safer to define a custom ID value, rather than trying to use the pattern Word uses that *could* result in a conflict. Especially if you can't find any other difference. – Cindy Meister Mar 08 '20 at 11:53
  • Thanks @CindyMeister ,handy tool! There are a couple of differences between the 2 docs and I have tried to copy paste from one to another but still can't get it working. The main differences are a `w:proofstate` in and an extra `w:rsid` in `/word/settings.xml`, and also the "good" file has a word/webextensions folder with `/_rels/taskpanes.xml.rels`, `taskpanes.xml` and `webextension1.xml`. Nothing too interesting in the first 2 files that copy pasting shouldn't cover but in `webextenions1.xml` there is an id in both `we:webextension` and `we:reference` which might be causing me trouble? – FrostyOnion Mar 08 '20 at 14:05

0 Answers0