代码之家  ›  专栏  ›  技术社区  ›  James Newton-King dbc

将自定义文件格式添加到Word 2007“另存为”对话框

  •  4
  • James Newton-King dbc  · 技术社区  · 16 年前

    我想添加在Word 2007中导出为新文件格式的选项。理想情况下,如果该选项可以是Word 2007“另存为”对话框中的另一种文件格式,则用户可以在“文件格式”下拉框中选择该格式。

    虽然我有很多.NET经验,但我没有为MS Office做太多的开发。从高层来看,我应该如何使用.NET向Word 2007添加另一种“另存为”格式?

    3 回复  |  直到 16 年前
        1
  •  3
  •   Dirk Vollmar    16 年前

    在Word 2007中,您基本上有两个选项可以添加自己的自定义文件导出过滤器:

        2
  •  2
  •   Joe    16 年前

    看看 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交互的外部程序中。至于 更换 内置的另存为对话框,您还需要处理 Microsoft.Office.Interop.Word.Application.DocumentBeforeSave 事件某处(vba,带有此代码等)截获默认行为。

    下面是“另存为”处理程序示例:

    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 );
        }
    
        3
  •  0
  •   Todd Main    16 年前

    无法保存为自定义格式或通过对象模型更改“另存为”对话框。现在看来,这是唯一的办法 http://msdn.microsoft.com/en-us/library/aa338206.aspx

    推荐文章