Connecting Tech Pros Worldwide Help | Site Map

Mail Merging in MSWord from Within Javascript

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 12:00 PM
North Country Boy
Guest
 
Posts: n/a
Default 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?

  #2  
Old July 20th, 2005, 12:00 PM
Vjekoslav Begovic
Guest
 
Posts: n/a
Default Re: Mail Merging in MSWord from Within Javascript

"North Country Boy" <kmcgregor@fastmail.fm> wrote in message
news:681286b3.0311200325.5905f0a2@posting.google.c om...[color=blue]
> 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);[/color]

In JScript, you have to escape backslash. Put:
var objTextFile = objFSO.CreateTextFile("C:\\temp", true);
[color=blue]
> var MergeTags = "Title|Forename|Surname";
> var MergeData = "TestTitle|TestForename|TestSurname";
>
> objTextFile.writeline (MergeTags);
> objTextFile.writeline (MergeData);
> objTextFile.Close;[/color]

Should be objTextFile.Close();
[color=blue]
> var FName = "C:\Development\ATOFina\CBS\Templates\Course.d oc";[/color]

Should be \\ instead of \.
[color=blue]
> var objWordDoc = objWordApp.Documents.Open(Filename:=FName);[/color]

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);

[color=blue]
> objWordDoc.MailMerge.OpenDataSource(Name:="C:\temp \merge.dat",[/color]

Same thing.
[color=blue]
> objWordDoc.MailMerge.Execute;[/color]

Should be: objWordDoc.MailMerge.Execute();
[color=blue]
> objWordApp.Options.DefaultFilePath(Path:=wdDocumen tsPath) = "C:\temp";[/color]

Wrong.
[color=blue]
> objWordDoc.Select;
> objWordDoc.Close;
> objWordMerged.Select;[/color]

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

Vjekoslav


  #3  
Old July 20th, 2005, 12:00 PM
Albert Wagner
Guest
 
Posts: n/a
Default Re: Mail Merging in MSWord from Within Javascript

On 20 Nov 2003 03:25:00 -0800
kmcgregor@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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.