Home  >  Article  >  Web Front-end  >  How to open, edit, and create Office documents directly on the Web page_javascript skills

How to open, edit, and create Office documents directly on the Web page_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:16:561838browse

How to open, edit, and create Office documents directly on the Web page
A friend asked how to achieve the same effect as in SharePoint on the Web page, directly activating the client's Word to open the .doc file, instead of directly clicking on it The .doc document is linked like when Word is opened in IE. I think this question will be of interest to many people, so I decided to write a blog to briefly describe the method.

After installing Office2003, an ActiveX control was installed into the system. This control is located in "Program FilesMicrosoft OfficeOFFICE11owssupp.dll". Through this control, JavaScript on the client page can activate the local Office software to open and edit Office documents. (In addition, Office XP should already contain this ActiveX control.)

First, use Script to create a local object:

openDocObj = new ActiveXObject("SharePoint.OpenDocuments.2") ; // In order to be compatible with Office XP, you can create "SharePoint.OpenDocuments.1"

Then, call the corresponding method of openDocObj. For example, open an Office document on the server:

openDocObj.ViewDocument("http://www.abc.com/documents/sample.doc");

the openDocObj object will be based on the parameters. Different Office document types (.doc, .xls, .ppt) to open different programs (Word, Excel, PowerPoint). The ViewDocument() method also has an overload signature that allows us to manually specify which program to activate to open the document:

openDocObj.ViewDocument("http://www.abc.com/documents/sample.doc" , ProgID of the program to be activated);

So what if you want to open the Office program to edit files online?

openDocObj.EditDocument("http://www.abc.com/documents/sample.doc");

You can directly activate Word, edit the document in Word, and then click directly The save function in Word can save the file to the server. Note: In order for Word to save the edited document directly to the server, the Windows Identity of the current context of accessing the Web site must be in the corresponding directory of the server (that is, the virtual directory "http://www.abc.com/documents"). The corresponding physical path on the server) has corresponding write permissions, otherwise the save action will fail. After editing is completed, EditDocument() will return a bool value to reflect whether the editing operation was successful.

We can also create a new document by opening a document template on the server:

openDocObj.CreateNewDocument("http://www.abc.com/documents/sampleTemplate. dot", "http://www.abc.com/documents/");

You can use the template "http://www.abc.com/documents/sampleTemplate.dot" to create a For new documents, the default saving location of new documents is "http://www.abc.com/documents/". The program used when creating a new document depends on the type of template file (for example, a .dot template would correspond to Word). Saving new documents also requires attention to permission issues. The CreateNewDocument() method will also return a bool value to reflect whether the operation was successful.

The first parameter of the CreateNewDocument() method, in addition to using the address of a template, can also be directly specified as the ProgID of the client program that you want to use to create a new document.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn