源文本包含简单的 HTML。 如何简单地格式化 MS Word 中的文本?

发布于 2024-07-18 17:15:45 字数 528 浏览 4 评论 0原文

我继承了一个存储基本 HTML 格式的项目(即 - <b><i>< /code> 标签)存储在数据库中并将其写入 Word 文档。 这是我的第一个 Word 自动化作业,所以要温柔!

目前,有一个复杂的函数在文档完成后运行,用于搜索和替换这些标签。 然而,由于这是在文档完成后运行的,因此在运行时确定的任何逻辑(即-在此处插入分页符)都可能导致灾难性的结果。 例如,如果我有一大块粗体文本,则该粗体文本会占用更多空间并将换行符推到下一页,从而导致页面大部分为空白。

我相信解决此问题的方法是将来自数据库的文本格式化,以便定位逻辑正确。 我不想多次调用复杂的过程,因为这非常耗时,而且我们的最终用户需要尽快获得此文档。

有没有一种简单的方法可以将 HTML 格式的文本写入 Word 文档,而无需查找和替换每个支持的标签? 我认为Word 中会有一些东西可以自动处理这个问题。 如果您能为我指明正确的方向,请提前致谢。

I've inherited a project that stores basic HTML formatting (i.e. - <b>, <i> tags) in a database and writes it out to a Word document. This is my first Word automation assignment, so be gentle!

Currently, there is a complicated function that runs after the document is complete that searches and replaces these tags. However, as this is run after the document is complete, any logic that is determined at run time (i.e. - insert page break here) can lead to disastrous results. For example, if I have a large chunk of bolded text, this bold text takes up more space and pushes the line break down to the next page, resulting in a mostly blank page.

I believe the fix for this is to format the text as it comes from the database so the positioning logic will be correct. I don't want to call the complicated procedure multiple times as it is time consuming and our end users need this document as quickly as possible.

Is there an easy way to write HTML formatted text to a Word document without needing to find and replace every supported tag? I would think that there would be something within Word that could handle this automatically. Thanks in advance if you can point me in the right direction.

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

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

发布评论

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

评论(2

明月夜 2024-07-25 17:15:45

试试这个:

首先,将要插入的 HTML 保存为普通的“.htm”文件。

然后使用 Range 对象及其 InsertFile 方法 插入“. htm” 文件在任何给定位置:

Dim r As Range

Set r = ActiveDocument.Range
r.InsertFile FileName:=TempFilePath, Link:=False, ConfirmConversions:=False

Word 应该足够智能,能够处理 HTML 并自行完成所有格式转换。 使用 CSS 来控制格式的更精细部分。

完成后删除“.htm”文件。

Try this:

First, save the HTML you are about to insert as an ordinary ".htm" file.

Then use the Range object and it's InsertFile method to insert the ".htm" file at any given position:

Dim r As Range

Set r = ActiveDocument.Range
r.InsertFile FileName:=TempFilePath, Link:=False, ConfirmConversions:=False

Word should be smart enough to handle the HTML and do all of the format conversion on it's own. Use CSS to control the finer parts of the formatting.

Delete the ".htm" file when done.

别理我 2024-07-25 17:15:45

也许您可以调用嵌入式 IE (IWebBrowser2) 来布局文本,然后以 Richtext 形式复制到剪贴板,最后以 RichText(格式化)形式粘贴到 Word。

maybe you can invoke an embedded IE (IWebBrowser2) to layout the text, then copy to clipboard as richtext, and finally paste to Word as RichText (formatted).

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