2009-12-10 42 views
4

我想添加選項以在Word 2007中導出爲新的文件格式。理想情況下,如果該選項可以是Word 2007中的另一種文件格式作爲用戶可以在文件格式下拉框中選擇的對話框。將自定義文件格式添加到Word 2007另存爲對話框

雖然我有很多.NET的經驗,但我還沒有爲MS Office做過多的開發。在高層次上,我應該看看如何將另一個另存爲格式添加到使用.NET的Word 2007中?

回答

2

看看在Microsoft.Office.Core.FileDialog接口和其Filters屬性(類型爲Microsoft.Office.Core.FileDialogFilters),您可以在其中添加和刪除過濾器。它們包含在Office.dll中的Visual Studio Tools for Office 12中。

至於得到正確FileDialog對象,首先獲取Microsoft.Office.Interop.Word.Application實例(通常是通過創建一個新的ApplicationClass,或等效,使用VBA的CreateObject),並調用它application。然後做類似如下:

Microsoft.Office.Core.FileDialog dialog = application.get_FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogSaveAs); 
dialog.Title = "Your Save As Title"; 
// Set any other properties 
dialog.Filters.Add(/* You Filter Here */); 

// Show the dialog with your format filter 
if(dialog.Show() != 0 && fileDialog.SelectedItems.Count > 0) 
{ 
    // Either call application.SaveAs(...) or use your own saving code. 
} 

實際的代碼可以是在COM加載項,或使用COM來打開/與Word交互的外部程序。至於替換內置的另存爲對話框,您還需要在某處(VBA和此代碼等)處理Microsoft.Office.Interop.Word.Application.DocumentBeforeSave事件來攔截默認行爲。

下面是一個例子 '另存爲' 處理程序:

private void application_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document document, ref bool saveAsUI, ref bool cancel) 
    { 
     // Be sure we are only handling our document 
     if(document != myDocument) 
      return; 

     // Allow regular "Save" behavior, when not showing the "Save As" dialog 
     if(!saveAsUI) 
      return; 

     // Do not allow the default UI behavior; cancel the save and use our own method 
     saveAsUI = false; 
     cancel = true; 

     // Call our own "Save As" method on the document for custom UI 
     MySaveAsMethod(document); 
    } 
+1

你測試嗎?我不認爲msoFileDialogSaveAs可以添加過濾器。 – 2010-01-12 08:31:23

+0

啊你說得對。我之前已將文件擴展名添加到msoFileDialogOpen,但Word不允許對「另存爲」對話框進行新的擴展。 其餘代碼可以工作,但是您需要爲實際的行爲擴展使用不同的「另存爲」對話框,例如'System.Windows.Forms.SaveFileDialog'。 – Joe 2010-01-12 15:20:09

相關問題