VBA Word更改表格单元中特定单词的字体大小

时间:2018-12-03 21:52:52

标签: excel vba ms-word excel-2010

从Excel中,我正在编辑Microsoft Word文档。我的Word文档中有一个表,其中第4列由数字组成,然后由字母 wk 组成。

示例:

+------+------+------+-------+
| Col1 | Col2 | Col3 | Col4  |
+------+------+------+-------+
| test |    2 |  123 | 1 wk  |
| test |    2 |  123 | 13 wk |
| test |    2 |  123 | 10 wk |
+------+------+------+-------+

我正在尝试更改字母 wk 的字体大小。我以为我可以选择,然后替换字母,但这绝对不能通过Excel在VBA中使用。我该如何实现?

我当前的代码:

Tbl.Columns(4).Select
WDApp.Selection.Find.ClearFormatting
With WDApp.Selection.Find
    '.ClearFormatting
    .Text = "wk"
    .Replacement.Text = "wk"
    .Font.Size = 9
End With
WDApp.Selection.Find.Execute Replace:=wdReplaceAll

2 个答案:

答案 0 :(得分:4)

这是未经测试的并且可以在移动设备上进行,所以请在这里与我联系。

当前您没有更改“替换”文本的文本大小,因此应更新为.Replacement.Font.Size = 9

With WDApp.ActiveDocument.Content.Find

    .ClearFormatting
    .ClearAllFuzzyOptions

    .Text = "wk"

    With .Replacement
        .Text = "wk"  'this line might be unnecessary
        .Font.Size = 9
    End With

    .Execute Format:=True, Replace:=wdReplaceAll

End With

答案 1 :(得分:2)

尝试:

Tbl.Columns(4).Select
With WDApp.Selection.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "wk"
  .Replacement.Text = "^&"
  .Replacement.Font.Size = 9
  .Execute Replace:=wdReplaceAll
End With

注意::如果只有包含“ wk”的单元格位于第4列中,则无需选择该列。例如:

With WDApp.ActiveDocument.Tables(1).Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "wk"
  .Replacement.Text = "^&"
  .Replacement.Font.Size = 9
  .Execute Replace:=wdReplaceAll
End With