0

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
3
  • Just to clarify, is the table with your values in Excel or Word?
    – TotsieMae
    Dec 3, 2018 at 22:01
  • @TotsieMae The table is located in Word
    – tutu
    Dec 3, 2018 at 22:02
  • 2
    You need to be changing .Replacement.Text’s size Dec 3, 2018 at 22:17

2 Answers 2

5

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
2

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.