[英]Using word vba to format parts of table cell text
我正在使用以下代碼將兩個Excel單元格中的文本合並到一個單詞表單元格中。 我想將第一部分(captionStr)設置為粗體,但似乎無法弄清楚如何僅指定范圍的那一部分。
我嘗試了.Range(0,Len(captionStr)).Font.Bold=True
和.Range.Characters(0,Len(captionStr)).Font.Bold=True
但是兩者都給了我“錯誤的參數數量”錯誤。 我將Office 2010與MS Word 14.0對象參考一起使用
With tbl.Cell(nRow, 2).Range
.Style = rfpDoc.Styles(wdStyleNormal)
captionStr = CStr(nSection) + ". " + ActiveCell.Text
bodyStr = ActiveCell.Offset(0, 1).Text
.Text = captionStr
.Range.InsertParagraphAfter
.Range.InsertAfter(bodyStr)
End With
訣竅是使用Range對象,而不是在編寫信息時引用整個單元格Range。 這樣一來,您就可以更好地控制格式。 格式將始終應用於范圍的當前內容。 所以更像
Dim rngCell as Word.Range
Set rngCell = tbl.Cell(nRow, 2).Range
With rngCell
'Go into the cell, rather than the entire cell
.Collapse wdCollapseStart
'other stuff
.Text = captionStr & vbCr
.Font.Bold = True
'Move to the end of the range
.Collapse wdCollapseEnd
'Doing this in a table cell moves to the next cell, so one back
.MoveEnd wdCharacter, -1
'Now the rest of the content
.Text = bodyStr
.Font.Bold = False
End With
將范圍想象為選擇,然后像使用向左/向右箭頭鍵將選擇“折疊”到一個點一樣將其折疊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.