By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,017 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,017 IT Pros & Developers. It's quick & easy.

Mail Merging in MSWord from Within Javascript

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
"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

P: n/a
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.