无法使用MailMerge.DataSource.LastRecord或MailMerge.DataSource.RecordCount获得最后记录,第一个变量总是返回-16,第二个变量返回-1。
发布于 2015-05-31 07:47:42
事业:
.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;
}Note
以上代码用于word应用程序级别的AddIn。
https://stackoverflow.com/questions/30555065
复制相似问题