Operate Word documents with JavaScript

Source: Internet
Author: User

1. Write content to word

First, set bookmarks in word, such as bookmark, which can be written in JavaScript.

VaR word;
Word =   New Activexobject ( " Word. Application " );
VaR range = Word. range;
Word. Visible =   True ;
VaR path =   " Filepath " ;
Word. Documents. Open (PATH );
Range = Word. activedocument. bookmarks ( " Bookmark " ). Range;
Range. insertbefore ( " Haha " ); // Insert content after bookmarks

//----- The followingCodeSection attaching protection and canceling protection documentation functions ----

// Protected document, editable at bookmarks
Range. Select (); // Selected bookmarks
VaR psw = ' 123 '
Word. activedocument. bookmarks ( " Bookmark " ). Range. Editors. Add ( - 1 ); // Constant: wdeditoreveryone =-1
Word. activedocument. Protect ( 3 , False , Psw, False , False ); // Constant: wdallowonlyreading = 3
// Cancel protection document
Word. activedocument. unprotect (psw );

//----------------------- End --------------

 

2. convert a Word file into an HTML file

 

< Script Language = Javascript >
Function saveword (){
VaR owordapp = New Activexobject ( " Word. Application " );
VaR odocument = Owordapp. Documents. Open ( " C: \ doc2html \ x.doc " );
Odocument. saveas ( " C: \ test.htm " , 8 )
Owordapp. Quit ();
}
</ Script >
</ Head >
< Body >
Click " Save " Button to save the file " C: \ test.doc " To " C: \ test.htm " :
< Input type = Button onclick = " Saveword () " Value = Save >

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.