Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-27-2015, 11:44 PM
cheech1981 cheech1981 is offline macro to delete line break Windows 7 64bit macro to delete line break Office 2010 64bit
Advanced Beginner
macro to delete line break
 
Join Date: Nov 2011
Location: New Jersey, USA
Posts: 77
cheech1981 is on a distinguished road
Default macro to delete line break

Hi all,

I often have clients who enter manual returns in parts of their document (for instance, in a list of references at the end of a scholarly document).

I use a handy find and replace with wildcards
Find: (^13)([a-z])


Replace: /2

This removes the manual line break in the middle of reference entries (but not line breaks between references, because those almost always start with a capital letter).

Is there a simple macro that I could use to do the same? (if it is detailed, please don't spend time and I can delete the thread--I'm just wondering if it happens to be simple for those who know).
Reply With Quote
  #2  
Old 05-28-2015, 12:44 AM
gmayor's Avatar
gmayor gmayor is offline macro to delete line break Windows 7 64bit macro to delete line break Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

First of all the first set of brackets is superfluous
Find: ^13([a-z])
Replace: \1
would achieve the same result - http://www.gmayor.com/replace_using_wildcards.htm

As for a macro, select the text that you wish to process and run

Code:
Sub RemoveParaOrLineBreaks()
Dim oRng As Range
Dim oFind As Range
    Set oRng = Selection.Range
    Set oFind = Selection.Range
    With oFind.Find
        Do While .Execute(FindText:="[^13^l]{1,}", MatchWildcards:=True)
            If oFind.InRange(oRng) Then
                oFind.Text = ""
            End If
        Loop
    End With
    Set oFind = oRng
    'Optionally remove additional spaces
    With oFind.Find
        Do While .Execute(FindText:="[ ]{2,}", MatchWildcards:=True)
            If oFind.InRange(oRng) Then
                oFind.Text = Chr(32)
                oFind.Collapse 0
            End If
        Loop
    End With
    'end of option
lbl_Exit:
    Set oRng = Nothing
    Set oFind = 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

Last edited by gmayor; 05-28-2015 at 05:02 AM.
Reply With Quote
  #3  
Old 05-28-2015, 04:10 AM
cheech1981 cheech1981 is offline macro to delete line break Windows 7 64bit macro to delete line break Office 2010 64bit
Advanced Beginner
macro to delete line break
 
Join Date: Nov 2011
Location: New Jersey, USA
Posts: 77
cheech1981 is on a distinguished road
Default

Wow, thank you for your detailed response and the macro Graham. When I select the references list at the end of the document and run the macro, it seems to remove all line breaks. What I'm looking for is to only remove line breaks followed by a lowercase letter.

For example, imagine that the user has these two reference entries in his/her references list:

De Jean, J., Upitis, R., Koch, C., & Young, J. (1999). The story of Phoenix Quest: How girls respond to a
prototype language and mathematics computer game. Gender and Education, 11(2).

Smith, X. Y. (2015). Some title. Some publication info.


Users often enter manual line breaks, as seen in the first reference entry above (manual line break after "respond to a"). These breaks cause problems with alignment, prevent alphabetization of the list, and cause other problems.

So the original find and replace will take care of this issue, but having it as a macro would help because I could add it into a larger macro that I'm compiling to do some formatting to references lists to save me time.

Any way to adjust the macro to just catch the breaks followed by a lowercase letter?

Thanks you again for your thoughtful reply.
Reply With Quote
  #4  
Old 05-28-2015, 04:56 AM
gmaxey gmaxey is offline macro to delete line break Windows 7 32bit macro to delete line break Office 2010 (Version 14.0)
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

If you just remove the linebreak after respond to a, you will be left with respond to aprototype. You need to add a space, yes?

Code:
Sub RemoveParaOrLineBreaks()
Dim oRng As Range, oFind As Range
  Set oRng = Selection.Range
  Set oFind = Selection.Range
  With oFind.Find
    .Text = "[^13^l]{1,}([a-z])"
    .MatchWildcards = True
    Do While .Execute
      If oFind.InRange(oRng) Then
        oFind.Text = Chr(32) & oFind.Characters.Last
      End If
    Loop
  End With
lbl_Exit:
  Set oRng = Nothing
  Set oFind = Nothing
  Exit Sub
End Sub
See: http://gregmaxey.com/word_tip_pages/...ng_macros.html for instructions to employ the VBA code provided above.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 05-28-2015, 05:02 AM
gmayor's Avatar
gmayor gmayor is offline macro to delete line break Windows 7 64bit macro to delete line break Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

The macro acts on the selected text, so only select the problematic lines. In the case of your example, select only

De Jean, J., Upitis, R., Koch, C., & Young, J. (1999). The story of Phoenix Quest: How girls respond to a
prototype language and mathematics computer game. Gender and Education, 11(2).

and then run the macro or to do what you asked

Code:
Sub RemoveParaOrLineBreaks()
Dim oRng As Range
Dim oFind As Range
    Set oRng = Selection.Range
    Set oFind = Selection.Range
    With oFind.Find
        .Replacement.Text = "\1"
        Do While .Execute(FindText:="[^13^l]{1,}([a-z])", _
                          MatchWildcards:=True, _
                          Replace:=wdReplaceAll)
        Loop
    End With
lbl_Exit:
    Set oRng = Nothing
    Set oFind = 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
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to delete line break Can't delete page break John Holstein Word 5 04-01-2015 09:00 AM
Line break between en dash and number 90-degree Word 1 01-14-2015 01:09 PM
macro to delete line break How can I remove line break from 4 dash's nmss18 Word 2 09-11-2014 06:07 AM
macro to delete line break delete page break seeker62 Word 3 07-22-2013 06:49 PM
macro to delete line break Delete Page Break using VBA wordpunk Word VBA 1 07-03-2012 03:05 PM

Other Forums: Access Forums

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