Save the. Net Operation Word documents as files in other formats

Source: Internet
Author: User

I was just studying howProgramSave the Word document as an XPS file, learn some concepts and operations, and record them immediately.

1.Add a class library for interoperation with word, which contains the. NET representation of the word object model:

 

At first glance, we know that Microsoft has spent a lot of effort on establishing the Office and. Net interoperability platforms. I can guess with so many DLL files. After selecting the Microsoft. Office. InterOP. Word Class Library, You can implement a series of interoperability with word. Next, you need to reference the corresponding namespace in the program:

Using Microsoft. Office. InterOP. word;

2.Create a word process object. Word does not represent one or two document files. It is the processing environment of these document files, or word is also a virtual machine, but a virtual machine that only processes documents. To implement document processing through interoperability with word, you must first create a process environment:

Application appclass = New Application ();

Appclass. Visible= True;

Creating this word process is still relatively simple. The visible attribute can be used to set whether the word process UI is visible. If it is set to true, the effect is the same as that after the word document is opened normally.

3.To open a Word document, I want to save it as a file in another format. Of course, I can edit a Word document again, but I chose to open an existing one as a replacement, in this way, you can ignore the difficulties in Word document editing:

Code

Object missing = System. reflection. Missing. value;

Document worddoc= NewDocument (RefMissing,RefMissing,RefMissing,RefMissing );

Object Filename = @" E: \ test " ;
Worddoc = Appclass. Documents. Open ( Ref Filename,
Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing,
Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing,
Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing );

To be honest, I was very uncomfortable seeing so many strange parameters at the beginning of my research. Document is a document class in the word object model. Do not confuse document with other namespaces. Constructing this document object can be done by default without any valid construction parameters. Then, an existing Word file is opened through the word process object. The most important thing to open a method is to specify the path of the document, that is, the first parameter. because many of the parameters of the method in the word object model are of the ref object type, so I can only continue to follow the same conventions (note that there is no file Suffix in the path, this is optional), other parameters continue to default. An existing Word document has been opened here.

4.Select the format of the saved file and call the saveas method:

Code

Object Format = Wdsaveformat. wdformatxps; ObjectSavepath = @ "E: \ test. XPS ";

Worddoc. saveas ( Ref Savepath, Ref Format, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing,
Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing );

The formats of all supported stored files are defined in the wdsaveformat Enumeration type. The definition of this enumeration type is obviously affected by the Office version installed on the current machine, and my version is 2007. After defining the path name and selecting the enumerated type for saving the file format, you can call the saveas method of the Document Object and specify it in the first two parameters. Other parameters use the default value. In this way, the purpose of using. Net to convert the file format of a Word document is completed.

Thanks for reading! Nice!

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.