如何使用 .NET 在 Word 文档中用格式化文本替换占位符?
我有一个 C# .NET 应用程序,需要加载 Word 文档、查找一些占位符文本(例如“
所有这些都有效,只是我不知道如何将 RTF 控件的格式设置到 Word 文档中。 RTF 文本包含粗体、点点和其他格式,并且文本长度超过 256 个字符,因此 Word 互操作查找和替换方法将不起作用。
我可以使用 Find 方法,然后将选择文本设置为我想要的内容(避免 256 个字符的限制),但我一生都无法弄清楚如何进行格式设置。有一个 FormattedText 成员似乎可以完成这项工作,但无论我分配给它什么(甚至将它分配给自身)都会生成错误,所以我只能假设它是只读的。
但这是一个没有实际意义的问题,因为 RTF 控件似乎只能提供没有格式的纯文本或包含 RTF 代码的字符串。 Word中似乎没有任何解释RTF代码的功能,所以我目前所能做的就是将
我一直在研究 OpenXML API,它可能允许我将格式化文本作为 XML 片段插入,但我不知道如何获取 RTF 控件的内容作为要插入的 XML 片段。
有谁知道可以实现这一目标的某种方法?
I have a C# .NET app which needs to load a Word document, find some placeholder text (e.g. "<DETAILS>") and replace it with formatted text pulled from an RTF control. This final document is then opened as an e-mail message in Outlook, ready to send.
All this works, except that I can't work out how to take the formatting across from the RTF control into the Word document. The RTF text contains bold, dot points and other formatting, and the length of the text is more than 256 characters so the Word interop find and replace method won't work.
I can use the Find method and then set the selection text to what I want (avoiding the 256 char limit) but I can't for the life of me figure out how to take the formatting across. There is a FormattedText member that seems like it would do the job, but no matter what I assign to it (even assigning it to itself) generates an error, so I can only assume it's meant to be read-only.
But that's a moot point because the RTF control seems only able to provide either plain text with no formatting, or a string containing RTF code. There doesn't seem to be any Word function for interpreting RTF code, so all I can do at the moment is replace the <DETAILS> placeholder with some incomprehensible RTF sequences.
I've been investigating the OpenXML API which might be able to let me insert formatted text as a XML fragment, but I can't see how I can get the contents of the RTF control as that XML fragment to be inserted.
Does anyone know some way that this could be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以解决您的问题:
如何使用 Visual Basic Automation 将 RTF 格式字符串粘贴到 Word 中
解决方案是将 RTF - 剪贴板中的代码并指定它是 RTF 格式。然后使用Wordapp.Selection.Paste将内容粘贴到word中。 Word 知道如何处理 RTF(它既可以打开 rtf 文档,也可以从剪贴板读取 RTF 数据)。
如果您使用此方法,请确保在操作之前存储剪贴板内容,并在操作之后恢复剪贴板内容,用户不喜欢程序在没有通知的情况下自行更改剪贴板内容。 (此方法仅更改剪贴板的RTF部分,剪贴板可以存储多种格式,因此您不必存储/恢复所有格式)
This should solve your problem:
How to paste a Rich Text Format string into Word with Visual Basic Automation
The solution is to put the RTF-code in clipboard and specify that it is RTF-format. Then paste the content to word with Wordapp.Selection.Paste. Word knows how to handle RTF (it can both open rtf-documents and read RTF-data from clipboard).
IF you use this method, make sure to store the clipboard-content before the action, and restore the clipboard content after, users dont like when programs alter clipboard-content by themself without notice. (This method only changes the RTF-part of clipboard, the clipboard can store multiple formats, so you dont have to store/restore all formats)