We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

MailMergeSettings.setDataSource keeps a reference to the PREVIOUS data source

MailMergeSettings.setDataSource() on the face of it works.

Using the example below, it seemingly switches the document’s datasource from old-datasource to new-datasource (e.g. in Word, mailmerged document gives different output, the list of mailmerge fields changes).

However, (in Word 2007), there is still a reference to the old datasource:-

  • Open test-out.doc
  • Click on mailings->Start Mail Merge-> Step by Step mail merge wizard.
  • On the window that opens to the right, there is text that says "Currently, your recipients are selected from “old-datasource”.
  • If you choose to “select a different list”, the displayed text changes to something like:-
    [......\new-datasource.doc] in new-datasource.doc

Curious, I converted the output document to .docx, and unzipped the contents. The following file has a reference to BOTH datasources:-

\word\_rels\settings.xml.rels

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<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:///C:\data\java\MaggieMM\new-datasource.doc" TargetMode="External"/>
</Relationships>
import com.aspose.words.License;
import com.aspose.words.Document;
import com.aspose.words.MailMergeSettings;
import com.aspose.words.MailMergeMainDocumentType;

//*************************************************************************************
public class Test
{
   //*************************************************************************************
   public static void main (String args[]) throws Exception {


 License license = new License();
    license.setLicense("Aspose.Words.Java.lic");

 System.out.println ("Opening test-in.doc");
    Document doc = new Document ("test-in.doc");

    MailMergeSettings mms = doc.getMailMergeSettings();
    mms.setMainDocumentType (MailMergeMainDocumentType.FORM_LETTERS);

    System.out.println ("OLD Datasource : "+mms.getDataSource()+mms.getHeaderSource());

    String newDataSource = System.getProperty ("user.dir")+"\\new-datasource.doc";
    mms.setDataSource (newDataSource);
    System.out.println ("NEW Datasource : "+mms.getDataSource());
 doc.setMailMergeSettings(mms);

 System.out.println ("Saving test-out.doc");
    doc.save ("test-out.doc");

   }
   //*************************************************************************************
}

Hi

Thanks for your request. Maybe in your case you should simply clear mail merge settings before setting new data source:
https://reference.aspose.com/words/java/com.aspose.words/mailmergesettings#clear()

hope this helps.

Best regards,

I tried that, and got the same results.

I noticed that even after clear() was called, the datasource was still set, as seen in the output:-

Opening test-in.doc
clearing mailmerge settings
OLD Datasource : C:\Data\Java\MaggieMM\old-datasource.doc
NEW Datasource : C:\Data\Java\MaggieMM\new-datasource.doc
Saving test-out.doc

import com.aspose.words.License;
import com.aspose.words.Document;
import com.aspose.words.MailMergeSettings;
import com.aspose.words.MailMergeMainDocumentType;
import com.aspose.words.MailMergeDataType;

//*************************************************************************************
public class Test
{
    //*************************************************************************************
    public static void main (String args[]) throws Exception {

        License license = new License();
        license.setLicense("Aspose.Words.Java.lic");

        System.out.println ("Opening test-in.doc");
        Document doc = new Document ("test-in.doc");

        MailMergeSettings mms = doc.getMailMergeSettings();
        System.out.println ("clearing mailmerge settings");
        mms.clear();
        mms.setDataType (MailMergeDataType.TEXT_FILE);
        mms.setMainDocumentType (MailMergeMainDocumentType.FORM_LETTERS);

        System.out.println ("OLD Datasource : "+mms.getDataSource());

        String newDataSource = System.getProperty ("user.dir")+"\\new-datasource.doc";
        mms.setDataSource (newDataSource);
        System.out.println ("NEW Datasource : "+mms.getDataSource());
        doc.setMailMergeSettings(mms);

        System.out.println ("Saving test-out.doc");
        doc.save ("test-out.doc");

    }
//*************************************************************************************
}

Hi

Thank you for additional information. Probably, what you need is simply reset Query as shown below:

String newDataSource = "C:\\Temp\\new-datasource.doc";
mms.setDataSource(newDataSource);
mms.setQuery("SELECT * FROM " + newDataSource);

Best regards,

No, I’m afraid that didn’t work.

Just to reiterate - the change of datasource is MOSTLY working, as seen by the attached image:-

  • Working - mailmerged fields say “New X” etc.
  • Working - The list of MM fields includes Field_4 - which is in the new datasource only.
  • Not working - there is a reference to the old mailmerge, highlighted in the attached image.

Thanks, Jonathan.

Hi

Thank you for additional information. It seems MS Word takes this information from Odso. So you should simply set data source in Odso as shown below:

mms.setDataSource(newDataSource);
mms.setQuery("SELECT * FROM " + newDataSource);
mms.getOdso().setDataSource(newDataSource);

Hope this helps.

Best regards,

This now works!

Thanks, Jonathan.