我最近在这里发布了一个关于阅读 Word 文件的问题。
该应用程序运行良好,但我收到此警告消息;
警告方法“Microsoft.Office.Interop.Word._Document.Close(ref object, ref object, ref object)”和非方法“Microsoft.Office.Interop.Word.DocumentEvents2_Event.Close”之间存在歧义。使用方法组。
某些 using 命名空间似乎有些歧义,我想知道如何解决这个问题。虽然应用程序运行,但我想尽量减少警告/错误。
我为该课程提供了以下代码;它所指的行是这两行
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);
}
}
}
}