Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Tom Tom is offline
external usenet poster
 
Posts: 153
Default save merged doc from c# code without displaying word

Hi,

I would like to do a mailmerge from within code, then save the document
without having to display word to the user.
The merge works fine if I do display word (wrdApp.Visible = true. If I
don't, I get the message 'Opening this document will run the following SQL
command..'

My code is as follows :

object objNull = System.Reflection.Missing.Value;
string strDataSource = @"C:\HRAccent\MailMerge\dataEmpl.csv";
object strTemplatePath = @"C:\HRAccent\MailMerge\testTemplate.doc";
object objTrue = true;
object objFalse = false;
object objOpenFormat;

objOpenFormat = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenF ormatAuto;

Microsoft.Office.Interop.Word.Application wrdApp = new
Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word._Document wrdDoc;
Microsoft.Office.Interop.Word.MailMerge wrdMailMerge;

wrdDoc = wrdApp.Documents.Open(ref strTemplatePath, ref objFalse, ref
objTrue, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref
objNull, ref objNull, ref objNull, ref objFalse, ref objNull, ref objNull,
ref objNull);

wrdApp.Visible = false;
wrdDoc.Activate();

wrdDoc.MailMerge.OpenDataSource(strDataSource, ref objNull, ref objFalse,
ref objTrue, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull,
ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull,
ref objNull);

wrdMailMerge = wrdDoc.MailMerge;

wrdMailMerge.Destination =
Microsoft.Office.Interop.Word.WdMailMergeDestinati on.wdSendToNewDocument;
wrdMailMerge.Execute(ref objFalse);

object wdFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdForma tRTF;
Object fileName = @"C:\HRAccent\MailMerge\test.doc";

wrdDoc.SaveAs( ref fileName, ref objNull, ref objNull, ref objNull, ref
objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref
objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);

wrdDoc.Saved = true;
wrdDoc.Close(ref objTrue,ref objNull,ref objNull);

wrdApp.Quit(ref objFalse,ref objNull,ref objNull);

wrdMailMerge = null;
wrdDoc = null;
wrdApp = null;


Anyone any ideas?

Thanx,
t
  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default save merged doc from c# code without displaying word

Replied to your other similar message.

Peter Jamieson

"tom" wrote in message
...
Hi,

I would like to do a mailmerge from within code, then save the document
without having to display word to the user.
The merge works fine if I do display word (wrdApp.Visible = true. If I
don't, I get the message 'Opening this document will run the following SQL
command..'

My code is as follows :

object objNull = System.Reflection.Missing.Value;
string strDataSource = @"C:\HRAccent\MailMerge\dataEmpl.csv";
object strTemplatePath = @"C:\HRAccent\MailMerge\testTemplate.doc";
object objTrue = true;
object objFalse = false;
object objOpenFormat;

objOpenFormat =
Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenF ormatAuto;

Microsoft.Office.Interop.Word.Application wrdApp = new
Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word._Document wrdDoc;
Microsoft.Office.Interop.Word.MailMerge wrdMailMerge;

wrdDoc = wrdApp.Documents.Open(ref strTemplatePath, ref objFalse, ref
objTrue, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull,
ref
objNull, ref objNull, ref objNull, ref objFalse, ref objNull, ref objNull,
ref objNull);

wrdApp.Visible = false;
wrdDoc.Activate();

wrdDoc.MailMerge.OpenDataSource(strDataSource, ref objNull, ref objFalse,
ref objTrue, ref objNull, ref objNull, ref objNull, ref objNull, ref
objNull,
ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref
objNull,
ref objNull);

wrdMailMerge = wrdDoc.MailMerge;

wrdMailMerge.Destination =
Microsoft.Office.Interop.Word.WdMailMergeDestinati on.wdSendToNewDocument;
wrdMailMerge.Execute(ref objFalse);

object wdFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdForma tRTF;
Object fileName = @"C:\HRAccent\MailMerge\test.doc";

wrdDoc.SaveAs( ref fileName, ref objNull, ref objNull, ref objNull, ref
objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull,
ref
objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);

wrdDoc.Saved = true;
wrdDoc.Close(ref objTrue,ref objNull,ref objNull);

wrdApp.Quit(ref objFalse,ref objNull,ref objNull);

wrdMailMerge = null;
wrdDoc = null;
wrdApp = null;


Anyone any ideas?

Thanx,
t



Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
merged labels - how do you save the merged document for use later DanaFox Mailmerge 1 November 1st 06 07:05 PM
How can I save each merged Word doc to a separate file? donna-LexusWebs Page Layout 1 March 16th 06 08:24 PM
Zip code that starts with "0" does not show it in merged document Hisako Mailmerge 1 October 11th 05 06:58 PM
Opening a word merged document from Access code doesn't update tjmax Mailmerge 0 September 13th 05 03:25 PM
Unable to save changes in HTML source code using Word AmyK Microsoft Word Help 1 May 22nd 05 10:52 AM


All times are GMT +1. The time now is 06:14 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"