×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Hi there, I am trying to automat

Hi there, I am trying to automat

Hi there, I am trying to automat

(OP)
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!

RE: Hi there, I am trying to automat

Microsoft doesn't recommend using automation from unattended, noninteractive client applications. You are facing just the top of the iceberg now. Check this link: http://support.microsoft.com/kb/257757

www.R-Tag.com Viewer and Scheduler for Crystal reports, SSRS and Dynamic Dashboards.

RE: Hi there, I am trying to automat

(OP)
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.

RE: Hi there, I am trying to automat

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close