Apply a user-defined character style to all ocurrences of manual boldformatting but skipping some sp

A

andreas

Dear Experts:

Below macro applies a user-defined character style to all words/
characters that have manually been formatted bold.

How can I make sure that the macro SKIPS paragraphs where the bold
formatting is part of the paragraph style. For example, this is the
case for all the built-in heading styles (wdStyleHeading1,2,3,4,5).

Help is much appreciated. Thank you very much in advance. Regards,
Andreas


Sub ApplyCharStyleToManuallyBold()

If MsgBox("Characters that have MANUALLY been formatted bold are
applied a user-defined character style." & _ vbCrLf & _
"Would you like to continue?", vbYesNo, "Formatierung der Kopfzeile
der Tabelle") = vbYes Then


Set rng = ActiveDocument.StoryRanges(wdMainTextStory)
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Font.Bold = True
.Replacement.Style = "Character_Style_Bold"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = False

.Execute Replace:=wdReplaceAll
End With
End If

End Sub
 
H

Helmut Weber

Hi Andreas,

what about the idea, that the command
range.font.reset
reveals, whether there was formatting manually applied?

Something along these lines, maybe:

Sub Macro7()
Dim rDcm As Range
Set rDcm = ActiveDocument.StoryRanges(wdMainTextStory)
With rDcm.Find
.Font.Bold = True
While .Execute
rDcm.Font.Reset
' rDcm.Select ' for testing
If rDcm.Font.Bold = False Then
rDcm.Style = "BoldStyle"
End If
' rDcm.Select ' for testing
rDcm.start = rDcm.End
rDcm.End = ActiveDocument.StoryRanges(wdMainTextStory).End
Wend
End With
End Sub

If you create a new range, by
Set rDcm = ActiveDocument.StoryRanges(wdMainTextStory)
it is not necessary to care about find.options like
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
etc.

All options are set to their default values.
However, I don't know, what they are. ;-)

Also
activedocument.range seems to be equivalent to
ActiveDocument.StoryRanges(wdMainTextStory).

But I'm not sure about that either.

Whithout covering all possible complications, of course,

cheers

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

andreas

Hi Andreas,

what about the idea, that the command
range.font.reset
reveals, whether there was formatting manually applied?

Something along these lines, maybe:

Sub Macro7()
Dim rDcm As Range
Set rDcm = ActiveDocument.StoryRanges(wdMainTextStory)
With rDcm.Find
   .Font.Bold = True
   While .Execute
      rDcm.Font.Reset
      ' rDcm.Select ' for testing
      If rDcm.Font.Bold = False Then
         rDcm.Style = "BoldStyle"
      End If
      ' rDcm.Select ' for testing
   rDcm.start = rDcm.End
   rDcm.End = ActiveDocument.StoryRanges(wdMainTextStory).End
   Wend
End With
End Sub

If you create a new range, by
Set rDcm = ActiveDocument.StoryRanges(wdMainTextStory)
it is not necessary to care about find.options like
   .ClearFormatting
   .Replacement.ClearFormatting
   .Forward = True
   .Wrap = wdFindContinue
etc.

All options are set to their default values.
However, I don't know, what they are. ;-)

Also
activedocument.range seems to be equivalent to
ActiveDocument.StoryRanges(wdMainTextStory).

But I'm not sure about that either.

Whithout covering all possible complications, of course,

cheers

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

Hey Helmut,

thanks for the quick answer. I ran it for about 15 min on a document
with 50 pages. I interrupted it since I thought that it had fallen
into an endless loop. I will do some longer testing and let you then.
Regards, Andreas
 
A

andreas

Hi Andreas,

what about the idea, that the command
range.font.reset
reveals, whether there was formatting manually applied?

Something along these lines, maybe:

Sub Macro7()
Dim rDcm As Range
Set rDcm = ActiveDocument.StoryRanges(wdMainTextStory)
With rDcm.Find
   .Font.Bold = True
   While .Execute
      rDcm.Font.Reset
      ' rDcm.Select ' for testing
      If rDcm.Font.Bold = False Then
         rDcm.Style = "BoldStyle"
      End If
      ' rDcm.Select ' for testing
   rDcm.start = rDcm.End
   rDcm.End = ActiveDocument.StoryRanges(wdMainTextStory).End
   Wend
End With
End Sub

If you create a new range, by
Set rDcm = ActiveDocument.StoryRanges(wdMainTextStory)
it is not necessary to care about find.options like
   .ClearFormatting
   .Replacement.ClearFormatting
   .Forward = True
   .Wrap = wdFindContinue
etc.

All options are set to their default values.
However, I don't know, what they are. ;-)

Also
activedocument.range seems to be equivalent to
ActiveDocument.StoryRanges(wdMainTextStory).

But I'm not sure about that either.

Whithout covering all possible complications, of course,

cheers

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

Dear Helmut,

now I did some testing on a little document. It is running just fine.
On longer documents it takes quite some time to complete. But still it
is working just fine.

Thank you very much again for your professinal help. Regards, Andreas
 

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