简体   繁体   中英

VBA Word change font size of specific words in a table cell

From Excel I am editing a Microsoft Word document. I have a table in my Word document where column 4 consists of a number and then the letters wk after it.

Example:

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

I am trying to change the font size of the letters wk . I figured I could do this with selection and then replace the letters, but it's definitely not working in VBA via Excel. How can I achieve this?

My current code:

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

This is untested and on mobile, so bear with me here.

Currently you're not changing the text size of the “replacement” text, so you should update to .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

Try:

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

Note: If the only cells containing 'wk' are in column 4, you don't need to select the column. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM