2

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?

Kara
  • 6,115
  • 16
  • 50
  • 57
user3165438
  • 2,631
  • 7
  • 34
  • 54

3 Answers3

1

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;
James S
  • 3,558
  • 16
  • 25
  • Seems a good suggestion. What is the difference between NetOffice and Office Interop PIAs? – user3165438 Apr 23 '14 at 09:24
  • NetOffice is a wrapper for the Office Com Interface, it uses LateBinding / reflection under the hood though, hence the ability to work with any Office Version. See the documentation for more info – James S Apr 23 '14 at 09:32
  • Have I to attach a dll just like I did With Office PIAs? – user3165438 Apr 23 '14 at 09:35
  • If the assemblies are referenced as part of your project they should automatically be included in the build - no effort needed. You can get the NetOffice assemblies via NuGet anyway, so even easier! – James S Apr 23 '14 at 09:38
  • NB - there is no need to reference the Office PIA assemblies anymore, so you can remove them from your project references – James S Apr 23 '14 at 09:52
  • Thanks, I need some help with migrating my code to this assemblies. I have downloaded the link you brought. Now, Where are the dlls to attach as references? – user3165438 Apr 23 '14 at 10:00
  • Do you have the NuGet Package Manager Installed? If not I'd recommend you install this (From Tools -> extensions and Updates) After that right click on your project -> Manage Nuget Packages. Then search for NetOffice. Install the packages NetOffice.Core.Net45 and NetOffice.Word.Net45 (for .net4.5, or whatever your version) You can of course just download manually, unpack, and add a reference instead if you want though – James S Apr 23 '14 at 10:03
  • Not so fluency conversion. I get the following error : `Error CS0266: Cannot implicitly convert type 'NetOffice.WordApi.Enums.WdWindowState' to 'Microsoft.Office.Interop.Word.WdWindowState'. An explicit conversion exists ` when I do `wordApp.WindowState = WdWindowState.wdWindowStateMinimize;` Any ideas? – user3165438 Apr 23 '14 at 10:43
  • remove all references to the Microsoft.Office.Interop assemblies from your project and classes - just use the references I've posted above instead In particular at the top of your class you should have: `using NetOffice.WordApi.Enums;` and NOT `Microsoft.Office.Interop.Word` – James S Apr 23 '14 at 10:52
  • Fine, That solved the problem. Now I get the strange exception "See inner exception for more details" when I create a ne file based on a template `wordDoc = wordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);` Thanks for your detailed answers! – user3165438 Apr 23 '14 at 11:09
  • Best post the innerexcepion then! However from looking at the NetOffice API it looks like there are usually overloads which mean you dont need to pass all the optional parameters as ref missing. None of the examples show passing parameters as ref, so you're probably ok with just `wordDoc = wordApp.Documents.Add(oTemplate);` Look at the linked example in my answer, or others on the site. – James S Apr 23 '14 at 11:24
0

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

Use the following project's code base to create and save ms word using c#: https://github.com/kauser-cse-buet/NetOfficeUsage