10

I just followed approach No 2 in the VariableReplace example from docx4j 2.8.1 and everything it does, is to remove the variable markers ${}.

The steps I did:

  • Opened Word 2013, typed ${variable} as text only
  • Saved it to somewhere
  • read it in my Java program and build my HashMap with .put("variable", "TEST");
  • other code is copied and pasted from the example above.
  • Saved the document

I'd expect 'TEST' solely, and get just 'variable' without the markers in the output document.

Martin Dames
  • 264
  • 3
  • 13

3 Answers3

28

No doubt Word is splitting your "variable" across runs, with grammar or spelling flags.

Fix it up with VariablePrepare

Put this line in after you instantiate the WordprocessingMLPackage:

VariablePrepare.prepare(wordMLPackage);

Then you can use your mappings to replace the variables.

Eric Brown
  • 13,774
  • 7
  • 30
  • 71
JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • This is the correct answer. I had the same problem as Martin and I spent over an hour for such a simple thing :( – Math Jan 30 '14 at 20:25
  • This was a life saver! – SimonV May 21 '15 at 07:19
  • Hi JasonPlutext why I'm not able to add the runs(R) inside content control using variable prepare?. Is that possible to do using that class. if yes how to do so, If not what will be your idea to do that. – santhanam Aug 25 '16 at 09:29
  • 1
    there's a broken link to VariablePrepare - I found this one instead https://www.javatips.net/api/docx4j-master/src/main/java/org/docx4j/model/datastorage/migration/VariablePrepare.java – MrTelly Aug 03 '19 at 05:08
2

I realize this is an old post, but for others that stumble onto this, another reason you can get this result is if you have incorrect "keys" in your HashMap. So in my case, I was using my old xml format as the key like

.put("<variable/>","TEST");

when I should have been using:

.put("variable","TEST");

The document itself was using tags like

${variable}

The VariableReplace code will remove the ${} formatting whether a match is found or not. So if it is not finding a match, then the keys might not match the ones in the document for some reason, and this might not strictly be related to VariablePrepare. But this was a very helpful post for me since the VariablePrepare, VariableReplace solution is now working for my purposes.

Also, I am not sure that even VariablePrepare can handle the case where you change the font, highlighting or other formatting in the middle of your tag in the document. In such cases, it will not be able to merge the tag into a single run, and so tag recognition will likely fail.

Ted
  • 3,212
  • 25
  • 20
0

The main reason why variables cannot be replaced is: using plain text like ${name} instead MERGEFIELD field type. Here is the link how to add MERGEFIELD into document - https://www.systemonesoftware.com/en/support/article/38-merge-fields-in-word-for-windows

Also you can use docx-stamper with SpEL - https://github.com/thombergs/docx-stamper

Orkhan Hasanli
  • 651
  • 13
  • 22