Word 互操作插件的新文档格式

发布于 2024-09-06 14:55:50 字数 528 浏览 4 评论 0原文

我在使用单词插件时遇到了一些问题。我正在尝试创建一种新的文档格式,并使其正常工作。该格式只是现有 docx 之上的一种编码。换句话说,我可以去掉编码并留下有效的 Word 文档。

保存:

现在,当用户想要以新格式保存文档时,我会提示保存原始文件,然后从磁盘读取原始文件,以新格式保存副本。这会在文件系统上留下 2 个文件(foo.docx 和 foo.myformat)。

但理想情况下,我想在 DocumentBeforeSave 事件中拦截文档,将文档作为字节数组获取,在内存中对其进行转换,然后仅保存一个副本。然而,我似乎找不到获取字节数组的方法。

打开:

现在,当用户选择要打开的以我的格式存储的文件时,我会制作一个临时副本 (tmp\blah.docx),然后只需使用 Application.Documents.Open(tmp\blah.docx) 打开该文档...)。

我更愿意从内存流创建文档,这样我就可以避免创建临时文件。是否可以从内存中创建一个新的Word.Document?

任何方向将不胜感激。非常感谢。

I'm having some trouble with a word add-in. I'm attempting to create a new document format, and have it sort-of working. The format is just an encoding on top of an existing docx. In other words, I can strip off the encoding and be left with a valid word document.

Saving:

Right now, when the user wants to save a document in the new format, I prompt to save the original, and then read the original from disk, saving a copy in the new format. This leaves 2 files on the filesystem (foo.docx and foo.myformat).

Ideally though, I'd like to intercept the document in the DocumentBeforeSave event, get the document as a byte array, transform it in memory, and just save a single copy. I can't seem to find a way to get the byte array however.

Opening:

Right now when the user selects a file to open that is stored in my format, I make a temporary copy (tmp\blah.docx), and then just open the document with Application.Documents.Open(tmp\blah.docx...).

I'd prefer instead to create the document from a memory stream, so I can avoid creating temporary files. Is it possible to create a new Word.Document from memory?

Any direction would be greatly appreciated. Thanks much.

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

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

发布评论

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

评论(2

挽袖吟 2024-09-13 14:55:50

对我来说,你想要做的事情看起来像是一个经典的 Word 文档导入/导出转换器。从 Word 2007 SP2 开始,您可以使用基于 OpenXML 的 API 来创建转换器。

这篇 MSDN 文章描述了如何做到这一点:

介绍 Open XML 格式外部文件转换器2007 Microsoft Office 系统 SP2

MSDN 上还提供了一个示例项目:

Open XML 转换器接口示例代码

What you are trying to do looks like a classic import/export converter for Word documents to me. From Word 2007 SP2 on, you can use an OpenXML based API to create your converter.

This MSDN article describes how to do it:

Introducing the Open XML Format External File Converter for 2007 Microsoft Office System SP2

There is also a sample project available on MSDN:

Open XML Converter Interface Sample Code

姜生凉生 2024-09-13 14:55:50

您可以使用 .WordOpenXML 在 VSTO 或 VBA 中获取文档

You can get the document in VSTO or VBA using .WordOpenXML

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