使用VBA将带有HTML标记的文本呈现为Word表中的格式化文本

时间:2018-09-17 04:08:03

标签: html internet-explorer word-vba formatted-text

我有一个带html标记的word文档,需要将其转换为格式化的文本。例如,我希望<strong>Hello</strong>显示为你好

我以前从未使用过VBA,但是我一直在尝试拼凑一些东西,使我能够从Word中的特定表单元格复制html文本,使用IE显示该文本的格式化版本, IE中的格式化文本,然后将其粘贴回该Word表单元格中。我认为我已经能够找出一些代码,但是我认为我没有正确地引用表格单元格。有人可以帮忙吗?这是我到目前为止的内容:

Dim Ie As Object

Set Ie = CreateObject("InternetExplorer.Application")

With Ie
    .Visible = False

    .Navigate "about:blank"

    .Document.body.InnerHTML = ActiveDocument.Tables(1).Cell(2, 2)

    .Document.execCommand "SelectAll"

    .Document.execCommand "Copy"

    ActiveDocument.Paste Destination = ActiveDocument.Tables(1).Cell(2, 2)

    .Quit
End With

结束子

2 个答案:

答案 0 :(得分:0)

.cell(2,2)的两种用法需要两个不同的方法。

要从单元格中获取文本,您需要修改第一行以阅读

.Document.body.InnerHTML = ActiveDocument.Tables(1).Cell(2, 2).range.text  

在第二种情况下,您的术语不正确。它应该显示为

ActiveDocument.Tables(1).Cell(2, 2).range.paste

您可以轻松获得有关各个关键字/属性的帮助。在VBA IDE中,只需将光标放在关键字/属性上,然后按F1。您将被带到MS帮助页面以获取关键字/属性。有时,当有多个备选方案时,您将有一个额外的选择步骤。

您还应该注意,属性.cell(row,column)容易失败,因为它依赖于表中没有合并的单元格。一种更可靠的方法是使用.cells(index)属性。

可能是您可以采用其他方式,并使用通配符搜索来找到标签,然后在应用合适的链接样式时替换所需的部分(您将无法使用段落样式,因为尝试仅格式化段落的一部分,而字符样式似乎不适用于查找/替换)。

下面是删除HTML标签并格式化其余文本格式的代码示例

Option Explicit

Sub replaceHTML_WithFormattedText()

' a comma seperated list of HTML tags
Const myTagsList                          As String = "strong,small,b,i,em"

' a list of linked styles chosen or designed for each tag
' Paragraph  styles cannot be used as we are replacing only part of a paragraph
' Character styles just don't seem to work
' The linked styles below were just chosen from the default Word styles as an example
Const myStylesList                        As String = "Heading 1,Heading 9,Comment Subject,Intense Quote,Message Header"

' <, > and / are special characters therefore need escaping with '\' to get the actual character
Const myFindTag                           As String = "(\<Tag\>)(*)(\<\/Tag\>)"
Const myReplaceStr                        As String = "\2"

Dim myTagsHTML()                        As String
Dim myTagsStyles()                      As String
Dim myIndex                             As Long

    myTagsHTML = Split(myTagsList, ",")
    myTagsStyles = Split(myStylesList, ",")

    If UBound(myTagsHTML) <> UBound(myTagsStyles) Then
        MsgBox "Different number of tags and Styles", vbOKOnly
        Exit Sub

    End If

    For myIndex = 0 To UBound(myTagsHTML)

        With ActiveDocument.StoryRanges(wdMainTextStory).Find
            .ClearFormatting
            .Format = True
            .Text = Replace(myFindTag, "Tag", Trim(myTagsHTML(myIndex)))
            .MatchWildcards = True
            .Replacement.Text = myReplaceStr
            .Replacement.Style = myTagsStyles(myIndex)
            .Execute Replace:=wdReplaceAll

        End With

    Next

End Sub

答案 1 :(得分:0)

尝试以下方法:

Sub ReformatHTML()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
  .ClearFormatting
  .Format = True
  .Forward = True
  .MatchWildcards = True
  .Wrap = wdFindContinue
  .Replacement.Text = "\2"
  .Replacement.ClearFormatting
  .Text = "\<(u\>)(*)\</\1"
  .Replacement.Font.Underline = True
  .Execute Replace:=wdReplaceAll
  .Replacement.ClearFormatting
  .Text = "\<(b\>)(*)\</\1"
  .Replacement.Font.Bold = True
  .Execute Replace:=wdReplaceAll
  .Replacement.ClearFormatting
  .Text = "\<(i\>)(*)\</\1"
  .Replacement.Font.Italic = True
  .Execute Replace:=wdReplaceAll
  .Replacement.ClearFormatting
  .Text = "\<(h\>)(*)\</\1"
  .Replacement.Highlight = True
  .Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub

上面的宏使用“常规” HTML代码进行粗体,斜体,下划线和突出显示。

由于您的文档似乎使用了不同的约定(也许是样式名称?),因此可以在代码中将(b>)替换为(strong>)。而且,如果要与Word自己的“强”样式相关联,您还需要更改:

.Replacement.Font.Bold = True

收件人:

.Replacement.Style = "Strong"