J
jerem
I'm trying to use the code below to replace some styles and have the user
dictate which style to find and replace, however, before I resolve the user
input part I'm having a problem with the Public Function code at the " If
objStyle.NameLocal = strStyleName Then StyleExists = True". This statement
never rings true despite the fact that there is a p2 style amongst the Styles
and Formatting, Available Formatting List and p2 is contained in the
strStyleName. At first this was perplexing as to why p2 was not being found
in the Style list and then I realized that it was looking to the wrong list
(or should I say not the list I want it to look to). It is looking in the
Bullets and Numbering List Styles which only contains 3 styles in there (as
opposed to many styles in the "Available Formatting List" which p2 is
amongst) which are 1.1/1.1.1/1.1.1.1 and then two other levels and, of
course, because it doesn't find p2 it makes no replacements.
I have tried to look up a replacement for .NameLocal (one that is
equalivalent to look to the "Available Formatting List" not the Bullets and
Numbering style list) with no luck. Any one know how to point the search in
the direction I need?
As always, thanks for your help.
Sub ReplaceStyles()
'FindStyleName = InputBox("What style do you want to find?")
'ReplaceStyleName = InputBox("What style do you want to replace it with?")
Dim objRange As Range
Set objRange = ActiveDocument.Range
With objRange.find
.ClearFormatting
.Replacement.ClearFormatting
If StyleExists("p2") = True Then
.Style = "p2"
.Replacement.Style = "p4"
.Execute Replace:=wdReplaceAll
End If
'The following code is if you want to do more finds and replaces
' If StyleExists("Name2") = True Then
' .Style = "Name2"
' .Replacement.Style = "NewName2"
' .Replacement.ParagraphFormat.SpaceBefore = 0
' .Execute Replace:=wdReplaceAll
' End If
'' Etc., etc.
End With
Set objRange = Nothing
End Sub
Public Function StyleExists(strStyleName As String) As Boolean
' Determines whether or not the target style exists in the
' active document.
Dim objStyle As Style
StyleExists = False
For Each objStyle In ActiveDocument.Styles
If objStyle.NameLocal = strStyleName Then
StyleExists = True
Exit For
End If
Next objStyle
End Function
dictate which style to find and replace, however, before I resolve the user
input part I'm having a problem with the Public Function code at the " If
objStyle.NameLocal = strStyleName Then StyleExists = True". This statement
never rings true despite the fact that there is a p2 style amongst the Styles
and Formatting, Available Formatting List and p2 is contained in the
strStyleName. At first this was perplexing as to why p2 was not being found
in the Style list and then I realized that it was looking to the wrong list
(or should I say not the list I want it to look to). It is looking in the
Bullets and Numbering List Styles which only contains 3 styles in there (as
opposed to many styles in the "Available Formatting List" which p2 is
amongst) which are 1.1/1.1.1/1.1.1.1 and then two other levels and, of
course, because it doesn't find p2 it makes no replacements.
I have tried to look up a replacement for .NameLocal (one that is
equalivalent to look to the "Available Formatting List" not the Bullets and
Numbering style list) with no luck. Any one know how to point the search in
the direction I need?
As always, thanks for your help.
Sub ReplaceStyles()
'FindStyleName = InputBox("What style do you want to find?")
'ReplaceStyleName = InputBox("What style do you want to replace it with?")
Dim objRange As Range
Set objRange = ActiveDocument.Range
With objRange.find
.ClearFormatting
.Replacement.ClearFormatting
If StyleExists("p2") = True Then
.Style = "p2"
.Replacement.Style = "p4"
.Execute Replace:=wdReplaceAll
End If
'The following code is if you want to do more finds and replaces
' If StyleExists("Name2") = True Then
' .Style = "Name2"
' .Replacement.Style = "NewName2"
' .Replacement.ParagraphFormat.SpaceBefore = 0
' .Execute Replace:=wdReplaceAll
' End If
'' Etc., etc.
End With
Set objRange = Nothing
End Sub
Public Function StyleExists(strStyleName As String) As Boolean
' Determines whether or not the target style exists in the
' active document.
Dim objStyle As Style
StyleExists = False
For Each objStyle In ActiveDocument.Styles
If objStyle.NameLocal = strStyleName Then
StyleExists = True
Exit For
End If
Next objStyle
End Function