如何在保留格式的同时使用Visual Basic将HTML格式的字符串插入Microsoft Word文档?

时间:2022-10-30 14:26:14

I use Visual Basic and an automation interface to retrieve strings from an external application. These strings contain simple html formatting codes (<b>, <i>, etc.). Is there any easy function in Visual Basic for Word to insert these strings into a word document and convert the html formatting codes to word formatting?

我使用Visual Basic和自动化接口从外部应用程序检索字符串。这些字符串包含简单的html格式代码(,等)。 Visual Basic for Word中是否有任何简单的函数将这些字符串插入到Word文档中并将html格式代码转换为字格式?

3 个解决方案

#1


12  

Here's a link to add HTML to the clipboard using VB:

这是使用VB将HTML添加到剪贴板的链接:

http://support.microsoft.com/kb/274326

Once you have the HTML on the clipboard, paste it into your word doc using something like this:

在剪贴板上放置HTML后,使用以下内容将其粘贴到word文档中:

ActiveDocument.Range.PasteSpecial ,,,,WdPasteDataType.wdPasteHTML

This is pretty much the equivalent of you cutting and pasting it in manually.

这几乎相当于您手动剪切和粘贴它。

#2


1  

Use InsertFile

Set objdoc = objInsp.WordEditor
Set objword = objdoc.Application
Set objsel = objword.Selection
objsel.WholeStory
vs_html = "<html><body>" + vs_body + "</body></html>"
vs_file = "C:\temp\1.html"
Call DumptoFile(vs_file, "", vs_html, False)
RetVal = objsel.InsertFile(vs_file, , , False, False)

#3


0  

AFAIK there is no builtin function to do that in VBA. You will have to write it yourself, which would be not too difficult if you restirct it to parse <b>, <i>, <a> and <p>, for example. All other tags would have to be ignored.

AFAIK在VBA中没有内置功能。你必须自己编写,如果你将它解析为解析,,

,那就太难了。必须忽略所有其他标签。

#1


12  

Here's a link to add HTML to the clipboard using VB:

这是使用VB将HTML添加到剪贴板的链接:

http://support.microsoft.com/kb/274326

Once you have the HTML on the clipboard, paste it into your word doc using something like this:

在剪贴板上放置HTML后,使用以下内容将其粘贴到word文档中:

ActiveDocument.Range.PasteSpecial ,,,,WdPasteDataType.wdPasteHTML

This is pretty much the equivalent of you cutting and pasting it in manually.

这几乎相当于您手动剪切和粘贴它。

#2


1  

Use InsertFile

Set objdoc = objInsp.WordEditor
Set objword = objdoc.Application
Set objsel = objword.Selection
objsel.WholeStory
vs_html = "<html><body>" + vs_body + "</body></html>"
vs_file = "C:\temp\1.html"
Call DumptoFile(vs_file, "", vs_html, False)
RetVal = objsel.InsertFile(vs_file, , , False, False)

#3


0  

AFAIK there is no builtin function to do that in VBA. You will have to write it yourself, which would be not too difficult if you restirct it to parse <b>, <i>, <a> and <p>, for example. All other tags would have to be ignored.

AFAIK在VBA中没有内置功能。你必须自己编写,如果你将它解析为解析,,

,那就太难了。必须忽略所有其他标签。