views:

55

answers:

4

I'm trying to use the sample code here:

I went to "Add Reference" dialog and added "Microsoft Word 12 library", but it doesn't appear in the Solution Explorer. I also added: using Microsoft.Office;

I get the following error message when trying to declare a "Word" object.

Error 1: The type or namespace name 'Word' could not be found (are you missing a using directive or an assembly reference?)

Any suggestions on how to properly reference the Word library?

+1  A: 

You should use Microsoft.Office.Interop.Word assembly and directive
using Microsoft.Office.Interop.Word;

Neverrav
I added using Microsoft.Office.Interop.Word;it didn't help:(
Dave
A: 

which version of office do you have installed? My guess is it's a different one. You can see the mapping of official names to versions here: http://en.wikipedia.org/wiki/Microsoft_Word#Versions

Rob Fonseca-Ensor
I do have a newer version than the guy who wrote the tutorial, but surely the basic functions should still work?
Dave
+2  A: 

Edit: changed so that it doesn't use the clipboard

using Microsoft.Office.Interop.Word;

public string Test(string path)
{
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    object file = path;
    object nullobj = System.Reflection.Missing.Value;

    Document doc = wordApp.Documents.Open(ref file, ref nullobj, ref nullobj,
                                           ref nullobj, ref nullobj, ref nullobj,
                                           ref nullobj, ref nullobj, ref nullobj,
                                           ref nullobj, ref nullobj, ref nullobj);

    string result = doc.Content.Text.Trim();
    doc.Close();
    return result;
}
ho1
I get error:Error 1 The type or namespace name 'IDataObject' could not be found (are you missing a using directive or an assembly reference?) C:\Users\...\Documents\Visual Studio 2010\WebSites\Copying1_1\Default.aspx.cs 106 13 C:\...\Copying1_1\This is a asp.net website, so it won't allow me to write: using System.Windows.Forms;
Dave
@Dave - Yes, it was using the clipboard which isn't really an ASP thing. I've updated the code to use `doc.Content.Text.Trim()` instead, which might work. I haven't actually tried the above code and it's probably not very good to have it on a server (especially if it's under heavy load) but it might work now.
ho1
A: 

The technique you're using requires that you have a specific version of Word installed on your developer machine and on any machines you target for deployment.

Since Office 2007, Office documents are stored using Open XML formats. These documents can be read and modified using the Packaging and XML APIs in the .NET Framework. You can also use the Open XML SDK to give you a higher-level abstraction layer on top of the Packaging and XML APIs. Using this technique does not have Word installed for development or production use.

More information on working with Open XML can be found by following the links below:

Open XML Developer

Open XML SDK 2.0 for Microsoft Office

Rob Windsor