Word-Table Cell的格式子串

时间:2017-01-10 13:54:29

标签: vba ms-word word-vba

是否有任何VBA可自动化的方法来单独格式化Word-Table单元格中的字符集。

我寻找一种解决方案来实现以下(符号代码)

Dim myLbl1 as String
Dim myTxt1 as String
Dim myLbl2 as String
Dim myTxt2 as String
Dim myLbl3 as String
Dim myTxt3 as String

….
‘ Code to fill in the afore defined Variables with random content and
' arbitrary length including linebreaks and paragraph breaks
….
Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, _
    numrows:=3, numcolumns:=1)
oTable.Rows(1).Cells(1).Range.Text = <bold>myLbl1</bold> & vbCrLf & _
    <italic>myTxt1</italic>
oTable.Rows(2).Cells(1).Range.Text = <bold>myLbl2</bold> & vbCrLf & _
    <italic>myTxt2</italic>
oTable.Rows(3).Cells(1).Range.Text = <bold>myLbl3</bold> & vbCrLf & _
    <italic>myTxt3</italic>
Set oTable = Nothing

有没有办法在VBA for Word中定义一个返回格式化字符串的函数,例如

Private function setBold(txt as string) as String
  setBold = <Word-Code-to-Format-the-Provided-String-as-Bold> & txt
End function

实现单独格式化子字符串的目标的推荐方法是什么,例如

  

myLbl3 &amp; vbCrLf&amp; myTxt3

1 个答案:

答案 0 :(得分:0)

您无法应用任何功能或类似HTML的解决方案。您需要依赖于使用对象层次结构的文档结构,在您的情况下,它与此类似:

Document > Table > Cell > Range > Paragraph > Range > Font

接下来你需要使用必需的属性。

在您的情况下,它可能是这样的(单个单元格的代码):

Dim CellRange As Range
Set CellRange = ActiveDocument.Tables(1).Rows(1).Cells(1).Range
CellRange.Paragraphs(1).Range.Font.Bold = True
CellRange.Paragraphs(2).Range.Font.Italic = True