lkpederson
02-03-2015, 01:29 PM
Here's an example of the text to change:
Style 1. text
Style 2. text
Style 3. text
Style 2. Text we're searching for
Style 3. text
Style 4. Change this from Style 4 to Style 4A
Style 4. Same as above.
Style 3. text
Style 4. Same as Style 4 above. Stop style changes from Style 4 to Style 4A.
Style 2. text
Style 3. text
The code I currently have selects the colored in green, as it should, however the style replacing after changes ALL Style 4's to Style 4A's. Not helpful. See code below. Thank you.
Sub FormatRefStds() Selection.HomeKey Unit:=wdStory
Application.ScreenUpdating = False
Dim RngFnd As Range, k As Integer, m As Integer
With ActiveDocument
Set RngFnd = .Range
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Text = ""
.Text = "REFERENCE STANDARDS"
.Style = "_03_CSI ARTICLE"
.Format = True
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.Execute
End With
If .Find.found = True Then
.Start = .Paragraphs.Last.Range.End
RngFnd.Start = .Start
End If
With .Find
.Text = ""
.Execute
End With
If .Find.found Then RngFnd.End = .Start
End With
With .Range
With .Find
.Text = ""
.Replacement.Text = ""
.Style = "_05_CSI Subparagraph 1"
.Replacement.Style = "_05RS_CSI Subparagraph1 - Reference Standards"
.MatchWildcards = False
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End With
End With
Application.ScreenUpdating = True
End Sub
Style 1. text
Style 2. text
Style 3. text
Style 2. Text we're searching for
Style 3. text
Style 4. Change this from Style 4 to Style 4A
Style 4. Same as above.
Style 3. text
Style 4. Same as Style 4 above. Stop style changes from Style 4 to Style 4A.
Style 2. text
Style 3. text
The code I currently have selects the colored in green, as it should, however the style replacing after changes ALL Style 4's to Style 4A's. Not helpful. See code below. Thank you.
Sub FormatRefStds() Selection.HomeKey Unit:=wdStory
Application.ScreenUpdating = False
Dim RngFnd As Range, k As Integer, m As Integer
With ActiveDocument
Set RngFnd = .Range
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Text = ""
.Text = "REFERENCE STANDARDS"
.Style = "_03_CSI ARTICLE"
.Format = True
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.Execute
End With
If .Find.found = True Then
.Start = .Paragraphs.Last.Range.End
RngFnd.Start = .Start
End If
With .Find
.Text = ""
.Execute
End With
If .Find.found Then RngFnd.End = .Start
End With
With .Range
With .Find
.Text = ""
.Replacement.Text = ""
.Style = "_05_CSI Subparagraph 1"
.Replacement.Style = "_05RS_CSI Subparagraph1 - Reference Standards"
.MatchWildcards = False
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End With
End With
Application.ScreenUpdating = True
End Sub