3

I have put together a simple C# Winforms Application that queries AS400 data based on user criteria and returns the results in a ListView Control. On Button_Click() I then store the Headers and Data in a .txt file. Below I am trying to use that .txt file as the DataSource for a Mail Merge word document.

When the code reaches oWrdDoc = oWord.Documents.Open(oTemplatePath); the program seems to just freeze up. Nothing occurs and I cannot step through to the next line. Anyone have ideas for what I am doing wrong?

public void Print(string docLoc, string docSource)
        {
            try
            {
                Word.Application oWord = new Word.Application();
                Word.Document oWrdDoc = new Word.Document();
                oWord.Visible = true;
                Object oTemplatePath = "C:\\Users\NAME\\Desktop\\B-AIAddChgDual10-06-NEW.doc";
                oWrdDoc = oWord.Documents.Open(oTemplatePath);
                Object oMissing = System.Reflection.Missing.Value;
                oWrdDoc.MailMerge.OpenDataSource("C:\\Users\\NAME\\Desktop\\Test2.txt", oMissing, oMissing, oMissing,
                    oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
                oWrdDoc.MailMerge.Execute();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
            }
            finally
            {
                //
            }
        }

EDIT: Turns out I did not have the new Word instance set to Visible = true so when the instance brought up a dialog saying the file was locked for editing (by myself???) it was prompting me to open a Read-Only version, which previously I could not see, making it look like everything was frozen in processing. I've modified my code above to reflect the changes.

Any ideas for why I'm locked out of my own file, and how to prevent this?

These are the dialogs I receive after accepting open a Read-Only document (in order):

Reult Dialog1 Dialog2 Dialog3 Dialog4 Dialog5 Dialog6 Dialog7

After selecting how to replace the fields in the above:

Merge Errors

Original Mail Merge Fields:

enter image description here

After Personal Selections:

enter image description here

How do I tell the Word Application to use the '!' character as the Field Delimiter in my C# code?

Also, how do I proceed with the dialogs? I'm assuming I receive each one due to my datasource not containing fields matching those listed as Mail Merge Fields?

Here are my Mail Merge Fields:

-fuldate -sys -memno -name -address1 -address2 -address3 -sal

And here are my Delimited fields from my .txt DataSource file:

memno!name!addr1!addr2!city!state!zip!old_addr1!old_addr2!old_city!old_state!old_zip

Analytic Lunatic
  • 3,853
  • 22
  • 78
  • 120

1 Answers1

0

You can set OpenAndRepair Mode to True and then Open the Document

Replace this:

oWrdDoc = oWord.Documents.Open(oTemplatePath);

With following:

oWrdDoc =  word.Documents.Open(oTemplatePath, OpenAndRepair: true);
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • Turns out the instance was not visible, so I was not seeing the dialog asking me if I wanted to open a read-only copy since the document was, for some reason, locked. See my EDIT for additional info. – Analytic Lunatic Nov 14 '13 at 19:46
  • oh great you solved it by yourself :) Happy Coding :) – Sudhakar Tillapudi Nov 14 '13 at 19:48
  • Well, I solved that issue, but if you see my EDIT I've got a lot of pesky dialogs :) How do I code in C# to tell it to use my '!' character as Field Delimiter? – Analytic Lunatic Nov 14 '13 at 19:49