对于 .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