Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / April 2007

Tip: Looking for answers? Try searching our database.

save merged document from c# without opening word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tom - 12 Apr 2007 16:38 GMT
Hi,

I would like to save a merged document to disk, without opening word.
The merging works fine if I make word visible and let the user save the doc,
but if I try to save from code without showing word, i get 'Opening this
document will run the following SQL command'.

The code I use 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.wdOpenFormatAuto;

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.WdMailMergeDestination.wdSendToNewDocument;
wrdMailMerge.Execute(ref objFalse);

object wdFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;
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
Peter Jamieson - 12 Apr 2007 23:01 GMT
Usually you have to take account of

http://support.microsoft.com/?kbid=825765

Peter Jamieson
> Hi,
>
[quoted text clipped - 64 lines]
> Thanx,
> t
tom - 13 Apr 2007 07:56 GMT
Thanks Peter,

But the thing is, if I do display word and let the user save the document,
it is no longer a document with mergefields, so it doesn't need a link to the
datasource anymore.

I want to achieve the same effect by code, do the mailmerge, save the doc
without it having the need to have the datasource still.

Tom
Peter Jamieson - 13 Apr 2007 08:22 GMT
Hi Tom,

I had a look at your code again and the problem could be to do with the
Save, because you're using wrdDoc to do the save. When Word merges to a new
document, that document becomes the ActiveDocument and you have to reference
it that way.

See if that changes anything.

Peter Jamieson
> Thanks Peter,
>
[quoted text clipped - 7 lines]
>
> Tom
tom - 13 Apr 2007 10:12 GMT
Thanx Peter,

It was exactly that. I have to use the new activedocument.

Tom

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.