Mail Merging in MSWord from Within Javascript

N

North Country Boy

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.doc";
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:=wdDocumentsPath) = "C:\temp";

objWordDoc.Select;
objWordDoc.Close;
objWordMerged.Select;


Any comments on how I can get this to work?
 
V

Vjekoslav Begovic

North Country Boy said:
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.doc";

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:=wdDocumentsPath) = "C:\temp";
Wrong.

objWordDoc.Select;
objWordDoc.Close;
objWordMerged.Select;

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

Vjekoslav
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top