View Full Version : Replacing a Paragraph with the Same Formating
heedaf
07-19-2017, 09:12 AM
I've got a paragraph with some words that are struck through and I would like to modify the paragraph (removing some words) and then replace the paragraph with the new modification and maintain the same formatting.  I know I could do this with copy and paste but that would be too slow.  Is there a way of doing this with a range (or something else)?  Everything I've tried removes the formatting.
heedaf
07-19-2017, 06:27 PM
Using oRng.formattedText works - but not for what I'm trying to do.  I'm converting a document that comes over as a "section: 1" and I need to change it to "Header 1" when this is done I loose the formatting for header title.  So I thought I can use formattedtext to store the formatting in a range but I can't place this text/formatting back to the same range.  I can anywhere else (oRng.text = oRng.text does work - which is strange).  Does anyone have any idea how I can change a heading and preserve the formatting on the title?
set NewRange = oRng.FormattedText
oRng.FormattedText = NewRange
Even using:
Set MyRange = oRng.FormattedText
Selection.FormattedText = MyRange
Doesn't work.
gmayor
07-19-2017, 08:28 PM
You can remove words from a selection without affecting the formatting, but if manual formatting is applied to the selection or parts of it , changing the style by any method will follow the rules relating to styles.
Sub Macro1()
'Graham Mayor - http://www.gmayor.com - Last updated - 20 Jul 2017
Dim oRng As Range, oSel As Range
    Set oRng = Selection.Paragraphs(1).Range
    'oRng.Style = "Heading 1"
    Set oSel = Selection.Paragraphs(1).Range
    With oSel.Find
        Do While .Execute(FindText:="dolore")    'the word or phrase to remove
            If oSel.InRange(oRng) Then
                oSel.Text = ""
                oSel.Collapse 0
            Else
                GoTo lbl_Exit
            End If
        Loop
    End With
lbl_Exit:
    Set oSel = Nothing
    Set oRng = Nothing
    Exit Sub
End Sub
heedaf
07-19-2017, 08:37 PM
Thank you for the reply!  So there is no easy way to restore the formatting once the style has changed the formatting?
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.