Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-31-2021, 04:15 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline Help with a logical code, end of paragraph lines Windows 10 Help with a logical code, end of paragraph lines Office 2019
Competent Performer
Help with a logical code, end of paragraph lines
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default Help with a logical code, end of paragraph lines

Hello Pro's,
I'll try my darnest to explain what I need, since it's hard for me to explain without and image, and I don't know how to enclose an image without granting access to my PC. It's a corporate thing.

My issue:



Long paragraphs, that overflows to the next line due to margins, how do I do a Find and Replace with situations where the end of line has an open parentheses, such as:
Here below is 2 examples:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies (the
purus lectus) malesuada libero, sit amet commodo magna eros quis urna.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies (as
purus lectus) malesuada libero, sit amet commodo magna eros quis urna.
So I want to:
  1. Select an area, in a large document;
  2. do a Find and Replace,

If it sees at the end of all sentences, an open parenthese with a Word,
I want to add a non-breaking space = ^s so it will be glued to the following word so a sentence will NOT end with an open parentheses at the end of a line.

So in these above cases: (the^spurus lectus) or (as^spurus lectus).

Is that doable without touching all open braket with a word?

I've been searching. My logic is thinking go to end of sentence and find the sentences that ends with (the or (as.

Any insights would be sooooooooooooooo appreciated

Cendrinne
Reply With Quote
  #2  
Old 05-31-2021, 06:53 PM
macropod's Avatar
macropod macropod is offline Help with a logical code, end of paragraph lines Windows 10 Help with a logical code, end of paragraph lines Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Why not simply do it for all cases? For example, using a wildcard Find/Replace:
Find = (\(*>)^32
Replace = \1^s
That way, regardless of any subsequent edits, you'll still get the desired outcome.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-31-2021, 08:16 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline Help with a logical code, end of paragraph lines Windows 10 Help with a logical code, end of paragraph lines Office 2019
Competent Performer
Help with a logical code, end of paragraph lines
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default Yeah, that could work

Hello, Macropod,

The reason I was trying to avoid doing that, is because, having a whole document justified, makes spaces between words more spaced out. So to equilibrate the space vs letters, at times you don't want to do it everywhere in a paragraph.

That is why I was wondering how to make it happen.

I've looked at the Keyboard shortcuts:

If I manually see at the end of the line, an open bracket with a word:
  • Kb button = End
  • Ctrl+ <--- left arrow + 1 character on left for the bracket
  • Shift-End (to select the word at the end)

I don't think there's a way to program it to do that on it's own right?

Cendrinne
Reply With Quote
  #4  
Old 06-01-2021, 01:33 AM
Guessed's Avatar
Guessed Guessed is offline Help with a logical code, end of paragraph lines Windows 10 Help with a logical code, end of paragraph lines Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This appears to work but I would need to see a big data set to be confident in it
Code:
Sub NoBreaksForTheWicked()
  Dim aRng As Range, iLine1 As Integer, iLine2 As Integer
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .Text = "(\(<*) "
    .Replacement.Text = "\1^l"
    .Forward = True
    .MatchWildcards = True
    .Wrap = wdFindStop
    Do While .Execute
      iLine1 = aRng.Information(wdFirstCharacterLineNumber)
      aRng.Collapse Direction:=wdCollapseEnd
      iLine2 = aRng.Words(1).Information(wdFirstCharacterLineNumber)
      If iLine2 <> iLine1 Then
        aRng.MoveStart Unit:=wdCharacter, Count:=-1
        aRng.Text = Chr(160)
      End If
      aRng.Collapse Direction:=wdCollapseEnd
    Loop
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 06-01-2021, 02:06 PM
macropod's Avatar
macropod macropod is offline Help with a logical code, end of paragraph lines Windows 10 Help with a logical code, end of paragraph lines Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Cendrinne View Post
The reason I was trying to avoid doing that, is because, having a whole document justified, makes spaces between words more spaced out. So to equilibrate the space vs letters, at times you don't want to do it everywhere in a paragraph.
In Word 2013 & later, the non-breaking space (Character 160) has a variable width. Hence doing as I suggested will have no effect on the justification.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 06-01-2021, 07:46 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline Help with a logical code, end of paragraph lines Windows 10 Help with a logical code, end of paragraph lines Office 2019
Competent Performer
Help with a logical code, end of paragraph lines
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Cool It's all good :)

Quote:
Originally Posted by macropod View Post
In Word 2013 & later, the non-breaking space (Character 160) has a variable width. Hence doing as I suggested will have no effect on the justification.
Yes I see your point, and I do thank you. You both have merit in your script / Find and replace recipe.

The only thing I can say, is that what I was exactly looking for, Guessed was able to gather what I needed in is script.

But it's not a contest, everyone of us, each put our thinking cap on, and we find a solution for the greatest good.

This is a fantastic forum to exchange and learn.

Thank you both of you (Guessed and macropod). I bow my hands to you both

Cendrinne
Reply With Quote
  #7  
Old 06-01-2021, 07:52 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline Help with a logical code, end of paragraph lines Windows 10 Help with a logical code, end of paragraph lines Office 2019
Competent Performer
Help with a logical code, end of paragraph lines
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Smile I've done many test, and I say you should be confidant!!!!

Quote:
Originally Posted by Guessed View Post
This appears to work but I would need to see a big data set to be confident in it
Code:
Sub NoBreaksForTheWicked()
  Dim aRng As Range, iLine1 As Integer, iLine2 As Integer
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .Text = "(\(<*) "
    .Replacement.Text = "\1^l"
    .Forward = True
    .MatchWildcards = True
    .Wrap = wdFindStop
    Do While .Execute
      iLine1 = aRng.Information(wdFirstCharacterLineNumber)
      aRng.Collapse Direction:=wdCollapseEnd
      iLine2 = aRng.Words(1).Information(wdFirstCharacterLineNumber)
      If iLine2 <> iLine1 Then
        aRng.MoveStart Unit:=wdCharacter, Count:=-1
        aRng.Text = Chr(160)
      End If
      aRng.Collapse Direction:=wdCollapseEnd
    Loop
  End With
End Sub
Andrew, I've tested on a small paragraph, and then tried on 5 pages worth of text with mixture of open parenthese with a word, in the middle of the paragraph and some at the end. Those in the middle, I've put the font color in Red, those at the end of the line, I've highlighted. So I can confirm, only those that were highlighted where affected.

So YES you can be confidant.

Thank you both of you, Guessed, and Macropod. You are both Great Mind Thinkers and Helpers.

I thank you from the bottom of my heart

Cendrinne
Reply With Quote
  #8  
Old 06-01-2021, 09:23 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline Help with a logical code, end of paragraph lines Windows 10 Help with a logical code, end of paragraph lines Office 2019
Competent Performer
Help with a logical code, end of paragraph lines
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default

I've added other stuff to fix paragraph endings. On a 11 pages of paragraphs, it took 5 min. Is there anyway, to speed it up? Why is it taking so long?


Code:
    Application.ScreenUpdating = False
    
    TST_End_of_Sentence_                                            'suggested by Guessed
    
    Options.DefaultHighlightColorIndex = wdBrightGreen
    
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Highlight = True
    With Selection.Find
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = True
        .MatchWildcards = True
    
        .Text = "(\(*>)^32"
        .Replacement.Text = "\1^s"                                  'suggested by Macropod
        .Execute Replace:=wdReplaceAll
    
        .Text = "(*>)^32(A-Z)"
        .Replacement.Text = "\1^s\2"
        .Execute Replace:=wdReplaceAll
    
    Options.DefaultHighlightColorIndex = wdYellow
    
        .Text = "(«)^32(<*)"                                            '«
        .Replacement.Text = "\1^s\2"
        .Execute Replace:=wdReplaceAll
    
        .Text = "(>)^32(»)"                                            '»
        .Replacement.Text = "\1^s\2"
        .Execute Replace:=wdReplaceAll
        
        .Text = "([%$])"                                               '$%
        .Replacement.Text = "^s\1"
        .Execute Replace:=wdReplaceAll
    
    Options.DefaultHighlightColorIndex = wdTurquoise
    
        .Text = "([0-9]>)^32(millio[ns]@)"                             '0-9 millions
        .Replacement.Text = "\1^s\2"
        .Execute Replace:=wdReplaceAll
    
        .Text = "(rie>)^32([A-Z0-9])"                                  'serie 0-9 / categorie A-Z
        .Replacement.Text = "\1^s\2"
        .Execute Replace:=wdReplaceAll
    
        .Text = "([0-9]{4})-([0-9])"
        .Replacement.Text = "\1^~\2"
        .Execute Replace:=wdReplaceAll

        .Text = "([0-9]>) (<[adfjmnos])"                           'Months
        .Replacement.Text = "\1^s\2"
        .Execute Replace:=wdReplaceAll
    
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

'Reset Find and Replace    
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = True
        .MatchWildcards = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    Application.ScreenUpdating = True
Reply With Quote
  #9  
Old 06-02-2021, 09:46 PM
macropod's Avatar
macropod macropod is offline Help with a logical code, end of paragraph lines Windows 10 Help with a logical code, end of paragraph lines Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I don't know what 'TST_End_of_Sentence_' is about, but it seems to suggest you're running some sort of loop. Your code is also slow because of its heavy use of Selection.

With the Find/Replace operation I suggested, there'd be no looping through the content. All you'd use is:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "([\(«]*>)^32"
    .Replacement.Text = "\1^s"
    .Forward = True
    .Format = False
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
You also have some strange-looking Find/Replace operations. For example:
.Text = "(*>)^32(A-Z)"
.Replacement.Text = "\1^s\2"
would replace an ordinary space before any upper-case letter with a non-breaking space. Why would you do that?

Similarly,
.Text = "([%$])"
.Replacement.Text = "^s\1"
would insert a non-breaking space before % or $, even if there's already a space there, with the potential of ending up with both a space (which may or not be a non-breaking space) followed by non-breaking space.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 06-11-2021, 09:39 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline Help with a logical code, end of paragraph lines Windows 10 Help with a logical code, end of paragraph lines Office 2019
Competent Performer
Help with a logical code, end of paragraph lines
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default On a given paragraph, this script below didn't work......

Hello, Andrew,
I don't want to leave you with the impression, it worked all the time, cause in one case, it didn't. Didn't even flintch. So I want to let the readers here read it's not a 100% working script in those circumstances.

Besides, does it exist and 100% full proof script? Or it's normal, it might not work all the time? I guess it depends if one variable is different than the test I've done previously.

Quote:
Originally Posted by Guessed View Post
This appears to work but I would need to see a big data set to be confident in it
Code:
Sub NoBreaksForTheWicked()
  Dim aRng As Range, iLine1 As Integer, iLine2 As Integer
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .Text = "(\(<*) "
    .Replacement.Text = "\1^l"
    .Forward = True
    .MatchWildcards = True
    .Wrap = wdFindStop
    Do While .Execute
      iLine1 = aRng.Information(wdFirstCharacterLineNumber)
      aRng.Collapse Direction:=wdCollapseEnd
      iLine2 = aRng.Words(1).Information(wdFirstCharacterLineNumber)
      If iLine2 <> iLine1 Then
        aRng.MoveStart Unit:=wdCharacter, Count:=-1
        aRng.Text = Chr(160)
      End If
      aRng.Collapse Direction:=wdCollapseEnd
    Loop
  End With
End Sub
It's ok, the one from Macropod worked.

I rather have a backup plan, than just one. Especially when time is an issue.

Regardless, I love then help I get from this forum. I learn so much from people here

Until next time.

Cendrinne
Reply With Quote
Reply

Tags
help end of sentence, long paragraphs

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a logical code, end of paragraph lines How to change the line spacing between just 2 lines within a paragraph Swarup Word 4 06-26-2019 02:32 PM
How to get rid of horizontal lines after each paragraph? Nisus Word 10 10-08-2018 01:15 PM
double spaced within paragraph 2 blank lines between paragraphs BigOldArt Word 1 08-24-2017 09:08 AM
Help with a logical code, end of paragraph lines Lines refuse to merge into one paragraph (Word 2010) Chris24 Word 2 01-22-2017 03:00 PM
Help with a logical code, end of paragraph lines Keep Paragraph Lines Together Issue SQLUSA Word 2 06-23-2012 05:00 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:29 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