1
votes

I'm working on creating a macro in Microsoft Word (2007) for a document that contains text such as this:

(1) Bold heading. Normal text.

With this text I'd like to perform a number of transformations upon the first part - (1) Bold heading. - of that text.

While "(1)" and "Bold heading." have a consistent style (bold and Arial), the space between the two does not (it's Times New Roman, non-bold).

I thought a search for the below would work, without any format restrictions.

"^13(\([0-9]@\)) (?@)."

Unfortunately, there's also cases where text is as follows:

(1) Normal text.

For blocks like this, I want to completely skip the text.

Unfortunately, my wildcard search is going to find these instances too, unless I can restrict it by font styles.

If I could normalize the space in the first case, then I could add the Font restrictions on my wildcard search to grab the correct content.

.Text = "^13(\([0-9]@\)) (?@)."
.Font.Name = "Arial"
.Font.Size = 9
.Font.Bold = True

But, I'd need to be able to grab two differently formatted items in a search to normalize that space, which, from my limited knowledge of VBA, doesn't appear to be possible.

Is there a way to find text with different formatting, in a Word macro?

Thanks!

2
I am not quite sure where the difficulty is, you can examine the document sentence by sentence or character by character. Perhaps you could add a line or two to your post?Fionnuala
Thanks Remou. I've updated the question to clarify that I want to transform that block of text, and stressed that my search would find too many instances. Hopefully that helps.James Skemp

2 Answers

4
votes

I wonder if something like this would suit:

Dim s As Range
Dim wd As Range
Dim BoldHead As Boolean
Dim doc As Document

Set doc = Word.Documents("Doc2.doc")

For Each s In doc.Sentences
    If s.Words(1).Bold = True Then
        BoldHead = True
        For Each wd In s.Words
            If Trim(wd) <> vbNullString _
                And wd <> "." _
                And wd.Bold = False Then
                BoldHead = False
            End If
        Next
        If BoldHead Then
            Debug.Print s
        End If
    End If
Next

Note that Word has a nasty enough habit of not counting the numbers, it sees them as automatic.

1
votes

Remou's answer is exactly what I needed, but since StackOverflow is a great resource, this is what I ended up tweaking it to for our particular case:

In particular, the text is within the first sentence of a paragraph. Unfortunately this doesn't seem to catch all of our cases, but it grabs most of them, and gets the user most of the way there.

(Some of the comments below were included on external resources I'd found, so whether they're actually necessary is questionable, but ... it works.)

' Do our bold heading replacements
Dim s As Range, p As Paragraph
Dim wd As Range
Dim BoldHead As Boolean
Dim doc As Document

Set doc = ActiveDocument

For Each p In doc.Paragraphs
    Set s = p.Range.Sentences(1)
    If s.Words(1).Bold = True And s.Words(1).Characters(1) = "(" Then
        BoldHead = True
        For Each wd In s.Words
            If Trim(wd) <> vbNullString _
                And wd <> "." _
                And wd.Bold = False Then
                BoldHead = False
            End If
        Next
        If BoldHead Then
            With s.Find
                ' Clear all previously set formatting for Find dialog box.
                .ClearFormatting
                .Text = "(\([0-9]@\)) (?@)."

                ' Clear all previously set formatting for Replace dialog box.
                .Replacement.ClearFormatting
                .Replacement.Text = "\1 \2."
                .Replacement.Font.SmallCaps = True
                .Replacement.Font.Name = "Times New Roman"
                .Replacement.Font.Bold = False

                ' The following parameters must be set as follows to find only text formatted for the specified font.
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = True
                .MatchSoundsLike = False
                .MatchAllWordForms = False

                .Execute Replace:=wdReplaceOne
            End With

            With s.Find
                ' Clear all previously set formatting for Find dialog box.
                .ClearFormatting
                .Text = "(\([0-9]@\)) "

                ' Clear all previously set formatting for Replace dialog box.
                .Replacement.ClearFormatting
                .Replacement.Text = "\1" & vbTab
                .Replacement.Font.SmallCaps = False
                .Replacement.Font.Name = "Arial"
                .Replacement.Font.Bold = True

                ' The following parameters must be set as follows to find only text formatted for the specified font.
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = True
                .MatchSoundsLike = False
                .MatchAllWordForms = False

                .Execute Replace:=wdReplaceOne
            End With
        End If
    End If
Next