-1

Env:Windows 10 64 bit, Visual Studio express 2013. I've installed microsoft office 2013 and 2003. And already add Microsoft Office 11.0 object library and Microsoft Word 11.0 object library references.But doesnot work. enter image description here enter image description here

I've tried that if I change Microsoft Word 11.0 object library to Microsoft Word 15.0 object library and everything is OK.But I need to work for word 2003,so, what should I do?

code:

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Word = Microsoft.Office.Interop.Word;

namespace readDOC {
    class Program {
        static void Main(string[] args) {

            string cd = Directory.GetCurrentDirectory();
            Word.Application word = new Word.Application();
            List<string> dirs = new List<string>();
            foreach (string dir in Directory.GetFiles(@".", "*.doc?")) {
                if (dir.IndexOf('~') == -1) {
                    dirs.Add(cd + dir.Substring(1));
                }
            }
            foreach (string fn in dirs) {
                Word.Document docs = word.Documents.Open(fn);
                try {
                    docs.Protect(Word.WdProtectionType.wdAllowOnlyReading);
                    Console.WriteLine(@"OK:{0}.", fn);
                } catch (Exception e) {
                    Console.WriteLine(@"NOT OK:{0}.", fn);
                } finally {
                    docs.Close();
                }
            }
            word.Quit();
            Console.WriteLine("Press any key to finish");
            Console.ReadLine();
        }
    }
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
tcpiper
  • 2,456
  • 2
  • 28
  • 41
  • 2
    What doesn't work? What is the error? Please post the *full* exception text, ie use `Exception.ToString()`. For all we know you may be getting a File Not Found error – Panagiotis Kanavos Aug 17 '16 at 09:55
  • BTW you *can't* open a specific version, as [explained here](http://stackoverflow.com/questions/12073152/how-to-open-specific-version-of-word-2007-2010-in-excel). This isn't specific to word, that's just how COM/OLE works. Each COM application registers all previous interfaces so when you ask for 11.0 you'll get the application registerd for this, ie Word 2013. – Panagiotis Kanavos Aug 17 '16 at 10:06
  • Why do you want to interop with 2003 anyway? You don't need that to generate `doc` instead of `docx` files - you can specify the format to save to through interop. Besides, very few people still use the old format. `docx` is already 7 years old. You could just generate `docx` files using the OpenXML SDK without having Word installed – Panagiotis Kanavos Aug 17 '16 at 10:07
  • @Panagiotis Kanavos Look at my first picture. the word `Interop` of line `using Word = Microsoft.Office.Interop.Word;` has a red line below which means `Interop` is not a member of `Microsoft.Office`, no need to build to make the error pop up and paste here. – tcpiper Aug 17 '16 at 10:36
  • @Panagiotis Kanavos I need to write a program that make hundreds of `.doc` and `.docx` files readonly under windows xp (sp1 or sp2 or sp3), if I interop with 2013, it's obviouly not work. – tcpiper Aug 17 '16 at 10:39
  • Why? Word 2013 can save both formats. In fact, no matter what version you have installed after 2007 it can create both formats. In any case, if you want your program to run on the client's machine you have to use whatever the client has installed. The Word Automation API is backwards compatible anyway – Panagiotis Kanavos Aug 17 '16 at 10:40
  • As for the picture, posting pictures and expecting people to understand what you ask is considered very bad form in SO, not least because images are ungooglable. You should specify that you had a compiler error from the start. The solution to this is to use the *correct* library, ie the Word Interop libary, not the Office objects library. The Word Interop libraries come with Visual Studio itself – Panagiotis Kanavos Aug 17 '16 at 10:45
  • Possible duplicate of [PIA's Installed but still receiving interop error](http://stackoverflow.com/questions/11484894/pias-installed-but-still-receiving-interop-error) – Panagiotis Kanavos Aug 17 '16 at 10:48
  • @Panagiotis Kanavos thx. It seems that vs express can't do this. I'll switch to vs 2015 community. – tcpiper Aug 17 '16 at 11:01

1 Answers1

1

After reviewing the question I believe it is because you are not using the Interop (Extensions).

Make sure since you are adding 2003 that you set your build platform target to x86. There is no x64 support for 2003. That would be my guess. I can't test 2003 since this is my work machine.

I hope my suggestion points you in the right direction.