Word Interop 代码中的歧义 [英] Ambiguity in Word Interop code
问题描述
我最近发布了一个关于阅读 Word 文件的问题这里.
I recently posted a question about reading Word files here.
应用程序运行良好,但我收到此警告消息;
The app runs fine however I get this Warning message;
警告方法之间的歧义'Microsoft.Office.Interop.Word._Document.Close(ref object, ref object, ref object)'和非方法Microsoft.Office.Interop.Word.DocumentEvents2_Event.Close".使用方法组.
Warning Ambiguity between method 'Microsoft.Office.Interop.Word._Document.Close(ref object, ref object, ref object)' and non-method 'Microsoft.Office.Interop.Word.DocumentEvents2_Event.Close'. Using method group.
某些 using 命名空间似乎有些歧义,我想知道如何解决这个问题.虽然应用程序运行,但我想尽量减少警告/错误.
There seems to be some ambiguity from some using namespace and I would like to know how to resolve this. Although the app runs, I would like to minimize warning/errors.
我提供了以下课程的代码;它所指的那一行就是这两行
I have provided the code below for the class; The line it refers to are these two lines
docs.Close(ref nullobject, ref nullobject, ref nullobject);
wordObject.Quit(ref nullobject, ref nullobject, ref nullobject);
完整代码:
namespace Wizard.Classes
{
class MSWordReader
{
public void read(String filename)
{
String buffer = "";
try
{
Microsoft.Office.Interop.Word.Application wordObject = new
Microsoft.Office.Interop.Word.Application();
object file = filename; //this is the path
object nullobject = Type.Missing;
object visible = false;
object readonlyp = true;
object addtorecent = false; //add to words recent filelist
Microsoft.Office.Interop.Word.Document docs = wordObject.Documents.Open
(ref file,ref nullobject, ref readonlyp, ref addtorecent,
ref nullobject, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref visible,
ref nullobject, ref nullobject, ref nullobject, ref nullobject
);
docs.ActiveWindow.Selection.WholeStory();
docs.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
buffer = data.GetData(DataFormats.Text).ToString();
docs.Close(ref nullobject, ref nullobject, ref nullobject);
wordObject.Quit(ref nullobject, ref nullobject, ref nullobject);
MessageBox.Show(buffer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
推荐答案
要解决歧义,请使用:
((Microsoft.Office.Interop.Word._Document)docs).Close(ref nullobject, ref nullobject, ref nullobject);
((Microsoft.Office.Interop.Word._Application)wordObject).Quit(ref nullobject, ref nullobject, ref nullobject);
这篇关于Word Interop 代码中的歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!