Directly reference Microsoft COM in javascript to generate Word

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Html>
<Head>
<Meta http-equiv = Content-Type content = "text/html; charset = UTF-8">
<Title> Build Document by Script </TITLE>
<Script language = "text/javascript">
Function word_onclick ()
{
Var myDocApp = null;
MyDocApp = new ActiveXObject ("word. Application ");
MyDocApp. Application. Visible = true;
Var myDoc = myDocApp. Documents. Add ();

MyDocApp. Selection. ParagraphFormat. Alignment = 1
MyDocApp. Selection. Range. Bold = true;
MyDocApp. Selection. Font. Size = 22;
MyDocApp. Selection. Text = "Leave Application Form"
MyDocApp. Selection. insertAfter ("\ n ");
MyDocApp. Selection. MoveRight (2, 10 );
MyDocApp. Selection. TypeParagraph ();
MyDocApp. Selection. Font. Bold = false;
MyDocApp. Selection. Font. Size = 12;

Var myTable0 = myDoc. Tables. Add (myDocApp. Selection. Range, 3, 4 );
MyTable0.cell (). Range. Text = "requester ";
MyTable0.cell (1, 2). Range. Text = "Zhang San ";
MyTable0.cell (1, 3). Range. Text = "Time for leave ";
MyTable0.cell (1, 4). Range. Text = "2006-2-10 ";

MyTable0.cell (2, 1). Range. Text = "employee ID ";
MyTable0.cell (32412). Range. Text = ";
MyTable0.cell (2, 3). Range. Text = "" ";
MyTable0.cell (2, 4). Range. Text = "2006-2-9 ";

MyTable0.cell (3, 1). Range. Text = "reason for leave ";
MyTable0.cell (3, 2). Range. Text = "catch a cold ";
MyTable0.cell (3, 3). Range. Text = "Processing Method ";
MyTable0.cell (3, 4). Range. Text = "Sick leave ";

Var range = myDocApp. ActiveDocument. Content;
Range. Collapse (0 );
Range. insertAfter ("\ n ");
Range = myDocApp. ActiveDocument. Content;
Range. Collapse (0 );

Var myTable2 = myDoc. Tables. Add (range, 1, 2 );
MyTable2.Columns (1). SetWidth (320,2 );
MyTable2.cell (). Range. Text = "requester signature" // do not delete Spaces
MyTable2.cell (1, 2). Range. Text = "requester's signature ";

Try {
MyDocApp. ActiveDocument. SaveAs ("e: \ JaveToWord.doc ");
} Catch (exception ){
Alert ("failed to save the file to the local device because the browser security settings are too high ");
MyDocApp. Documents. close ();
MyDocApp. Application. quit ();
MyDocApp = null;
Window. close ();
}
}
</SCRIPT>
</HEAD>
<BODY>
<P> <a href = "javascript: word_onclick ()"> Generate WORD </a> </p>
</BODY>
</HTML>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.