简体   繁体   English

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

[英]C# - MS Word - MailMerge how to get Last Record when MailMerge.DataSource.LastRecord returns -16

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



Cause: 原因:
.LastRacord and .RecordCount was returning -16 and -1 because I was reading data from the CSV file. .LastRacord.RecordCount返回-16和-1,因为我正在从CSV文件读取数据。

Solution: The following code will return the last record or record count reading dataset from CSV file or text file. 解决方案:以下代码将从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;
}

Note 注意
The above code is for word application level AddIn 上面的代码是针对Word应用程序级别的AddIn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM