Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-04-2023, 01:13 AM
RobiNew RobiNew is offline Find and format lines of verse Windows 10 Find and format lines of verse Office 2016
Competent Performer
Find and format lines of verse
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default Find and format lines of verse

I have a tentative code to find consecutive one-line paragraphs (lines of verse) and to replace Chr(13) with (11) at the end of the line. I have uploaded a test docx. Can someone help? Thanks!
Code:
Sub LinesOfVerse()
    For Each Paragraph In ActiveDocument.Paragraphs
TypePara = "No"
NrPara = NrPara + 1
If Paragraph.Range.ComputeStatistics(wdStatisticLines) = 1 Then
    Paragraph.Range.Select
    Paragraph.Range.Characters.Last.Text = vbNullString
    Paragraph.Range.Characters.Last.Text = Chr(11)
    Paragraph.Range.Characters.Last.Text = vbNullString
    Paragraph.Range.Characters.Last.Text = Chr(11)
    TypePara = "Yes"
End If
If Paragraph.Range.ComputeStatistics(wdStatisticLines) = 1 _
And TypePara = "Yes" Then
    Paragraph.Range.Select
    Paragraph.Range.Characters.Last.Text = vbNullString
    Paragraph.Range.Characters.Last.Text = Chr(11)
    Paragraph.Range.Characters.Last.Text = vbNullString
    Paragraph.Range.Characters.Last.Text = Chr(11)
'    MsgBox "Check!"
End If
    Next
     End Sub

Attached Files
File Type: docx Lines of verse.docx (45.1 KB, 5 views)
Reply With Quote
  #2  
Old 11-04-2023, 04:34 AM
vivka vivka is offline Find and format lines of verse Windows 7 64bit Find and format lines of verse Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! La prochaine has come. Regarding you code, I'd replace vbnull with chr(32) or " " to insert space. Next, 1) do you want to merge a one-line para with the next one-line para or with all one-line paras that follow or 2) do you want to merge a one-line para with any following para?
Reply With Quote
  #3  
Old 11-04-2023, 05:02 AM
vivka vivka is offline Find and format lines of verse Windows 7 64bit Find and format lines of verse Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

RobiNew, is this is what you want to get?
Attached Images
File Type: png Test.png (5.7 KB, 25 views)
Reply With Quote
  #4  
Old 11-04-2023, 07:23 AM
RobiNew RobiNew is offline Find and format lines of verse Windows 10 Find and format lines of verse Office 2016
Competent Performer
Find and format lines of verse
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

No, Vivka, that is not what I want to get. No merging of paragraphs, but replacing of "^p" with "^l" in consecutive one-line paragraphs, as in the new docx I have posted. Thanks!
Attached Files
File Type: docx Lines of verse2.docx (45.8 KB, 1 views)
Reply With Quote
  #5  
Old 11-04-2023, 08:15 AM
vivka vivka is offline Find and format lines of verse Windows 7 64bit Find and format lines of verse Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

RobiNew, here you are, enjoy it!
Code:
Sub OneLiners()
'In selection, replace Chr(13) with Chr(11) between all consecutive one-line paras.

Dim oRng
Application.ScreenUpdating = False
Set oRng = selection.range
    For Each Para In oRng.Paragraphs
        If Para.range.End >= oRng.End Then Exit Sub
        If Para.range.ComputeStatistics(wdStatisticLines) = 1 And _
            Para.Next.range.ComputeStatistics(wdStatisticLines) = 1 Then
            Para.range.Characters.Last.text = Chr(11)
            Do While Para.Next.range.ComputeStatistics(wdStatisticLines) = 1
                Para.range.Characters.Last.text = Chr(11)
            Loop
        End If
    Next
Application.ScreenUpdating = True
Set oRng = Nothing
End Sub
Reply With Quote
  #6  
Old 11-04-2023, 08:39 AM
RobiNew RobiNew is offline Find and format lines of verse Windows 10 Find and format lines of verse Office 2016
Competent Performer
Find and format lines of verse
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks a lot, Vivka! But what I need is a bit more sophisticated. The macro should first detect one or more consecutive one-line paragraphs and then operate with your code. In other words, it should operate in the absence of a specific selection. Is that possible? I know you can do it. Thanks!
Reply With Quote
  #7  
Old 11-04-2023, 08:56 AM
vivka vivka is offline Find and format lines of verse Windows 7 64bit Find and format lines of verse Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

RobiNew, replace

Set oRng = selection.range
with
Set oRng = ActiveDocument.range

and the macro will work on the whole doc. Or maybe I don't quite understand your point.
Reply With Quote
  #8  
Old 11-04-2023, 10:00 AM
RobiNew RobiNew is offline Find and format lines of verse Windows 10 Find and format lines of verse Office 2016
Competent Performer
Find and format lines of verse
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks a lot, Vivka! It's perfect now. Why are you still labelled as "Beginner"?!
Reply With Quote
  #9  
Old 11-04-2023, 10:37 AM
vivka vivka is offline Find and format lines of verse Windows 7 64bit Find and format lines of verse Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

You are welcome, RobiNew! I'm not a professional. It's my hobby and I'm still learning. This forum is my university.
I realise that my macros are not perfect, but practice makes perfect.
The following code is two-lines shorter:

Code:
Sub OneLiners()
'In selection, replace Chr(13) with Chr(11) between all
'consecutive one-line paras.

Dim oRng
Application.ScreenUpdating = False
Set oRng = ActiveDocument.range
    For Each Para In oRng.Paragraphs
'Prevent returning an error if the next para is nothing:
        If Para.range.End >= oRng.End Then Exit Sub
        If Para.range.ComputeStatistics(wdStatisticLines) = 1 Then
            Do While Para.Next.range.ComputeStatistics(wdStatisticLines) = 1
                Para.range.Characters.Last.text = Chr(11)
            Loop
        End If
    Next
Application.ScreenUpdating = True
Set oRng = Nothing
End Sub
Reply With Quote
  #10  
Old 11-04-2023, 11:12 AM
RobiNew RobiNew is offline Find and format lines of verse Windows 10 Find and format lines of verse Office 2016
Competent Performer
Find and format lines of verse
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Anyway, you're certainly not a beginner! And many thanks for the new code!
Reply With Quote
  #11  
Old 11-06-2023, 01:45 AM
RobiNew RobiNew is offline Find and format lines of verse Windows 10 Find and format lines of verse Office 2016
Competent Performer
Find and format lines of verse
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi, Vivka! I'm using your code here above, but I need to insert two conditions:
-- skip if paragraph is centered
-- and skip if paragraph contains a numeral
I've tried the two sets of code here below, but the second condition does not work.


Para.Range.Select
If Para.Range.ParagraphFormat.Alignment <> wdAlignParagraphCenter _
And Mid(Para.Range, 1) <> "^#" Then

Para.Range.Select
If Para.Range.ParagraphFormat.Alignment <> wdAlignParagraphCenter _
And Not IsNumeral(Mid(Para.Range, 1)) Then


Can you help? Thanks!
Reply With Quote
  #12  
Old 11-06-2023, 12:29 PM
vivka vivka is offline Find and format lines of verse Windows 7 64bit Find and format lines of verse Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! The following macro skips one-liners clusters if their first para is center-aligned or has a digit:
Code:
Sub OneLiners()
'In selection, replace Chr(13) with Chr(11) between all
'consecutive one-line paras, if....

Dim oRng
Application.ScreenUpdating = False
Set oRng = ActiveDocument.range
    For Each Para In oRng.Paragraphs
        If Para.range.End >= oRng.End Then Exit Sub
        If Para.range.ParagraphFormat.Alignment = wdAlignParagraphCenter _
            Or Para.range Like "*[0-9]*" Then GoTo nexxt
        If Para.range.ComputeStatistics(wdStatisticLines) = 1 Then
            Do While Para.Next.range.ComputeStatistics(wdStatisticLines) = 1
                Para.range.Characters.Last.text = Chr(11)
            Loop
        End If
nexxt:
    Next
Application.ScreenUpdating = True
Set oRng = Nothing
End Sub
If you want to see if other paras meet the requirements, place the If statement in other place (experiment with it).
Reply With Quote
  #13  
Old 11-07-2023, 01:33 AM
RobiNew RobiNew is offline Find and format lines of verse Windows 10 Find and format lines of verse Office 2016
Competent Performer
Find and format lines of verse
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks a lot, Vivka! Everything seems OK now. I am really grateful.
Reply With Quote
  #14  
Old 11-07-2023, 06:39 AM
vivka vivka is offline Find and format lines of verse Windows 7 64bit Find and format lines of verse Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

You are welcome, RobiNew!
Reply With Quote
  #15  
Old 11-07-2023, 12:22 PM
gmaxey gmaxey is online now Find and format lines of verse Windows 10 Find and format lines of verse Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Vivka,


I don't know if any of us here are professionals or if any of us have stopped learning from others. RobiNew, Vivka status flag is based solely on post count.


I looked at the code you posted Vivka and have the following comments/suggestions:
-I try to avoid GoTo
-While what you posted seems to work for the OP, it seems that if a one line paragraph was nested with other one line paragraphs (not the first) then the OP stated conditions were not handled.
-Rest of comments are inline with the code.


Code:
Sub OneLinersII()
'In selection, replace Chr(13) with Chr(11) between all
'consecutive one-line paras, if....
Dim oPar As Paragraph 'If you dim one variable, why don't you dim all variables?
Dim oRng As Range 'You declared the range object as a variant.  Why not as a range?
  Application.ScreenUpdating = False
  Set oRng = ActiveDocument.Range
  For Each oPar In oRng.Paragraphs
    If oPar.Range.End >= oRng.End Then Exit For 'If you Exit Sub then you won't execute your ScreenUpdating line
    If Not oPar.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter _
      And Not oPar.Range Like "*[0-9]*" Then
      If oPar.Range.ComputeStatistics(wdStatisticLines) = 1 Then
        Do While oPar.Next.Range.ComputeStatistics(wdStatisticLines) = 1 _
          And Not oPar.Range.Next.ParagraphFormat.Alignment = wdAlignParagraphCenter _
          And Not oPar.Next.Range Like "*[0-9]*"
          oPar.Range.Characters.Last.Text = Chr(11)
        Loop
      End If
    End If
  Next oPar
  Application.ScreenUpdating = True
  Set oRng = Nothing: Set oPar = Nothing
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by gmaxey; 11-08-2023 at 04:32 AM.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Format Word Document - Remove timestamps on individual lines lbeck Word 4 06-21-2023 12:47 PM
How do I format indented bulleted lines??? HELP PLEASE!!! nicholeproffitt Word 1 02-05-2015 05:42 PM
Find and format lines of verse Print table and on verse form vba LaercioNB Excel 1 08-09-2013 06:03 PM
How to remove unwanted lines - I cannot even find how/where they are inserted! nickib Word 7 08-09-2013 06:01 AM
Find and format lines of verse Find and Replace multiple lines/paragraphs jcw Word 1 11-18-2011 11:47 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:53 PM.


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