无法获取使用MailMerge.DataSource.LastRecord
或MailMerge.DataSource.RecordCount
的最后记录,第一个变量总是返回-16,第二个变量返回-1。
原因:
.LastRacord
和.RecordCount
返回-16和-1,因为我正在从CSV文件中读取数据。
解决方案:下面的代码将返回从CSV文件或文本文件读取数据集的最后一条记录或记录计数。
public int GetMailMergeLastRecord()
{
Document doc = Globals.AddIn.ActiveDocument;
// for storing current record
int currentRec = (int)doc.Mailmerge.DataSource.ActiveRecord;
//getting the last Record
int lastRecord = 1;
doc.MailMerge.DataSource.ActiveRecord = (doc.MailMerge.DataSource.ActiveRecord - (int)doc.MailMerge.DataSource.ActiveRecord) + Int32.MaxValue;
lastRecord = (int)doc.MailMerge.DataSource.ActiveRecord;
// resetting the current record as above line of codes will change the active record to last record.
doc.MailMerge.DataSource.ActiveRecord = (doc.MailMerge.DataSource.ActiveRecord - (int)doc.MailMerge.DataSource.ActiveRecord) + currentRec;
return lastRecord;
}
注意
以上代码适用于word应用级AddIn