View Single Post
 
Old 12-09-2011, 04:32 PM
Coreysan Coreysan is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Dec 2011
Posts: 10
Coreysan is on a distinguished road
Default Word Merge from Excel not in same order!

I have an excel file with 10 records in sorted order. When I perform a Word merge manually (using the Word ribbon) then the output in Word shows all the 10 pages in exactly the same order as the records in Excel.
So, if excel has records 1-10, then the Word doc shows pages 1-10 in the same exact order.

However, if I perform the merge programmatically in C#, then the Word pages are not in the same order. So, if Excel shows records 1-10, the Word doc might show page 4, then page 9, then page 2, etc.

How can I programmatically get the Word doc pages in the exact same order, so that when I print, they print out in the same order as the excel records are in?

Here's some code I use:

publicvoid Open_Word_File(string wdoctemplatefile2)
{
wrdApp =
new Microsoft.Office.Interop.Word.Application();

wrdApp.Visible =
false;
oTempFilePath = wdoctemplatefile2;
oMissing = System.Reflection.
Missing.Value;
oFalse =
false;
//Open the Word Template
wrdDoc = wrdApp.Documents.Open(ref oTempFilePath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
wrdDoc.Select();
}


publicvoid Open_Excel_File(string wfilename)
{
//Word.MailMerge wrdMailMerge = wrdDoc.MailMerge;
wrdMailMerge = wrdDoc.MailMerge;
object mysql = "select * from [sheet1$] order by idx";
wrdMailMerge.OpenDataSource(wfilename, oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref mysql, ref oMissing, ref oMissing, ref oMissing);
}

publicvoid Perform_Word_Merge()
{
// Perform mail merge.
wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
wrdMailMerge.Execute(
ref oFalse);
}
Reply With Quote