Solved

Creating tables with alternating text in Word using Javascript

Posted on 2008-10-13
2
339 Views
Last Modified: 2012-06-27
I am trying to create a MS Word doc with Javascript. I have had some success with being able to either create the table or the text and populate the table cells with values. When I try to alternate a table then text then another table, the text ends up in the first cell of the first table. This is run from an IE browser.
What am I missing?

function fnCreateWord() {

	var objWord = new ActiveXObject("Word.Application");

	objWord.Caption = "Test";
 

	objDoc = objWord.Documents.Add()

	objWord.Application.Visible = true;

	objSelection = objWord.Selection;

	

	var tbl = objDoc.Tables.Add(objWord.Selection.Range,4,3);
 

// The following is placed properly into the second cell and second column. 	

	var cCell = tbl.cell(2,2).range;

	tbl.cell(2,2).Column.Width = 100;

	cCell.Font.Bold = true;

	cCell.Font.Size = "18";

	cCell.text =  "ZZZZZZZZZZZZ";
 

// The following ends up in first row and the first column of tbl. It needs to be after the table. 

	objSelection.Font.Name = "Arial";

	objSelection.Font.Size = "18";

	objSelection.TypeText("AAAAAAAAAAAAAA");

	objSelection.TypeParagraph();
 

	objSelection.Font.Size = "14";

	objSelection.TypeText("" + Date() );

	objSelection.TypeParagraph();

	objSelection.TypeParagraph();

// There needs to be another table inserted here with more text following. 

}

Open in new window

0
Comment
Question by:HarleyInDTX
2 Comments
 
LVL 28

Accepted Solution

by:
Pravin Asar earned 200 total points
ID: 22944710
Here is a complete solution.

Please try it with IE browser

<html>

<head>

<title>Create Word Document/Page Title</title>

<style type="text/css">

</style>

<script language="javascript">

var EndOfStory =6;
 

function fnCreateWord() {

        var objWord = new ActiveXObject("Word.Application");

        objWord.Caption = "Test";

 

        objDoc = objWord.Documents.Add()

        objWord.Application.Visible = true;
 

        objSelection = objWord.Selection;

        var paragraph = objDoc.Paragraphs.Add();
 

        // Text Before Table 1

        paragraph.Range.Text = "Table 1";

 		objSelection.EndKey(EndOfStory);

        objSelection.TypeParagraph();

        objSelection.TypeParagraph();
 

        // Table 1

        objSelection = objWord.Selection;        

        var tbl1 = objDoc.Tables.Add(objSelection.Range,3,3);

	    var objTable = tbl1;
 

		objTable.Cell(1, 1).Range.Text = "Row 1, Column 1";

		objTable.Cell(1, 2).Range.Text = "Row 1, Column 2";

		objTable.Cell(1, 3).Range.Text = "Row 1, Column 3";

 		objSelection.EndKey(EndOfStory);

		objSelection.TypeParagraph();
 

        // Some Text 

		objSelection.TypeParagraph();

		objSelection.TypeParagraph();

        objSelection.Text = "My Text";

 		objSelection.EndKey(EndOfStory);

		objSelection.TypeParagraph();

		objSelection.TypeParagraph();
 

        // Text Before Table 2

        objSelection = objWord.Selection;

        var paragraph = objDoc.Paragraphs.Add();

        paragraph.Range.Text = "Table 2";

 		objSelection.EndKey(EndOfStory);

        objSelection.TypeParagraph();
 

        // Table 2
 

        objSelection = objWord.Selection;

        objSelection.TypeParagraph();

        var tbl2 = objDoc.Tables.Add(objSelection.Range,3,3);

	    
 

		tbl2.Cell(1, 1).Range.Text = "Table 2 Row 1, Column 1";

		tbl2.Cell(1, 2).Range.Text = "Table 2 Row 1, Column 2";

		tbl2.Cell(1, 3).Range.Text = "Table 2 Row 1, Column 3";

        objSelection.EndKey (EndOfStory);

        objSelection.TypeParagraph();

}

</script>

</head>

<body onload="fnCreateWord();">

</body>

</html>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now