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

Free Support Forum - aspose.com

IMailMergeDataSource.getValue() Never Triggered

I am trying to run a mail merge with regions using a custom data source. I put some debug code into my class that implements IMailMergeDataSource to track what method is being run and getValue is never called. Here’s everything that gets called:

DEBUG 27 Jun 2011 10:30:04 com.goengineer.agile.CustomMailMergeDataSource:: Table Records to process: 6
DEBUG 27 Jun 2011 10:30:28 com.goengineer.agile.CustomMailMergeDataSource:: Returning table name: Workflow
DEBUG 27 Jun 2011 10:30:28 com.goengineer.agile.CustomMailMergeDataSource:: Move Next called. Before call record index: -1. Table size: 6


It’s just these three methods: instantiation, getTableName(), and moveNext(). Why would getValue not be called?

Here’s the code for the custom class and I have attached the Word template:

public class CustomMailMergeDataSource implements IMailMergeDataSource {
private Logger log = Logger.getLogger(CustomMailMergeDataSource.class);
private String tableName;
private int mRecordIndex;
private ArrayList<HashMap<String,String>> mTableRecords;

/*
* tableName = The name of the region in Word for the merge.
*/
public CustomMailMergeDataSource(ArrayList<HashMap<String,String>> tableRecords, String tableName) {
this.mTableRecords = tableRecords;
this.tableName = tableName;
this.mRecordIndex = -1;

log.debug("Table Records to process: " + this.mTableRecords.size());
}

public String getTableName() throws Exception {
log.debug("Returning table name: " + this.tableName);
return this.tableName;
}

public boolean getValue(String fieldName, Object[] fieldValue) throws Exception {
log.debug("Mail Merge getting field: " + fieldName);
HashMap<String,String> temp = this.mTableRecords.get(this.mRecordIndex);
if(!temp.containsKey(fieldName)) {
fieldValue[0] = null;
return false;
}

fieldValue[0] = temp.get(fieldName);
return true;
}

public boolean moveNext() throws Exception {
log.debug("Move Next called. Before call record index: " + this.mRecordIndex + ". Table size: " + mTableRecords.size());
if (mRecordIndex >= mTableRecords.size()) {
return false;
}

mRecordIndex++;

return mRecordIndex >= mTableRecords.size();
}

public IMailMergeDataSource getChildDataSource(String tableName) throws Exception {
return null;
}

}



Hi

Thanks for your request. The problem is in your implementation of moveNext method. Please see the following code, as you can see your condition returns false in case if index of record is less than items count. But it should return true.

public boolean moveNext() throws Exception {

log.debug("Move Next called. Before call record index: " + this.mRecordIndex + ". Table size: " + mTableRecords.size());

if (mRecordIndex >= mTableRecords.size()) {

return false;

}

mRecordIndex++;

return mRecordIndex >= mTableRecords.size();

}

You should modify your code as shown below:

public boolean moveNext() throws Exception {

log.debug("Move Next called. Before call record index: " + this.mRecordIndex + ". Table size: " + mTableRecords.size());

if (mRecordIndex >= mTableRecords.size()) {

return false;

}

mRecordIndex++;

return mRecordIndex < mTableRecords.size();

}

Best regards,

Ah, perfect! Thanks for the second set of eyes.