2008-10-31 100 views
3

我正在開發我的第一個Word 2007插件,並且我已將OfficeRibbon添加到了我的項目中。在一個按鈕點擊處理程序中,我想引用當前的Word.DocumentWord.ApplicationVSTO:爲什麼OfficeRibbon.Context爲空?

我正試圖通過OfficeRibbon.Context屬性獲取參考,文檔中所說的屬性應參照當前的Application對象。但是,它始終是null

有誰知道任

一)如果有什麼我需要做的,使OfficeRibbon.Context出現填充正確?
b)如果還有其他方法,我可以獲得對Word應用程序或活動Word文檔的引用?

注:

  • 我使用VS2008 SP1

  • 色帶看起來已初始化罰款:色帶正確呈現在Word中;我可以通過構造函數和OnLoad成員來調試調試器;按鈕單擊處理程序正確執行。

  • 這裏的the online help for this property;

OfficeRibbon.Context屬性

C#
public Object Context { get; internal set; }

表示與此OfficeRibbon對象相關聯的檢查員窗口或應用程序實例的對象。

備註

在Outlook中,此屬性指的是在其上顯示此OfficeRibbon檢查員窗口。

在Excel,Word和PowerPoint中,此屬性返回顯示此OfficeRibbon的應用程序實例。

回答

4

我在使用VS2008 SP1創建Excel 2007 AddIn時也遇到了此問題。我使用的解決方法是在internal static屬性的應用程序存儲在主外接程序類,然後在我的絲帶引用它在事件處理程序:

public partial class ThisAddIn 
{ 
    internal static Application Context { get; private set; } 

    private void ThisAddIn_Startup(object sender, System.EventArgs e) 
    { 
     Context = Application; 
    } 
    ... 
} 

public partial class MyRibbon : OfficeRibbon 
{ 
    private void button1_Click(object sender, RibbonControlEventArgs e) 
    { 
     DoStuffWithApplication(ThisAddIn.Context); 
    } 
    ... 
} 
1

雖然我不太瞭解Office 2007單詞對象模型的變化,但這裏是我使用VBA知識的解釋。

應用程序是一個全局可用的對象。 此外,Application.ActiveDocument應該讓你處理當前文檔。

投機:你是如何嘗試添加色帶?

2

嘗試用引用文檔:

Globals.ThisDocument.[some item] 

MSDN Reference

2

得到它:

Globals.ThisAddIn.Application