#1
|
|||
|
|||
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); } |
#2
|
||||
|
||||
Hi Coreysan,
What is 'idx' and happens if you delete 'order by idx'?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Word Merge from Excel not in same order!
The select statement is actually irrelevant. It exhibits the same behavior with or without the reference to "order by idx", which is column 1, in numbered order; an indexed numerical column.
I have this problem no matter what I try or don't try. Any suggestions? |
#4
|
||||
|
||||
Hi Coreysan,
Unless your code is opening a different version of the Excel file, with a different sort order, I can't see why it would generate a different result.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word mail merge from a protected Excel worksheet | Platform | Mail Merge | 10 | 09-28-2011 05:12 AM |
Merge excel info into word letter. | curatorfm | Mail Merge | 1 | 03-20-2011 04:26 AM |
Word Merge Connection to Excel failing | techexpressinc | Mail Merge | 3 | 01-26-2011 01:27 PM |
Reverse order of headings throughout Word | my_vine_figtree | Word VBA | 1 | 08-17-2010 01:46 AM |
Help with switches: Word mail merge with Excel | ks_ | Mail Merge | 0 | 12-09-2009 05:09 PM |