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):
After selecting how to replace the fields in the above:
Original Mail Merge Fields:
After Personal Selections:
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