468,301 Members | 1,464 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

Mail Merging in MSWord from Within Javascript

Hi

I'm trying to run the mail merge operation in MS Word from within
Javascript. I've already done this in VB but I'm having problems
trying to do the same thing in Javascript. First I am trying to create
a textfile which is the datasource for the mail merge process, then
I'm trying to run Word and mail merge.

The code I am trying to use in Javascript is:

var objFSO = new ActiveXObject( "Scripting.FileSystemObject" );
var objTextFile = objFSO.CreateTextFile("C:\temp", true);
var MergeTags = "Title|Forename|Surname";
var MergeData = "TestTitle|TestForename|TestSurname";

objTextFile.writeline (MergeTags);
objTextFile.writeline (MergeData);
objTextFile.Close;

objFSO = null;
objTextFile = null;

var objWordApp = new ActiveXObject("Word.Application");
objWordApp.Visible = true;

var FName = "C:\Development\ATOFina\CBS\Templates\Course.d oc";
var objWordDoc = objWordApp.Documents.Open(Filename:=FName);
objWordDoc.Select;

var objWordSelection = objWordApp.Selection;
var objWordMailMerge = objWordDoc.MailMerge;

objWordDoc.MailMerge.OpenDataSource(Name:="C:\temp \merge.dat",
LinkToSource:=True, addtorecentfiles:=False);

objWordDoc.MailMerge.Execute;
var objWordMerged = objWordApp.ActiveDocument;

objWordApp.Options.DefaultFilePath(Path:=wdDocumen tsPath) = "C:\temp";

objWordDoc.Select;
objWordDoc.Close;
objWordMerged.Select;
Any comments on how I can get this to work?
Jul 20 '05 #1
2 8865
"North Country Boy" <km*******@fastmail.fm> wrote in message
news:68**************************@posting.google.c om...
Hi

I'm trying to run the mail merge operation in MS Word from within
Javascript. I've already done this in VB but I'm having problems
trying to do the same thing in Javascript. First I am trying to create
a textfile which is the datasource for the mail merge process, then
I'm trying to run Word and mail merge.

The code I am trying to use in Javascript is:

var objFSO = new ActiveXObject( "Scripting.FileSystemObject" );
var objTextFile = objFSO.CreateTextFile("C:\temp", true);
In JScript, you have to escape backslash. Put:
var objTextFile = objFSO.CreateTextFile("C:\\temp", true);
var MergeTags = "Title|Forename|Surname";
var MergeData = "TestTitle|TestForename|TestSurname";

objTextFile.writeline (MergeTags);
objTextFile.writeline (MergeData);
objTextFile.Close;
Should be objTextFile.Close();
var FName = "C:\Development\ATOFina\CBS\Templates\Course.d oc";
Should be \\ instead of \.
var objWordDoc = objWordApp.Documents.Open(Filename:=FName);
This is wrong. Since Open method of Documents property looks:

..Open(FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument,
WritePasswordTemplate, Format, Encoding, Visible)

you have to write:

var objWordDoc = objWordApp.Documents.Open(FName);

(if you pass FName only, of course). If you want to pass FileName and
ReadOnly parameters, you will have to write:

var objWordDoc = objWordApp.Documents.Open(FName,undefined,false);

objWordDoc.MailMerge.OpenDataSource(Name:="C:\temp \merge.dat",
Same thing.
objWordDoc.MailMerge.Execute;
Should be: objWordDoc.MailMerge.Execute();
objWordApp.Options.DefaultFilePath(Path:=wdDocumen tsPath) = "C:\temp";
Wrong.
objWordDoc.Select;
objWordDoc.Close;
objWordMerged.Select;


objWordDoc.Select();
objWordDoc.Close();
objWordMerged.Select();

Vjekoslav
Jul 20 '05 #2
On 20 Nov 2003 03:25:00 -0800
km*******@fastmail.fm (North Country Boy) wrote:
<snip>
Thank god you are trying to do this with IE proprietary "features." At
least the rest of us are safe.

--
Then there was the man who drowned crossing a stream with an average
depth of six inches.
-- W. I. E. Gates
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dan Nash | last post: by
1 post views Thread by Morris | last post: by
1 post views Thread by S Taylor | last post: by
5 posts views Thread by Y.A. | last post: by
6 posts views Thread by Peter | last post: by
7 posts views Thread by giladp1 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.