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?
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;
}
You should use Microsoft.Office.Interop.Word assembly and directive
using Microsoft.Office.Interop.Word;
I added reference of microsoft word object library and also wrote using Microsoft.Office.Interop.Word;
at the top. But the code didn't work. I was working with the code given in this link: http://support.microsoft.com/kb/316384
Then, I changed all the "Word" with "Microsoft.Office.Interop.Word" inside the code and it worked.
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
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
Ensure you have added the reference to Microsoft.Office.Interop.Word. You can add the reference by right clicking on reference, choose "add reference" and then lookup for Microsoft.Office.Interop.Word under the ".NET" tab
In your program file insert:
using Word = Microsoft.Office.Interop.Word;
Notice the "Word=", this is the secret to solving your pain. You must have Word installed, btw.
Here's how I approach it, when I am not sure the user has Word installed: .
// 1. This code creates the word application, a first REQUIRED step in manipulating a Word file
Word.ApplicationClass wordApplication;
try { wordApplication = new Word.ApplicationClass(); }
catch (Exception e) { MessageBox.Show("ERROR! Do you have MS Word installed? " + e.Message.ToString()); }
Source
use this
using Excel = Microsoft.Office.Interop.Excel;
instead of
using Microsoft.Office.Interop.Word;
精彩评论