0
votes

I already have the following code that deletes the paragraph following and entirely bold paragraph (i.e. clearing blank lines after subheadings). I've been trying to amend it to create another macro that increases the font size of every bold character by 4 points.

Dim para As Paragraph
Dim searchRange As Range

Set searchRange = Selection.Range
searchRange.End = ActiveDocument.Content.End

    For Each para In searchRange.Paragraphs
        If para.Range.Font.Bold = True Then para.Next.Range.Delete
    Next para

This is what I tried to do, but kept getting a "method or data member not found" error on "Range" in "If char.Range.Font.Bold = True" below, and I'm not well enough versed in VBA to know what the actual problem is. Can I not use Characters to search each character in a document? Does Characters work differently to Paragraphs? To my uninformed brain it seems like I could just switch out parts of the macro above (searching every paragraph) to make it apply to what I'd like to do here (search every character).

Dim char As Characters
Dim searchRange As Range

Set searchRange = Selection.Range
searchRange.End = ActiveDocument.Content.End

    For Each char In searchRange.Paragraphs
        If char.Range.Font.Bold = True Then
            char.Font.Size = char.Font.Size + 4
    Next char

I'm not married to the bit about the search range, I'm happy for it to search from the cursor to the end, or just search the whole document, I just copied the other code to try to increase my chances of it working!

2

2 Answers

0
votes

You don't need VBA for this. Find and Replace will do this job. Set Find what to search for bold formatting at the size of the current text, then set Replace with to a size that is 4 points larger. Even if you have several different text sizes, it's faster to run a find and replace several times than write a macro.

0
votes

There are a couple of issues with the code in the question, that aren't really intuitive to recognize.

  1. There is no Character data type in the Word object model, only Characters
  2. A Character object is of the data type Range So: Dim char as Range
  3. A Character object is not equivalent to a Paragraph object, so the For...Each enumeration For Each char in searchRange.Paragraphs triggers a "Type Mismatch". It's necessary to loop searchRange.Characters.

The following compiles and runs for me:

Dim char As Word.Range
Dim searchRange As Range

Set searchRange = Selection.Range
searchRange.End = ActiveDocument.content.End

    For Each char In searchRange.Characters
        If char.Font.Bold = True Then
            char.Font.Size = char.Font.Size + 4
        End If
    Next char