Save each page/record of a Mail Merge as its own document?

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1489 Views - Last Post: 02 December 2013 - 11:01 AM Rate Topic: -----

#16 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3650
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: Save each page/record of a Mail Merge as its own document?

Posted 27 November 2013 - 08:22 AM

I'll ask a silly question, and I'll probably get a silly answer. Does your output have to have separate Word documents for each mail merge record, or can you get by with a separate PDF for each mail merge record? It seems to me that it would be easier to let the mail merge print to a .PDF file, and then use a library that knows how to read and manipulate .PDFs to split up the pages afterwards.
Was This Post Helpful? 0
  • +
  • -

#17 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 223
  • View blog
  • Posts: 1,035
  • Joined: 25-June 12

Re: Save each page/record of a Mail Merge as its own document?

Posted 27 November 2013 - 09:03 AM

View PostSkydiver, on 27 November 2013 - 03:22 PM, said:

I'll ask a silly question, and I'll probably get a silly answer. Does your output have to have separate Word documents for each mail merge record, or can you get by with a separate PDF for each mail merge record? It seems to me that it would be easier to let the mail merge print to a .PDF file, and then use a library that knows how to read and manipulate .PDFs to split up the pages afterwards.


If it were any other project I would say...... Yes?

This however is one (for other business reasons above my pay grade) where each record (page) of the Mail Merge Word Document needs to be saved as its own individual Word Doc with a unique name for archiving/indexing. This set-up I believe carries over from old VB6 programs which function in a similar fashion for output. These programs however do not build the data in the same manner that I have done in C#. If I recall correctly, and in loose explanation, the VB6 programs keep the records in a form of collections where they fill the Mail Merge document 1 record at a time, saving each record before processing the next.

This is very similar to what Andrew originally suggested:

Quote

One option to mention, and quickly dismiss, is to perform the merge multiple times with a single record. This will be extremely inefficient, even more so from C#.


This could I believe be done for this project, but would indeed be quite inefficient...
Was This Post Helpful? 0
  • +
  • -

#18 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3650
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: Save each page/record of a Mail Merge as its own document?

Posted 27 November 2013 - 09:21 AM

So what was the point of moving to C#? Just a straight port? Or try to get improvements in the process? It would be worth the time to ask what happens to the word documents afterwards. Why does the business process have to have a Word document? If the business process was willing to move forward to a newer version of a programming language, and a newer version of Word, why does the output still have to be a Word document? Why does the process not break when a newer version of Word is used, but breaks when a PDF is used instead?

(I'm sorry if the questions above come across as pushy, but I got the same set of questions from my boss a few weeks ago where I was just trying to keep my head above water and comply with a business requirement that our "client" needed, and it was requiring a lot of time and complexity to comply with it, but it was unclear as to why the requirement existed. Yes, I know that it's hard to step back and try to get a bigger picture of what is above your pay grade, but it's that stepping back that also shows that you are making steps towards advancement in your career.)
Was This Post Helpful? 0
  • +
  • -

#19 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 223
  • View blog
  • Posts: 1,035
  • Joined: 25-June 12

Re: Save each page/record of a Mail Merge as its own document?

Posted 27 November 2013 - 09:52 AM

View PostSkydiver, on 27 November 2013 - 04:21 PM, said:

(I'm sorry if the questions above come across as pushy...


No no, I understand, not pushy at all. This is a NEW project that is doing things SIMILAR to several old projects. When these correspondence letters generated from the Mail Merge process, they need to be saved individually into our Member Records File System, called NetFYI, to reference when they were created and mailed out. All the similar documents I've looked over have been saved into NetFYI as a Word Document, so that is what I believed the final product needed to be.

I just queried my Manager on the possibility of using .PDF's as the end result, but while he himself does not agree with using Word Docs, "The standard is to save them as .doc at this time. I don't agree with that but we will not be changing it at this time."

Basically it looks like this is a case of "we've always followed this norm...". So yea, I'm trying to save each page of a word document as it's own word document... :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

#20 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: Save each page/record of a Mail Merge as its own document?

Posted 27 November 2013 - 11:57 AM

You need to work on your debugging skills ;)

Anyway, as part of the debugging process..

oNewWord.Activedocument.Range().FormattedText = rng.FormattedText;

Split this into two lines, declaring a Word.Range object. This will narrow down the issue to either Range or FormattedText.

Try with .Text (on each side) rather than FormattedText.

You already have oNewWrdDoc defined. If this is where you are attempting to copy the text to then try:
oNewWrdDoc.Range()    // etc..

You might re-examine my previous advice that you may need to explicitly state a Range as
Range(0, oMissing)
in C#. (Possibly not, but its worth checking again.)

Take my tutorial: step through the code.
Explore some things directly from Word VBA.

(I'm repeating myself.. :) )

Good luck.
Was This Post Helpful? 0
  • +
  • -

#21 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 223
  • View blog
  • Posts: 1,035
  • Joined: 25-June 12

Re: Save each page/record of a Mail Merge as its own document?

Posted 02 December 2013 - 08:28 AM

I'm getting closer! :clap:

I'm now able to copy all text in a specified range (well, page 1 for now during testing) but am losing alignment-formatting as well as not copying the Signature Image in the document.

Template:
Posted Image

Processed Mail Merge / New Individual Document for Saving:
Posted Image

newWrdRng.Text = rng.Text; near the end is where I'm at currently.

CODE:
public void OpenAndReview()
        {
            try
            {
                string docSave = @"C:\Users\NAME\Desktop\Test.doc";

            //MergeDocLibrary mdl = new MergeDocLibrary();
            //mdl.mergeDocument(docSource, docLoc);

            // Original Mail Merge Document
            Word.Range rng;
            object start = 0;
            object end = 0;
            Word.Range newWrdRng;
            Word.Application oWord = new Word.Application();
            Word.Document oWrdDoc = new Word.Document();

            // New Document Instance
            Word.Application oNewWord = new Word.Application();
            Word.Document oNewWrdDoc = new Word.Document();

            // Set 'False' in PROD, 'True' in DEV
            oWord.Visible = true;
            oNewWord.Visible = true;
            Object oTemplatePath = docLoc;

            // Open Mail Merge Doc
            oWrdDoc = oWord.Documents.Open(oTemplatePath);

            // Open New Document
            oNewWrdDoc = oNewWord.Documents.Open(docSave);
            Object oMissing = System.Reflection.Missing.Value;

            // Open Mail Merge Datasource
            oWrdDoc.MailMerge.OpenDataSource(docSource, oMissing, oMissing, oMissing,
               oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

            MessageBox.Show(oWord.Activedocument.Name.ToString());
            MessageBox.Show(oWord.Activedocument.Sections.Count.ToString());
            
            // Execute Mail Merge
            oWrdDoc.MailMerge.Execute();
            
            // Set Mail Merge Document as Active Doc
            //oWrdDoc.Activate();

            MessageBox.Show(oWord.Activedocument.Name.ToString());
            MessageBox.Show(oWord.Activedocument.Sections.Count.ToString());
            
            // Select Section 1 of Mail Merge Doc?
            rng = oWord.Activedocument.Sections[1].Range;

            // Copy text from 1 word doc to another
            oNewWrdDoc.Activate();
            newWrdRng = oNewWrdDoc.Range(0, 0);
            newWrdRng.Text = rng.Text;

            // .............

            // Save new docuemnt...?
            oNewWrdDoc.SaveAs2("SuccesfullySavedTest.doc");

        }
        catch (Exception ex)
        {
            MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
        }
        finally
        {
            //
        }
    }


Was This Post Helpful? 0
  • +
  • -

#22 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 223
  • View blog
  • Posts: 1,035
  • Joined: 25-June 12

Re: Save each page/record of a Mail Merge as its own document?

Posted 02 December 2013 - 09:34 AM

Got the Copy/Paste solved, now I just need to figure out how to clear out my "New" word document after saving so it is ready to process the next Mail Merge Section in my next loop iteration.

My current code is not clearing out the document, meaning that with each new iteration the new word document contains the previous processed section(s) + the new section that is supposed to be processed.

Rough Outline:

Doc 1: Record 1 + an extra blank page (unsure why?)

Doc 2: Record 2 + Record 1 + an extra blank page (unsure why?)

Doc 3: REcord 3 + Record 2 + Record 1 + an extra blank page (unsure why?)

public void OpenAndReview()
        {
            try
            {
                string docSave = @"C:\Users\NAME\Desktop\Test.doc";

                //MergeDocLibrary mdl = new MergeDocLibrary();
                //mdl.mergeDocument(docSource, docLoc);

                // Original Mail Merge Document
                Word.Range rng;
                object start = 0;
                object end = 0;
                Word.Range newWrdRng;
                Word.Application oWord = new Word.Application();
                Word.Document oWrdDoc = new Word.Document();

                // New Document Instance
                Word.Application oNewWord = new Word.Application();
                Word.Document oNewWrdDoc = new Word.Document();

                // Set 'False' in PROD, 'True' in DEV
                oWord.Visible = true;
                oNewWord.Visible = true;
                Object oTemplatePath = docLoc;

                // Open Mail Merge Doc
                oWrdDoc = oWord.Documents.Open(oTemplatePath);

                // Open New Document
                oNewWrdDoc = oNewWord.Documents.Open(docSave);
                Object oMissing = System.Reflection.Missing.Value;

                // Open Mail Merge Datasource
                oWrdDoc.MailMerge.OpenDataSource(docSource, oMissing, oMissing, oMissing,
                   oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

                //MessageBox.Show(oWord.Activedocument.Name.ToString());
                //MessageBox.Show(oWord.Activedocument.Sections.Count.ToString());
                
                // Execute Mail Merge
                oWrdDoc.MailMerge.Execute();
                
                // Set Mail Merge Document as Active Doc
                //oWrdDoc.Activate();

                //MessageBox.Show(oWord.Activedocument.Name.ToString());
                //MessageBox.Show(oWord.Activedocument.Sections.Count.ToString());


                int docCnt = oWord.Activedocument.Sections.Count - 1;
                int cnt = 0;
                while (cnt != docCnt)
                {
                    cnt++;
                    // Copy Desired Section from Mail Merge
                    oWord.Activedocument.Sections[cnt].Range.Copy();
                    // Set focus to the New Word Doc instance
                    oNewWord.Activate();
                    // Paste copied range to New Word Doc
                    oNewWord.Activedocument.Range(0, 0).Paste();
                    // Save New Word Doc
                    oNewWord.Activedocument.SaveAs2(@"C:\Users\NAME\Desktop\SuccesfullySavedDoc-" + cnt + ".doc");

                    // Clear New Word Doc 
oNewWord.Activedocument.Content.Select();
        oNewWord.Selection.TypeBackspace();

                    // Set Mail Merge as Active Document
                    oWord.Activate();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
            }
            finally
            {
                //
            }
        }



I would think clearing out the Word Document would be an easy thing, just haven't found it yet :sweatdrop:

EDIT: Got the document clearing out. Now just need to figure out why I'm getting an extra blank page in each document after Paste().

This post has been edited by AnalyticLunatic: 02 December 2013 - 10:01 AM

Was This Post Helpful? 0
  • +
  • -

#23 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: Save each page/record of a Mail Merge as its own document?

Posted 02 December 2013 - 11:01 AM

Is the extra page caused by:

  • Copying across of the next section break from the original merged document
  • An extra paragraph mark
  • The margins of the new document being different from those of the original document.

Once you've determined this it sounds like you'll have to remove the extra paragraph or section break after copying across, or change the margin settings for the new document-template that you are using.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2