Thread: [Solved] copy a paragraph
View Single Post
 
Old 12-12-2024, 04:31 AM
Italophile Italophile is offline Windows 11 Office 2021
Expert
 
Join Date: Mar 2022
Posts: 554
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

I don't know why your code is getting an error, but there is a better way to achieve what you want without using the clipboard.

Code:
Sub COPYPAR()
    'Copy a range from the current para's start till after "=" and
    'add it after the para.

    Dim selRng As Range: Set selRng = Selection.Paragraphs(1).Range
    
    Dim copyRng As Range: Set copyRng = selRng.Duplicate
    'make sure that = is found
    If Not copyRng.MoveEndUntil("=", wdBackward) = 0 Then
        'make sure that the = is in the same paragraph
        If copyRng.InRange(selRng) Then
            selRng.InsertParagraphAfter
            selRng.Collapse wdCollapseEnd
            selRng.FormattedText = copyRng.FormattedText
        End If
    End If
End Sub
Reply With Quote