Question

I'm developing a feature with which users can upload docx-documents with placeholders. These placeholders will later be replaced with data from a database. In order to assure that the user only uses predefined placeholders, he has to select a Ms Access file (.mdb) whose defined fields (mergefield) are valid placeholders. I succeed in replacing the mergefields with my data. However, I'm not able to get rid of the defined relationship to the Access file. I know that the following has to be removed from the xml of my docx-document:

<pkg:part pkg:name="/word/_rels/settings.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml"><pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/recipientData" Target="recipientData.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/mailMergeSource" Target="file:///D:\Downloads\Lager.mdb" TargetMode="External"/></Relationships></pkg:xmlData></pkg:part>


<w:mailMerge><w:mainDocumentType w:val="formLetters"/><w:linkToQuery/><w:dataType w:val="native"/><w:connectString w:val="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=P:\SINA\Projektteam\Referat413\Basisklassen fuer Schablonen\Lager.mdb;Mode=Read;Extended Properties=&quot;&quot;;Jet OLEDB:System database=&quot;&quot;;Jet OLEDB:Registry Path=&quot;&quot;;Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password=&quot;&quot;;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False"/><w:query w:val="SELECT * FROM `Office Address List` "/><w:odso><w:udl w:val="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=D:\Benutzer-Profile\boehmv\Downloads\Lager.mdb;Mode=Read;Extended Properties=&quot;&quot;;Jet OLEDB:System database=&quot;&quot;;Jet OLEDB:Registry Path=&quot;&quot;;Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password=&quot;&quot;;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo Validation=False"/>
<w:table w:val="Office Address List"/>
<w:src r:id="rId1"/><w:colDelim w:val="9"/><w:type w:val="addressBook"/><w:fHdr/><w:fieldMapData><w:column w:val="0"/><w:lid w:val="de-DE"/></w:fieldMapData><w:fieldMapData><w:column w:val="0"/><w:lid w:val="de-DE"/></w:fieldMapData><w:fieldMapData><w:column w:val="0"/><w:lid w:val="de-DE"/></w:fieldMapData><w:fieldMapData><w:column w:val="0"/><w:lid w:val="de-DE"/></w:fieldMapData><w:fieldMapData><w:column w:val="0"/><w:lid w:val="de-DE"/></w:fieldMapData><w:recipientData r:id="rId2"/></w:odso>
</w:mailMerge>

Any idea how to solve this with docx4j? I found the w:mailMerge-tag with the following approach:

String xml = XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getDocumentSettingsPart().getJaxbElement(), true, true);

Unfortunately, I'm not able to change the DocumentSettingsPart.

Was it helpful?

Solution

I've finally found a way which works for me. The following method removes the mailmerge settings:

private void removeMailmergeRelationship(WordprocessingMLPackage wordMLPackage) {
    DocumentSettingsPart dsp = wordMLPackage.getMainDocumentPart().getDocumentSettingsPart();
    CTSettings settings = dsp.getJaxbElement();
    settings.setMailMerge(null);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top