473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

VBA macro for removing unwanted paragraph break

3
I am in Word and have a lot of text pulled from a PDF that has inserted breaks in to lines. For example it may go

"The quick brown fox jumps
over the lazy dog."

...and what I'd want, obviously, is that sentence all on one line

e.g. "The quick brown fox jumps over the lazy dog."

Essentially, every time there is a paragraph break (I believe they are Char(10) or vbLf) followed by a lowercase letter, I want to change that paragraph character to a simple space.

I am completely new to visual basic. I have some Frankenstein-monster-esque chunks of code that I've pulled from various places on the internet, but I'm afraid that sharing it would probably limit people's recommendations on how to do what at least appears to be a fairly simple thing by making them try to use or fix the structure of what I've got, when I'm not the least bit attached to it anyway.

Can someone please point me in the right direction here?
Much obliged
Jun 13 '16 #1
4 1147
Luk3r
300 256MB
Go ahead and share the code you've currently got and you'll get more help instead of us working in the dark. :)
Jun 14 '16 #2
fngb3
3
Sub ChangeCase1()
Dim vFindLineBreak As Variant
Dim vReplace As Variant
Dim orng As Range

'This is what I found that seems to detect a lowercase character from a to z.
Function FirstLower(strIn As String) As String
Dim objRegex As Object
Dim objRegM As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "[a-z]"
.ignorecase = False
If .test(strIn) Then
Set objRegM = .Execute(strIn)(0)
FirstLower = objRegM.firstindex + 1
Else
FirstLower = "no match"
End If
End With
End Function

'I want this to find instances where a line break is followed by a lowercase character, and replace those two things with a space.
vFindText = vbLf + FirstLower

vReplaceText = " "

Next
End Sub
Jun 14 '16 #3
fngb3
3
Or, and this seems more promising.

Sub findlower()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^13 + ([a-z])"
.Replacement.Text = " \1"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
End Sub
Jun 14 '16 #4
Luk3r
300 256MB
I think you can accomplish what you're after by changing this line:
Expand|Select|Wrap|Line Numbers
  1. Pattern = "[a-z]"
to this:
Expand|Select|Wrap|Line Numbers
  1. Pattern = "[a-z][\r\n]"
In case you're wondering how I tested this, I used the following code in Visual Studio using VB.NET, but the regex should still be the same.
Expand|Select|Wrap|Line Numbers
  1. 'Create a test string with a line feed (vbLf) in the middle
  2. Dim testString As String = "The quick brown fox jumps" & vbLf & " over the lazy dog."
  3.  
  4. 'Display test string in a window
  5. MsgBox(testString)
  6.  
  7. 'Using regular expressions, replace the line feed if it follows lowercase letter
  8. testString = System.Text.RegularExpressions.Regex.Replace(testString, "[a-z][\r\n]", "")
  9.  
  10. 'Display new test string format in a window
  11. MsgBox(testString)
Jun 14 '16 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Phil Amey | last post by:
In a web based form I am able to make sure that there is text in an input field but I want to restrict the user from using such characters as ~ # & ' How can I modify this JavaScript below to...
2
by: Matt Silberstein | last post by:
Is there a way to have a style that gives me a "paragraph" break automatically. I have a calendar that is vertical on one page and horizontal on the other, but has the same content. If I could...
30
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> http://www.python.org/peps/pep-0274.html >> The reason, of course,...
2
by: Litron | last post by:
Hi All, I'm working with 3 Frames on a web-page: (top) Top.html, (left) Navigation.html, and (right) "Content" which contains varying html pages based on the (left) Navigation Selection - the...
5
by: Maxi | last post by:
I just have one index.html page with NO links NO pics. Just a simple html page. Whenever I open that page I want to eliminate few things like: I don't want scroll bar -> I have a solution...
0
by: Abhinay | last post by:
hi, this is abhinay, i have data ie xml file Ex: <root> <child1> <cc1>aaa</cc1> </child1>
15
by: Yogi | last post by:
Hi there, I have a quick question. In my html document, I want to make a new paragraph whenever I have a blank line in the html source. Using <p> and </pevery time is kind of cumbersome (I want...
3
by: Joe Stateson | last post by:
I have a template field that contains filenames that are (or can be) on a unix system. The hypen (and others) is a valid filename character in unix. When I display the name of the file I get an...
9
by: daknightuk | last post by:
I have a HTML page which posts information to a PHP page which contains a query for mysql to add records to a database I want to strip all special characters and only allow 0-9 and a-z but also...
1
by: KMM833 | last post by:
Please excuse what I hope is the simplicity of this question. I use Access, but don't do any programming in it. I am building a report that is designed to give organization by organization answers...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.