使用word vba格式化表格单元格文本的各个部分

时间:2016-04-19 21:04:05

标签: vba ms-word word-vba

我正在使用以下代码将两个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

1 个答案:

答案 0 :(得分:1)

诀窍是使用Range对象,而不是在写入信息时引用整个单元格Range。这样,您就可以更好地控制格式化。格式将始终应用于Range的当前内容。更像是

var fileToDelete = Path.Combine(Server.MapPath("~/Content/photos/people"),
                                                                     updatedStaff.Photo);

将范围视为选择范围,并将其折叠为使用左/右箭头键将选区“折叠”到某个点。

相关问题