Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-11-2021, 08:54 AM
effimera effimera is offline How to duplicate paragraphs & format them Windows 10 How to duplicate paragraphs & format them Office 2019
Novice
How to duplicate paragraphs & format them
 
Join Date: Jun 2021
Posts: 8
effimera is on a distinguished road
Default How to duplicate paragraphs & format them

Hello there,

I am wondering if someone could help me with a similar task of duplicating paragraphs (except in tables, not in textboxes either) by using VBA.

I tried to record a macro that duplicates source paragraphs underneath and italicizes the duplicated ones at the same time, but it doesn't work properly.
I recorded it with 3 paragraphs, but this is the problem -- if I run the macro on larger text (i.e. with more than three paragraphs), it does so on the first three only, and the rest remains unduplicated.


So, does anybody know how to refine it in order to duplicate and italicize an unlimited number of paragraphs. And please with no locking, as I would often need to select and hide or format them for CAT translation.

Here it is:
Code:
  Sub DuplicateAndItalicizeParagraph()
  '
  ' DuplicateAndItalicizeParagraph Makro
  '
  '
      Selection.HomeKey Unit:=wdStory
      Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
      Selection.Copy
      Selection.MoveRight Unit:=wdCharacter, Count:=1
      Selection.TypeParagraph
      Selection.MoveUp Unit:=wdLine, Count:=1
      Selection.PasteAndFormat (wdFormatOriginalFormatting)
      Selection.MoveLeft Unit:=wdCharacter, Count:=1
      Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend
      Selection.Font.Italic = wdToggle
      Selection.MoveRight Unit:=wdCharacter, Count:=3
      Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
      Selection.Copy
      Selection.MoveRight Unit:=wdCharacter, Count:=1
      Selection.TypeParagraph
      Selection.MoveUp Unit:=wdLine, Count:=1
      Selection.PasteAndFormat (wdFormatOriginalFormatting)
      Selection.MoveLeft Unit:=wdCharacter, Count:=1
      Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend
      Selection.Font.Italic = wdToggle
      Selection.MoveRight Unit:=wdCharacter, Count:=3
      Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
      Selection.Copy
      Selection.MoveRight Unit:=wdCharacter, Count:=1
      Selection.TypeParagraph
      Selection.PasteAndFormat (wdFormatOriginalFormatting)
      Selection.MoveLeft Unit:=wdCharacter, Count:=1
      Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend
      Selection.Font.Italic = wdToggle
      Selection.MoveRight Unit:=wdCharacter, Count:=1
  End Sub
Thank you in advance.
Reply With Quote
  #2  
Old 12-16-2021, 09:57 PM
gmayor's Avatar
gmayor gmayor is offline How to duplicate paragraphs & format them Windows 10 How to duplicate paragraphs & format them Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Without the document to test, the following should work

Code:
Sub Macro1()
Dim oRng As Range
Dim oPara As Range, oNewPara As Range
Dim i As Long
    Set oRng = Selection.Range
    If oRng.End = ActiveDocument.Range.End Then
        oRng.InsertParagraphAfter
    End If
    For i = oRng.Paragraphs.Count To 1 Step -1
        Set oPara = oRng.Paragraphs(i).Range
        Set oNewPara = oPara.Duplicate
        oNewPara.Collapse 0
        oNewPara.FormattedText = oPara.FormattedText
        oNewPara.Font.Italic = True
    Next i
lbl_Exit:
    Set oRng = Nothing
    Set oPara = Nothing
    Set oNewPara = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 12-20-2021, 04:40 AM
effimera effimera is offline How to duplicate paragraphs & format them Windows 10 How to duplicate paragraphs & format them Office 2019
Novice
How to duplicate paragraphs & format them
 
Join Date: Jun 2021
Posts: 8
effimera is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Without the document to test, the following should work

Code:
Sub Macro1()
Dim oRng As Range
Dim oPara As Range, oNewPara As Range
Dim i As Long
    Set oRng = Selection.Range
    If oRng.End = ActiveDocument.Range.End Then
        oRng.InsertParagraphAfter
    End If
    For i = oRng.Paragraphs.Count To 1 Step -1
        Set oPara = oRng.Paragraphs(i).Range
        Set oNewPara = oPara.Duplicate
        oNewPara.Collapse 0
        oNewPara.FormattedText = oPara.FormattedText
        oNewPara.Font.Italic = True
    Next i
lbl_Exit:
    Set oRng = Nothing
    Set oPara = Nothing
    Set oNewPara = Nothing
    Exit Sub
End Sub

Thank you, gmayor!
It works perfectly - much obliged
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to duplicate paragraphs & format them Duplicate paragraphs except in tables jalve Word VBA 21 05-26-2021 07:38 PM
How to duplicate paragraphs & format them Format multiple paragraphs differently Dave T Word VBA 7 07-31-2019 08:12 PM
How to duplicate paragraphs & format them delete 1 or 2 adjacent duplicate paragraphs, macro moorea21 Word 4 11-01-2018 12:53 PM
How to duplicate paragraphs & format them How to find duplicate phrases/paragraphs in a long document iamgator Word VBA 5 12-27-2016 01:34 AM
How to duplicate paragraphs & format them VBA to set format for paragraphs that meet with specific requirements AustinBrister Word VBA 3 06-01-2015 07:00 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:41 AM.


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