0

MailMerge.DataSource.LastRecord无法使用或MailMerge.DataSource.RecordCount第一个变量始终返回 -16 而第二个变量返回 -1获取最后一条记录。

4

1 回答 1

0



原因:
.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

于 2015-05-31T07:47:42.740 回答