Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi there, I am trying to automat

Status
Not open for further replies.

paulusg62

Technical User
Oct 23, 2019
2
NL
Hi there,

I am trying to automate mailmerge from C# by opening a word-template (already bound to excel), refresh the link to excel and generate a result-document. This works fine, but the execution of the code gets interrupted when the template is closed, because the Word Save As File Dialog box pops up, waiting for one of the buttons to be clicked on. Obviously I don't want this, the template can be closed without saving, but the dialog box interrupts the program flow. My code looks like this:


this.Cursor = Cursors.WaitCursor;

Object oMissing = System.Reflection.Missing.Value;
Object oFalse = false;
String toetsbestand = @"C:\Import\BBS1001.xls";
String template = @"C:\Import\Template.docx";
String uitslag = @"C:\Import\Uitslag.docx";

Word.Application mailApp = new Word.Application();
mailApp.Visible = false;
mailApp.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
mailApp.Documents.Add(template);
mailApp.ActiveDocument.MailMerge.MainDocumentType = Word.WdMailMergeMainDocType.wdFormLetters;

mailApp.ActiveDocument.MailMerge.OpenDataSource(toetsbestand,
template, false, false, true,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, "SELECT * FROM 'BBS1001'",
ref oMissing, ref oMissing, Word.WdMergeSubType.wdMergeSubTypeOther);
mailApp.ActiveDocument.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
mailApp.ActiveDocument.MailMerge.Execute(Pause: false);
mailApp.ActiveDocument.SaveAs(uitslag);
mailApp.ActiveDocument.Close(SaveChanges: false);

GC.Collect();
GC.WaitForPendingFinalizers();

mailApp.Quit();

Marshal.ReleaseComObject(mailApp);

this.Cursor = Cursors.Default;
MessageBox.Show("Uitslag staat klaar!");

Does anyone have any idea how to prevent the Word Dialog Box from popping up? Any help will be greatly appreciated, thanx!
 
Well I came across this idea earlier when searching the internet for answers, that the interactive behavior of the Office applications is hard to suppress when using the Interop interface. The point is that i had automated this process earlier in Visual Basic for Applications, and it worked fine with no interruptions of the code execution. In my view the C# code I wrote now does exactly the same as the VBA-code I wrote earlier, so it seems to me that it should be possible to solve the problem.
 
You probably should look for the alternative to automatization as in your case it's not really welcomed by Microsoft, as far as I know
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top