簡體   English   中英

使用 VBA 將帶有 HTML 標簽的文本呈現為 Word 表格中的格式化文本

[英]Rendering text with HTML tags to Formatted text in a Word table using VBA

我有一個帶有 html 標簽的 word 文檔,我需要將其轉換為格式化文本。 例如,我希望<strong>Hello</strong>顯示為Hello

我以前從未使用過 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
End Sub

對於 .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

嘗試一些類似的東西:

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM