A Microsoft Word forum. Microsoft Office Word Forum - WordBanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Microsoft Office Word Forum - WordBanter forum » Microsoft Word Newsgroups » Mailmerge
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

save merged document from c# without opening word



 
 
Thread Tools Display Modes
  #1  
Old April 12th 07, 04:38 PM posted to microsoft.public.word.mailmerge.fields
Tom
external usenet poster
 
Posts: 153
Default save merged document from c# without opening word

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.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
Ads
  #2  
Old April 12th 07, 11:01 PM posted to microsoft.public.word.mailmerge.fields
Peter Jamieson
external usenet poster
 
Posts: 4,583
Default save merged document from c# without opening word

Usually you have to take account of

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

Peter Jamieson
"tom" wrote in message
news
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.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



  #3  
Old April 13th 07, 07:56 AM posted to microsoft.public.word.mailmerge.fields
Tom
external usenet poster
 
Posts: 153
Default save merged document from c# without opening word

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

  #4  
Old April 13th 07, 08:22 AM posted to microsoft.public.word.mailmerge.fields
Peter Jamieson
external usenet poster
 
Posts: 4,583
Default save merged document from c# without opening word

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
"tom" wrote in message
...
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



  #5  
Old April 13th 07, 10:12 AM posted to microsoft.public.word.mailmerge.fields
Tom
external usenet poster
 
Posts: 153
Default save merged document from c# without opening word

Thanx Peter,

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

Tom

  #6  
Old April 24th 13, 12:57 AM
Alex Muyal Alex Muyal is offline
Junior Member
 
First recorded activity by WordBanter: Apr 2013
Posts: 1
Default

Quote:
Originally Posted by tom View Post
Thanx Peter,

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

Tom
Hi, can you show the resulting code of such an action or at least a way to set the new active document and where ?

Thank you, I am dealing with the same problem

Alex.M
  #7  
Old May 21st 13, 03:06 AM
talerKK talerKK is offline
Banned
 
First recorded activity by WordBanter: May 2013
Posts: 2
Default

I'll give it a try ,thx for codes
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
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 08:05 PM
how todisable message box that comes when opening merged document Krishna Kumar Sinha Mailmerge 1 December 9th 05 11:43 AM
how todisable message box that comes when opening merged document Krishna Kumar Sinha Mailmerge 0 December 8th 05 04:20 PM
how todisable message box that comes when opening merged document Krishna Kumar Sinha Mailmerge 1 December 8th 05 09:59 AM
Opening a word merged document from Access code doesn't update tjmax Mailmerge 0 September 13th 05 03:25 PM


All times are GMT +1. The time now is 08:44 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.
Copyright 2004-2022 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.