2009-12-10 103 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

相关问题