简体   繁体   中英

Saving Word document

I would like to save a Word document according to the Word installed version;

In case it is Word 2003 (appropriate version number is 11), with DOC extension.
In case the Word version is higer than 2003, with DOCX extension.

The difference is reflected in the second argument sent to the SaveAS method:

object fileFormat = GraphDocsSettings.Default.WordInstalledVersion > 11.0?     
                WdSaveFormat.wdFormatXMLDocument : WdSaveFormat.wdFormatDocument;

wordDoc.SaveAs(ref outputFile, fileFormat, 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);  

However, When using Interop.Word 11.0 I get the following error:

Microsoft.Office.Interop.Word.WDSaveFormat does not contain a definition for wdFormatXMLDocument.  

Any ideas?

Not sure the API is exactly the same between different word versions.

If I can make a suggestion - use NetOffice( see link ) instead of the office interop assemblies.

the API is the same as the Office Interop API, and it will work with all (current) versions of Microsoft Office.

NB - Here is a sample: http://netoffice.codeplex.com/wikipage?title=Word_Example01

You should import the following namespaces to get it to work:

using NetOffice;
using Word = NetOffice.WordApi;
using NetOffice.WordApi.Enums;
using Office = NetOffice.OfficeApi;

That is because you use the wrong version of the interop assemblies. You cannot reference a value which only exists in higher versions of the framework.

You should use a higher interop version, or create two separate projects, one for the older version of the interop assemblies, one for the higher versions.

使用以下项目的代码库通过c#创建和保存ms字: https : //github.com/kauser-cse-buet/NetOfficeUsage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM