Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-12-2024, 12:01 AM
yacov yacov is offline copy a paragraph Windows 10 copy a paragraph Office 2016
Competent Performer
copy a paragraph
 
Join Date: Oct 2019
Posts: 146
yacov is on a distinguished road
Default copy a paragraph

Hi,
I have been using this macro for a long time to copy a paragraph. Recently it has stopped working and is giving an error message: run time eroro 4605
Does anyone have any idea what happened
Thanks a lot.



Sub COPYPAR()
'Copy a range from the current para's start till after "=" and


'paste it after the para.

Dim oRng As Range
Set oRng = Selection.Paragraphs(1).Range
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With oRng.Find
.Text = "="
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
If .Execute Then
oRng.Start = Selection.Paragraphs(1).Range.Start
oRng.Copy
Selection.Paragraphs(1).Range.Select
Selection.Collapse wdCollapseEnd
Selection.InsertAfter vbCr
Selection.Collapse
Selection.PasteAndFormat (wdFormatOriginalFormatting)
End If
End With
End Sub
Reply With Quote
  #2  
Old 12-12-2024, 04:26 AM
gmaxey gmaxey is offline copy a paragraph Windows 10 copy a paragraph Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Your procedure seems to work here. You could try to simplifiy:

Code:
Sub COPYPAR()
Dim oPar As Paragraph, oRng As Range
  Set oPar = Selection.Paragraphs(1)
  Set oRng = oPar.Range
  With oRng.Find
    .ClearFormatting
    .Text = "="
    If .Execute Then
      oRng.Start = oPar.Range.Start
      oPar.Range.InsertAfter oRng.FormattedText & vbCr
    End If
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 12-12-2024, 04:31 AM
Italophile Italophile is offline copy a paragraph Windows 11 copy a paragraph 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
  #4  
Old 12-12-2024, 04:34 AM
yacov yacov is offline copy a paragraph Windows 10 copy a paragraph Office 2016
Competent Performer
copy a paragraph
 
Join Date: Oct 2019
Posts: 146
yacov is on a distinguished road
Default

works great. thanks alot.
Reply With Quote
  #5  
Old 12-12-2024, 04:47 AM
gmaxey gmaxey is offline copy a paragraph Windows 10 copy a paragraph Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Which one works great? The two posts provide different solutions.
Attached Images
File Type: jpg Test.jpg (78.5 KB, 13 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 12-12-2024, 01:21 PM
yacov yacov is offline copy a paragraph Windows 10 copy a paragraph Office 2016
Competent Performer
copy a paragraph
 
Join Date: Oct 2019
Posts: 146
yacov is on a distinguished road
Default

In fact, both macros work without problems.
Reply With Quote
  #7  
Old 12-12-2024, 01:25 PM
yacov yacov is offline copy a paragraph Windows 10 copy a paragraph Office 2016
Competent Performer
copy a paragraph
 
Join Date: Oct 2019
Posts: 146
yacov is on a distinguished road
Default

hi Greg. the solution 1 is better for me.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro/VBA to copy text paragraph and insert as new row in summary table, with RAG l0zzie88 Word VBA 2 01-18-2024 03:42 PM
Copy the entire paragraph in wildcards asderam Word VBA 0 01-28-2021 05:54 PM
copy a paragraph Macro to search for a particular word, copy the entire paragraph to a new document Productivity Word VBA 2 10-25-2019 06:40 AM
copy a paragraph Direct (copy-paste) reference from another paragraph (CITATION) WordNoobx Word 6 05-15-2018 05:46 AM
Copy text to new document based on paragraph numbering mike.mm Word VBA 7 11-22-2016 06:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:25 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft