C#-MS Word-MailMerge如何在MailMerge.DataSource.LastRecord返回-16时获取最后一条记录

C#-MS Word-MailMerge如何在MailMerge.DataSource.LastRecord返回-16时获取最后一条记录,c#,ms-word,mailmerge,record-count,C#,Ms Word,Mailmerge,Record Count,无法使用MailMerge.DataSource.LastRecord或MailMerge.DataSource.RecordCount获取最后一条记录第一个变量始终返回-16,第二个变量返回-1。原因: .LastRacord和.RecordCount返回-16和-1,因为我正在从CSV文件读取数据。 解决方案: 以下代码将返回CSV文件或文本文件中读取数据集的最后一条记录或记录计数 public int GetMailMergeLastRecord() { Document

无法使用
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应用程序级加载项