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

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

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

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

Posted 26 November 2013 - 12:52 PM

I've written the following code allowing users to query data based on word document that will be printed and the data source (different systems), where they can choose SOURCE1, SOURCE2, or BOTH. The data is queried, placed into an ODBC DataReader and then looped through to fill a ListView control.

I then allow users to select which records they want to print, and those records are processed into the selected word document as a Mail Merge operation.

What I need to do now is save each individual word document page (each record of the mail merge) to a specific folder location with its own name. Does anyone have any thoughts on how to go about this? I've found nothing so far but references to a third-party plug-in download.

Build Query:
    private void btnSearch_Click(object sender, EventArgs e)
            {
                lvData.Clear();
                string qryCmd = "";
    
                createColumns();
    
                switch (cmbLetterType.SelectedIndex)
                {
                    case 0:
                        qryCmd = chkSystem("oldAddr");
                        break;
                    case 1:
                        qryCmd = chkSystem("newAddr");
                        break;
                    case 2:
                        qryCmd = chkSystem("nameChg");
                        break;
                }
    
                // If the option for BOTH is selected, qryCmd is a long string containing the individual queries for SOURCE1 and SOURCE2.
                // Here I check if a semicolon (;)/>/>/>/> exists, denoting that there are 2 cmd queries in the string.
                // If so, I split the query calling GetiSeriesData() with the first query string, then assigning qryCmd the second 
                // strings value and continuing processing as normal.
                if (qryCmd.Contains(";"))
                {
                    char[] delimitChar = { ';' };
                    string[] splitQueries = qryCmd.Split(delimitChar);
                    qryCmd = splitQueries[0];
                    GetiSeriesData(qryCmd);
                    qryCmd = splitQueries[1];
                }
    
                GetiSeriesData(qryCmd);
    
                // Display message if no records found.
                if (lvData.Items.Count == 0)
                {
                    MessageBox.Show("No records found.");
                }
                lblRecCnt.Text = lvData.Items.Count.ToString();
                this.lvData.CheckBoxes = true;
             }


-------------------------------------------------------------------------------------------
Get Data, Fill ListView
    public void GetiSeriesData(string query)
            {
                OdbcDataReader dr;
                try
                {
                    //Set value of system based on table being looked at in ConnectionString
                    string sysValue = "";
                    if (query.Contains("lib1"))
                    {
                        sysValue = "P";
                    }
                    if (query.Contains("lib2"))
                    {
                        sysValue = "N";
                    }
    
                    MergeDocLibrary mdl = new MergeDocLibrary();
                    dr = mdl.GetData(query);
    
                    Int16 x = 0;
                    string gndr = "";
    
                    // Fill ListView Control lvData
                    while (dr.Read())
                    {
                        lvData.Items.Add(dr["MEMNO"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["NAME"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["ADDR1"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["ADDR2"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["CITY"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["STATE"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["ZIP"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["OLD_ADDR1"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["OLD_ADDR2"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["OLD_CITY"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["OLD_STATE"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["OLD_ZIP"].ToString().Trim());
    
                        lvData.Items[x].SubItems.Add(sysValue.ToString().Trim());
                        lvData.Items[x].SubItems.Add(DateTime.Today.ToString("d"));
                        lvData.Items[x].SubItems.Add(dr["SEX"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["LNAME"].ToString().Trim());
                        if (dr["SEX"].ToString().Trim() == "M")
                        {
                            gndr = "Mr.";
                        }
                        else // (dr["SEX"].ToString().Trim() == "F)
                        {
                            gndr = "Ms.";
                        }
                        lvData.Items[x].SubItems.Add(gndr + dr["LNAME"].ToString().Trim());
                        lvData.Items[x].SubItems.Add(dr["CITY"].ToString().Trim() + ", " + dr["STATE"].ToString().Trim() + " " + dr["ZIP"].ToString().Trim());
                        x += 1;
                    }
    
                    mdl.closeConn();
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
                }
                finally
                {
    
                }
            }

------------------------------------------------------------------------------------------------------
Mail Merge Operation:
public void Print()
        {
            try
            {
                //MergeDocLibrary mdl = new MergeDocLibrary();
                //mdl.mergeDocument(docSource, docLoc);
                Word.Application oWord = new Word.Application();
                Word.Document oWrdDoc = new Word.Document();
                // Set 'False' in PROD, 'True' in DEV
                oWord.Visible = true;
                Object oTemplatePath = docLoc;
                oWrdDoc = oWord.Documents.Open(oTemplatePath);
                Object oMissing = System.Reflection.Missing.Value;
                oWrdDoc.MailMerge.OpenDataSource(docSource, oMissing, oMissing, oMissing,
                   oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
                oWrdDoc.MailMerge.Execute();

                // SAVE EACH RECORD AS IT's OWN DOC.... //
            }
            catch (Exception ex)
            {
                MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
            }
            finally
            {
                //
            }
        }



Is This A Good Question/Topic? 0
  • +

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

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,393
  • Joined: 12-December 12

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

Posted 26 November 2013 - 01:22 PM

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#.

A mail-merge document is a single document with multiple section breaks. You can loop through all the Sections, copying the text to a new document, saving and closing this document each time.

Sub GetIt()
    Dim rng As Word.Range
    
    Set rng = Activedocument.Sections(1).Range
    Documents.Add
    Activedocument.Range.FormattedText = rng.FormattedText
End Sub

You'll probably find that it copies the section break as well, so each document except the last will have an additional blank page. I'll leave you to remove this ;)

BTW (as I've mentioned before) run and test code (and record macros) in Word first, before attempting to use it in C#

BTWW Don't call your function Print() :whistling: it will only cause confusion later.

This post has been edited by andrewsw: 26 November 2013 - 01:22 PM

Was This Post Helpful? 0
  • +
  • -

#3 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

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

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

Posted 26 November 2013 - 01:46 PM

Ok, here's my new 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;
                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);
               
                // Execute Mail Merge
                oWrdDoc.MailMerge.Execute();

                // Set Mail Merge Document as Active Doc
                oWrdDoc.Activate();


                // ERROR
                // Select Section 1 of Mail Merge Doc?
                rng = oWord.Activedocument.Sections[1].Range;

                // Place selected text into the new Document???
                oNewWord.Activedocument.Range().FormattedText = rng.FormattedText;

                // 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
            {
                //
            }
        }



When it reaches the line that I'm assigning 'rng' it's value, I receive:

Source:		Microsoft Word
Message:	Type mismatch
Data:		System.Collections.ListDictionaryInternal


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,393
  • Joined: 12-December 12

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

Posted 26 November 2013 - 01:57 PM

I would guess.. because when you perform a mail-merge it creates a new document, and you are trying to find a Section in the mail-merge/template document itself. You need to dip into the new document that is created.

You could try commenting this line:
oWrdDoc.Activate();

When the Merge is executed I would guess (again) that the merged-document becomes active.. it normally does in Word.

Alternatively, if you ensure that there are no other documents open, then you could close the mail-merge document, leaving only the merged-document.. which will be active.
Was This Post Helpful? 0
  • +
  • -

#5 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

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

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

Posted 26 November 2013 - 02:07 PM

It appears you were right about when executing the MailMerge that the MailMerge document becomes the Active one. Commenting out the 'oWrdDoc.Activate();' did not however alleviate the error I received.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,393
  • Joined: 12-December 12

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

Posted 26 November 2013 - 02:18 PM

Do you need this document?

oNewWrdDoc = oNewWord.Documents.Open(docSave);

It may be this document that is becoming active.

If you don't need it - because you will be saving the merged documents each to a completely new document - then I would remove it and create the new document after setting rng, as in my sample code.

Throw in a few MessageBoxes showing the name of the ActiveDocument at each stage. Use another MessageBox to display Activedocument.Sections.Count as well.



Make Word Visible and take the debugging tutorial linked in my signature, so that you could step through your code! Debugging skills are essential.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,393
  • Joined: 12-December 12

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

Posted 26 November 2013 - 02:24 PM

You might need to use Range() (with brackets - as you have further down) as well, I haven't checked/tested this myself.

Edited: A quick search suggests the brackets aren't necessary in this line:
oNewWord.Activedocument.Range().FormattedText = rng.FormattedText;

but I think they might be necessary here:
rng = oWord.Activedocument.Sections[1].Range();

in fact, you may have to use:
rng = oWord.Activedocument.Sections[1].Range(0, oMissing);

This post has been edited by andrewsw: 26 November 2013 - 02:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

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

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

Posted 26 November 2013 - 02:34 PM

Ok, on the first message box for name I receive the name of my Template document.

The section count of this document is 1 (Template is 1 page, makes sense).

On the third message box, second for Activedocument.Name, I receive the name of the opened MailMerge Document, "Form Letters1".

The section count of this document is 4 (This seems off because I am only selecting 3 records for MailMerge...?)

After this I receive a new error message:

Source:		Microsoft Word
Message:	This command is not available because no document is open.
Data:		System.Collections.ListDictionaryInternal




New 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;
                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());
                
                // ERROR
                // Select Section 1 of Mail Merge Doc?
                rng = oWord.Activedocument.Sections[1].Range;

                // Place selected text into the new Document???
                oNewWord.Activedocument.Range().FormattedText = rng.FormattedText;

                // 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
  • +
  • -

#9 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,393
  • Joined: 12-December 12

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

Posted 26 November 2013 - 02:39 PM

Quote

Message: This command is not available because no document is open.

That should be a much easier error to resolve.

I also edited my previous post, in case you missed this.

This post has been edited by andrewsw: 26 November 2013 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

#10 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

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

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

Posted 26 November 2013 - 02:44 PM

View Postandrewsw, on 26 November 2013 - 09:24 PM, said:

You might need to use Range() (with brackets - as you have further down) as well, I haven't checked/tested this myself.

Edited: A quick search suggests the brackets aren't necessary in this line:
oNewWord.Activedocument.Range().FormattedText = rng.FormattedText;


Changing Range() to Range here flags Range with Error: 'Microsoft.Office.Word.Interop._document.Range(ref object, ref object)' is a 'method', which is not valid in the given context.

View Postandrewsw, on 26 November 2013 - 09:24 PM, said:

but I think they might be necessary here:
rng = oWord.Activedocument.Sections[1].Range();


This gives: Non-invocable member 'Microsoft.Office.Interop.Word.Section.Range' cannot be used like a method.

View Postandrewsw, on 26 November 2013 - 09:24 PM, said:

in fact, you may have to use:
rng = oWord.Activedocument.Sections[1].Range(0, oMissing);


This also gives: Non-invocable member 'Microsoft.Office.Interop.Word.Section.Range' cannot be used like a method.

View Postandrewsw, on 26 November 2013 - 09:39 PM, said:

Quote

Message: This command is not available because no document is open.

That should be a much easier error to resolve.

I also edited my previous post, in case you missed this.


But that particular error doesn't seem to make any sense? At the time, I have 3 word instances (2 full word documents) open.

I have my original template open, the instance I make visible where I was previously loading a blank document, and then the final Mail Merge Document, "Form Letters1".

I caught your previous post, replied with my attempts at making suggested changes.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,393
  • Joined: 12-December 12

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

Posted 26 November 2013 - 02:55 PM

..seems like my guesswork is off today ;)

I may have mentioned this before but.. there should not be a need for more than one instance of Word - there rarely is. I would also try and work with just one or two documents at a time, but I assume you have your reasons to try and juggle more than this. But it remains my suspicion that this multiplicity is the root-cause of your issue. KISS
Was This Post Helpful? 0
  • +
  • -

#12 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

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

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

Posted 26 November 2013 - 03:17 PM

Here is my updated code. I thought it was the same line where I was assigning 'rng' giving me that new error message, but it is actually the one below it:

                // ERROR!
                // Place selected text into the new Document???
                oNewWord.Activedocument.Range().FormattedText = rng.FormattedText;
----------------------------------------------------------------------------------
Source:		Microsoft Word
Message:	This command is not available becuase no document is open.
Data:		System.Collections.ListDictionaryInternal



Full 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;
                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());             // Template's Name
                MessageBox.Show(oWord.Activedocument.Sections.Count.ToString());   // Count of 1
                
                // Execute Mail Merge
                oWrdDoc.MailMerge.Execute();
                
                // Set Mail Merge Document as Active Doc
                //oWrdDoc.Activate();

                MessageBox.Show(oWord.Activedocument.Name.ToString());             // MailMerge Doc, "Form Letters1"
                MessageBox.Show(oWord.Activedocument.Sections.Count.ToString());   // Count of 4 (I'm only selecting 3 records???)
                
                // Select Section 1 of Mail Merge Doc?
                rng = oWord.Activedocument.Sections[1].Range;

                // ERROR!  
                // Place selected text into the new Document???
                oNewWord.Activedocument.Range().FormattedText = rng.FormattedText;

                // 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
            {
                //
            }
        }


This post has been edited by AnalyticLunatic: 26 November 2013 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,393
  • Joined: 12-December 12

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

Posted 26 November 2013 - 03:43 PM

If you concentrate on your code that uses oNewWord you will see that you have started a new instance of Word but it doesn't have any documents open.
Was This Post Helpful? 0
  • +
  • -

#14 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

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

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

Posted 26 November 2013 - 03:51 PM

Good catch...

After changing, I am receiving the original(ish) error for that same line of code:

Source:		Microsoft Word
Message:	Type mismatch
Data:		System.Collections.ListDictionaryInternal



Updated above code to show where I am opening a document with that new word instance.

...?

This post has been edited by AnalyticLunatic: 26 November 2013 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#15 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 222
  • 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 - 07:58 AM

Still having issues with my Type Mismatch error :/

Bad Code:
// ERROR! - Type Mismatch
// Place selected text into the new Document???
oNewWord.Activedocument.Range().FormattedText = rng.FormattedText;



Updated 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;
                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;


                // ERROR! - Type Mismatch
                // Place selected text into the new Document???
                oNewWord.Activedocument.Range().FormattedText = rng.FormattedText;


                // 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
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2