Find/replace shading - should be simple

W

wal

Word 2003

The following code (based on the Word MVP site) works to change the
formatting as shown at the ************** lines:

Sub FindReplaceFormatting()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Font.Italic = True **************
.Replacement.Font.Bold = True **************
.MatchCase = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchWholeWord = False
.Execute Replace:=wdReplaceAll
End With
End Sub

But if I replace the ************** lines with, respectively, the
following, nothing happens (shaded text stays wdColorGray30).

.Font.Shading.BackgroundPatternColor = wdColorGray30
.Replacement.Font.Shading.BackgroundPatternColor = wdColorGray80

If I use wdFindAsk for the .Wrap line, the correct number of items in
wdColorGray30 is announced, but the change does not take place. Do I
need to adjust the code? Or does this just not work in Word VBA?
THANKS.
 
S

Stefan Blom

Note that there is also a ForeGroundPatternColor property for Shading
objects in Word. Are you sure you are working with the correct property? If
you record the find and replace operation that you want, what are the
results of the recording?

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"wal" wrote in message

Word 2003

The following code (based on the Word MVP site) works to change the
formatting as shown at the ************** lines:

Sub FindReplaceFormatting()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Font.Italic = True **************
.Replacement.Font.Bold = True **************
.MatchCase = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchWholeWord = False
.Execute Replace:=wdReplaceAll
End With
End Sub

But if I replace the ************** lines with, respectively, the
following, nothing happens (shaded text stays wdColorGray30).

.Font.Shading.BackgroundPatternColor = wdColorGray30
.Replacement.Font.Shading.BackgroundPatternColor = wdColorGray80

If I use wdFindAsk for the .Wrap line, the correct number of items in
wdColorGray30 is announced, but the change does not take place. Do I
need to adjust the code? Or does this just not work in Word VBA?
THANKS.
 
W

wal

Thanks, but no, it's BackgroundPatternColor.

Unfortunately, a find-replace macro for this purpose can't be recorded
because: (1) shading is not an option in the Find/Replace dialog and
(2) in any event most if not all formatting does not get recorded when
you attempt to record a find-replace macro with the recorder.

Also, when I run the macro as shown on a selection of text, the dialog
at the end says, "... 8 [or whatever] replacements were made. Do you
want to search the remainder..." (But nothing is actually changed.)
If I change the code to ForegroundPatternColor, the dialog says, "...
0 replacements were made..."
 
S

Stefan Blom

OK, since it isn't an option in the user interface, that suggests it isn't
supported when finding and replacing in VBA either.

You could work directly with range objects, searching and replacing shading
character by character. It would be slow, I suspect, but it would do the
job. Something like this:

Dim c As Range
For Each c In ActiveDocument.Characters
If c.Font.Shading.BackgroundPatternColor = wdColorGray30 Then
c.Font.Shading.BackgroundPatternColor = wdColorGray80
End If
Next c

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"wal" wrote in message

Thanks, but no, it's BackgroundPatternColor.

Unfortunately, a find-replace macro for this purpose can't be recorded
because: (1) shading is not an option in the Find/Replace dialog and
(2) in any event most if not all formatting does not get recorded when
you attempt to record a find-replace macro with the recorder.

Also, when I run the macro as shown on a selection of text, the dialog
at the end says, "... 8 [or whatever] replacements were made. Do you
want to search the remainder..." (But nothing is actually changed.)
If I change the code to ForegroundPatternColor, the dialog says, "...
0 replacements were made..."
 
W

wal

That does the job. Thanks!

OK, since it isn't an option in the user interface, that suggests it isn't
supported when finding and replacing in VBA either.

You could work directly with range objects, searching and replacing shading
character by character. It would be slow, I suspect, but it would do the
job. Something like this:

Dim c As Range
For Each c In ActiveDocument.Characters
If c.Font.Shading.BackgroundPatternColor = wdColorGray30 Then
    c.Font.Shading.BackgroundPatternColor = wdColorGray80
End If
Next c

--
Stefan Blom
Microsoft Word MVP

---------------------------------------------"wal"  wrote in message


Thanks, but no, it's BackgroundPatternColor.

Unfortunately, a find-replace macro for this purpose can't be recorded
because: (1) shading is not an option in the Find/Replace dialog and
(2) in any event most if not all formatting does not get recorded when
you attempt to record a find-replace macro with the recorder.

Also, when I run the macro as shown on a selection of text, the dialog
at the end says, "... 8 [or whatever] replacements were made.  Do you
want to search the remainder..." (But nothing is actually changed.)
If I change the code to ForegroundPatternColor, the dialog says, "...
0 replacements were made..."

Note that there is also a ForeGroundPatternColor property for Shading
objects in Word. Are you sure you are working with the correct property?
If
you record the find and replace operation that you want,  what are the
results of the recording?
---------------------------------------------"wal"  wrote in message

Word 2003
The following code (based on the Word MVP site) works to change the
formatting as shown at the ************** lines:
Sub FindReplaceFormatting()
With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .Font.Italic = True **************
    .Replacement.Font.Bold = True **************
    .MatchCase = False
    .MatchSoundsLike = False
    .MatchWildcards = False
    .MatchWholeWord = False
    .Execute Replace:=wdReplaceAll
End With
End Sub
But if I replace the ************** lines with, respectively, the
following, nothing happens (shaded text stays wdColorGray30).
    .Font.Shading.BackgroundPatternColor = wdColorGray30
    .Replacement.Font.Shading.BackgroundPatternColor = wdColorGray80
If I use wdFindAsk for the .Wrap line, the correct number of items in
wdColorGray30 is announced, but the change does not take place.  Do I
need to adjust the code?  Or does this just not work in Word VBA?
THANKS.- Hide quoted text -

- Show quoted text -
 
S

Stefan Blom

I'm glad I could help!

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"wal" wrote in message

That does the job. Thanks!

OK, since it isn't an option in the user interface, that suggests it isn't
supported when finding and replacing in VBA either.

You could work directly with range objects, searching and replacing
shading
character by character. It would be slow, I suspect, but it would do the
job. Something like this:

Dim c As Range
For Each c In ActiveDocument.Characters
If c.Font.Shading.BackgroundPatternColor = wdColorGray30 Then
c.Font.Shading.BackgroundPatternColor = wdColorGray80
End If
Next c

--
Stefan Blom
Microsoft Word MVP

---------------------------------------------"wal" wrote in message


Thanks, but no, it's BackgroundPatternColor.

Unfortunately, a find-replace macro for this purpose can't be recorded
because: (1) shading is not an option in the Find/Replace dialog and
(2) in any event most if not all formatting does not get recorded when
you attempt to record a find-replace macro with the recorder.

Also, when I run the macro as shown on a selection of text, the dialog
at the end says, "... 8 [or whatever] replacements were made. Do you
want to search the remainder..." (But nothing is actually changed.)
If I change the code to ForegroundPatternColor, the dialog says, "...
0 replacements were made..."

Note that there is also a ForeGroundPatternColor property for Shading
objects in Word. Are you sure you are working with the correct property?
If
you record the find and replace operation that you want, what are the
results of the recording?
---------------------------------------------"wal" wrote in message

Word 2003
The following code (based on the Word MVP site) works to change the
formatting as shown at the ************** lines:
Sub FindReplaceFormatting()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Font.Italic = True **************
.Replacement.Font.Bold = True **************
.MatchCase = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchWholeWord = False
.Execute Replace:=wdReplaceAll
End With
End Sub
But if I replace the ************** lines with, respectively, the
following, nothing happens (shaded text stays wdColorGray30).
.Font.Shading.BackgroundPatternColor = wdColorGray30
.Replacement.Font.Shading.BackgroundPatternColor = wdColorGray80
If I use wdFindAsk for the .Wrap line, the correct number of items in
wdColorGray30 is announced, but the change does not take place. Do I
need to adjust the code? Or does this just not work in Word VBA?
THANKS.- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top