无法使用MailMerge.DataSource.LastRecord
或MailMerge.DataSource.RecordCount
获取最后一条记录,第一个变量始终返回-16,第二个变量返回-1。
答案 0 :(得分:0)
的原因:强>
由于我正在从CSV文件中读取数据,因此.LastRacord
和.RecordCount
返回-16和-1。
的解决方案:强>
以下代码将返回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;
}
注意强>
上面的代码用于单词应用程序级别AddIn