Word Automation多重粘贴问题
有没有比通过 C# 剪贴板更好的方法将 HTML 片段粘贴到 Word 文档中?
using Word = Microsoft.Office.Interop.Word;
我正在使用一些代码将 HTML 放入剪贴板:
HtmlFragment.CopyToClipboard(changedText);
我在 word 中进行了选择(从表单字段)并且我这样做了:
word.Selection.Paste();
但有时它只是抛出 COM 异常。如果我添加,
Thread.Sleep(100);
我可以让它工作,但这并不理想。
Insert 方法看起来是一个更好的选择,但没有从 HTML 插入。
那么使用自动化界面快速将大量 HTML 片段插入到 Word 中的最佳方法是什么?
编辑
回复中有一些很好的建议,但问题是一个简单的
标签导致Word粘贴失败。
Is there a better way to paste HTML fragments into a Word document than via the clipboard from C#?
using Word = Microsoft.Office.Interop.Word;
I'm using some code that puts HTML into the clipboard:
HtmlFragment.CopyToClipboard(changedText);
I have a selection in word (from a formfield) and I do:
word.Selection.Paste();
But sometimes it just throws a COM exception. If I add
Thread.Sleep(100);
I can get it to work, but that's not ideal.
The Insert methods look like a better option but there is no Insert from HTML.
So what's the best way to insert lots of HTML fragments into Word quickly using the automation interfaces?
Edit
Some good advice in the responses but the issue turned out to be a simple <br>
tag causing word to fail on paste.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于互操作,您需要使用
Selection.PasteSpecial
带有wdPasteHTML
的WdPasteDataType
。如果您使用的是新的 Word 格式(即 2007/2010),您可以完全放弃互操作,而直接使用 WordprocessingML(使用 开放 XML SDK 或仅使用 Linq 和 System.IO.Packaging 自由编写)。或者,如果需要的话,您可以将其与 Interop 结合使用。
如果您使用 Open XML,则只需使用
altChunk
即可导入 HTML。下面是一个示例(其中包括 HTML 示例),位于 如何使用 altChunk 进行文档组装。还有另一个(刚刚出版 - 今天发布):使用 altChunk 导入包含编号的 HTML。For interop, instead of
Selection.Paste
you'll want to useSelection.PasteSpecial
with aWdPasteDataType
ofwdPasteHTML
.If you're using the new formats of Word (i.e. 2007/2010), you could give up interop all together and just go with WordprocessingML (using the Open XML SDK or just free-hand it with Linq and System.IO.Packaging). Or you could just it in conjunction with Interop if that was a need.
If you're using Open XML, you could just use
altChunk
to import HTML. Here's an example (which includes an example for HTML) at How to Use altChunk for Document Assembly. And another (fresh off the presses - it was released today): Importing HTML that contains Numbering using altChunk.不过,对于 Otaku 的评论+1,一般来说,我发现最好使用各种 RANGE.* 函数来粘贴数据,而不是选择对象,或者通过剪贴板粘贴。主要原因是,如果您通过剪贴板进行粘贴,则会打乱剪贴板上的所有内容(这可能不是用户希望发生的情况)。
Selection 对象适用于所有打开的 Word 文档,这在某些情况下可能会给您带来麻烦。不幸的是,有些事情您几乎无法通过其他方式完成。
并且,有些事情(例如更改当前光标位置的文本)必须使用选择对象。
+1 to Otaku's comments, though, generally speaking, i've found it best to use the various RANGE.* functions for pasting in data than the Selection object, or pasting through the clipboard. the main reason is, if you paste through the clipboard, you scramble whatever was on the clipboard (which might not be what the user wants to happen).
the Selection object applies across all open word documents, which can get you in trouble in some cases. Unfortunately there are a few things that you just about can't do any other way.
And, there are some things (Like altering text at the current cursor position) that you MUST use the selection object for.
+1 给 DarinH 评论。另外需要注意的是,您可以使用 Range 粘贴到文档中的任何位置,而无需更改文档的选择(文档中的光标)。
有时 PasteAndFormat 会在新创建的文档上引发异常,如果发生这种情况,请在此处检查我的回复: https://stackoverflow.com/a/65796482/ 15001063
+1 to DarinH comments. Also something to note is that you can paste on any place in the document using Range without having to change the selection of the document (the cursor in the document).
Sometimes PasteAndFormat throws an Exception on freshly created Documents, check my reply here if that happens: https://stackoverflow.com/a/65796482/15001063