本文介绍了歧义在Word中互操作的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发布了一个问题关于阅读Word文件。



该应用程序运行正常,但是我得到这样的警告消息;



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);

The whole code:

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);
            }
        }
    }
}
解决方案

To resolve the ambiguity, use:

((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中互操作的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:06
查看更多