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

发布于 2024-08-14 15:39:06 字数 196 浏览 2 评论 0原文

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

虽然我有很多 .NET 经验,但我还没有为 MS Office 做过太多开发。从高层次来看,使用 .NET 将另一种另存为格式添加到 Word 2007 中时,我应该注意什么?

I want to add the option to export to a new file format in Word 2007. Ideally it would be nice if the option could be another file format in the Word 2007 Save As dialog that the user could select in the file format dropdown box.

Although I have a lot of .NET experience I haven't done much development for MS Office. At a high level what should I look at to add another save as format to Word 2007 using .NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

川水往事 2024-08-21 15:39:06

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

You basically have two options in Word 2007 to add your own custom file export filters:

简美 2024-08-21 15:39:06

看一下 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/与 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 );
    }

Take a look at the Microsoft.Office.Core.FileDialog interface and its Filters property (which is type Microsoft.Office.Core.FileDialogFilters), where you can add and remove filters. They are included with Visual Studio Tools for Office 12, in Office.dll.

As to getting the correct FileDialog object, first acquire a Microsoft.Office.Interop.Word.Application instance (usually by creating a new ApplicationClass or, equivalently, using VBA's CreateObject) and call it application. Then do something like the following:

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.
}

The actual code can either be in a COM Add-In, or an external program that uses COM to open/interact with Word. As to replacing the built-in Save As dialog, you'll also need to handle the Microsoft.Office.Interop.Word.Application.DocumentBeforeSave event somewhere (VBA, with this code, etc) to intercept the default behavior.

Here's an example 'save as' handler:

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 );
    }
他是夢罘是命 2024-08-21 15:39:06

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

It's not possible to save to a custom format or change the SaveAs dialog through the Object Model. Right now, this is looking like the only way http://msdn.microsoft.com/en-us/library/aa338206.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文