非静态字段所需的对象引用

时间:2012-10-30 00:56:40

标签: c# object ms-word office-interop

我在此上下文中收到此错误消息:

int left = 0;
            int top = 0;
            int width = 0;
            int height = 0;
            Word.Range rng2 = rng;
            Microsoft.Office.Interop.Word.Window.GetPoint(out left, out top, out width, out height, rng);

我该如何解决?

1 个答案:

答案 0 :(得分:2)

窗口是非静态的,您需要创建它的新实例才能使用它。

 //using Word = Microsoft.Office.Interop.Word;
                Word.Application application = this.Application;

                //Not sure how you create your application object, just including this incase you're not using VSTO
                //Word.Application application =
                //(Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
                //OR
                //Word.Application application = new Word.Application();

                Word.Range rng = application.ActiveDocument.Range();
                int left,top,width,height = 0;
                Word.Range rng2 = rng;
                application.ActiveWindow.GetPoint(out left, out top, out width, out height, rng);

Microsoft.Office.Interop.Word.Window window = application.ActiveWindow;
            window.GetPoint(out left, out top, out width, out height, rng);