0

All.

I am used to programming VBA in Excel, but am new to the structures in Word. I am working through a library of text files to update them. Many of them are either OCR documents, or were manually entered.

Each has a recurring pattern, the most common of which is unnecessary carriage returns.

For example, I am looking at several text files where there is a double return after each line. A search and replace of all double carriage returns removes all paragraph distinctions. However, each line is approximately 30 characters long, and if I manually perform the following logic, it gives me a functional document.

If there is a double carriage return after 30+ characters, I replace them with a space. If there were less than 30 characters prior to the double return, I replace them with a single return.

Can anyone help me with some rudimentary code that would help me get started on that? I could then modify it for each "pattern" of text documents I have.

e.g.

In this case, there are more than

thirty characters per line.  And I

will keep going to illustrate this

example.

This would be a new paragraph, and

would be separated by another of

the single returns.

I want code that would return:

In this case, there are more than thirty character returns. And I will keep going to illustrate this example.

This would be a new paragraph, and would be separated by another of the single returns.

Let me know if anyone can throw something out that I can play with!

4
  • 1
    I tried to adjust your example since the site's formatting removes extra spaces. Not sure if I got it right... I just went through something similar with a set of huge files... and finally figured out RegEx in the process. That's likely the easiest/fastest way... Have you used RegEx? Also, is this a one-time thing or something that needs automation for the future? If one-time for a set of files (or rare enough that it can be done manually), I can answer with steps using Notepad++. Otherwise, VBA+RegEx could be better. And just to confirm, you said these are text* files? (not docx etc)
    – ashleedawg
    Sep 4, 2018 at 5:55
  • +1 for the RegEx and NP++ idea... you can use RegEx in VBA but it takes a bit of setting up... you might want to mess around here: regex101.com/r/zG9GPw/1 Sep 4, 2018 at 6:09
  • Great, thanks, I will!
    – BLP_1975
    Sep 5, 2018 at 3:32
  • @ashleedawg - I've just started playing a bit I have not used RegEx, but it does look useful. It's essentially a one-time shot, so if you've got steps using Notepad++, that would be great. Yes - these are text files, not docx or anything like that.
    – BLP_1975
    Sep 6, 2018 at 7:14

2 Answers 2

1

You can do this without code (which RegEx requires), simply using Word's own wildcard Find/Replace tools, where:

Find = ([!^13]{30,})[^13]{1,}
Replace = \1^32

and, to clean up the residual multi-paragraph breaks:

Find = [^13]{2,}
Replace = ^p

You could, of course, record the above as a macro...

5
  • Thanks! I hadn't played around with the conditional statements in Find/Replace. I will play with the coding answers as well, just to wrap my head around the options.
    – BLP_1975
    Sep 5, 2018 at 3:34
  • Thanks! Still not getting exactly the results I want, but I've just started looking at the Find/Replace wildcards.
    – BLP_1975
    Sep 6, 2018 at 7:14
  • You might find msofficeforums.com/word/… helpful for dealing with clean-up issues more generally. For some useful info on wildcards see wordmvp.com/FAQs/General/UsingWildcards.htm
    – macropod
    Sep 6, 2018 at 7:54
  • Just wanted to say thanks again, still playing with it. I think the issue is that some of the carriage returns are not the same character... It replaces some of the double returns as I expect, but seems to miss some of them. Anyway, will keep playing, just wanted to say thanks for this option in addition to the coding ones!
    – BLP_1975
    Sep 8, 2018 at 20:45
  • The second F/R will replace all repeated carriage returns with a single one, though you may end up with a pair of them before a table if your document has any. Perhaps your document also has manual line breaks. With Word's formatting display on, you'll see paragraph breaks as ¶ symbols and manual line breaks as ↵ symbols. And, if you're working with tables, their end-of-cell & end-of-row markers look like the ¤ symbol; you can't delete those via Find/Replace.
    – macropod
    Sep 8, 2018 at 22:20
0

Here is a RegEx that might work for you:

(\n\n)(?<!\.(\n\n))

The substitution is just a plain space, you can try it out (and modify / tweak it) here: https://regex101.com/r/zG9GPw/4

This 'pattern' tells the RegEx engine to look for the newline character \n which occurs x2 like this \n\n (worth noting this is from your question and might be different in your files, e.g. could be \r\n) and it assumes that a valid line break will be proceeded by a full stop: \..

In RegEx the full stop symbol is a single character wild card so it needs to be escaped with the '\' (n and r are normal characters, escaping them tells the RegEx engine they represent newline and return characters).

So... the expression is looking for a group of x2 newline characters but then uses a negative look-behind to exclude any matches where the previous character was a full stop.

Anyway, it's all explained on the site: enter image description here


Here is how you could do a RegEx find and replace using NotePad++ (I'm not sure if it comes with RegEx or if a plugin is needed, either way it is easy). But you can set a location, filters (to target specific file types), and other options (such as search in sub-directories).

enter image description here

Other than that, as @MacroPod pointed out you could also do this with MS Word, document by document, not using any code :)

2
  • Thanks - just starting playing with RegEx and coding, and having fun with it - lol - but need to get these done before I have fun. Will try a few of the other options, then will teach myself a bit more about that! :)
    – BLP_1975
    Sep 6, 2018 at 7:15
  • @BLP_1975 hey, for a single shot NotePad++ will do this easily (assuming you are using plain text files). I'll update my answer with an image for NP++ Sep 6, 2018 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.