簡體   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。



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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM