PDA

View Full Version : Solved: Macro for carriage return



Codeblue
07-11-2012, 11:05 AM
I tried recording a macro for a doc that has text somewhat jumbled together. Specifically I need a blank line between paragraphs. Each of the paragraphs start with a word that has blue text. Is it possible to have code to look for blue text and bump the paragraph down a line. My macro didn't distinguish the blue text it just recorded my keystrokes of up and down the page I added blank lines.

Frosty
07-11-2012, 11:21 AM
Why don't you try recording a macro which finds text which is blue?

From there, you can, instead of adding a carriage return, apply a style which has space after of 12pt or something.

Inserting blank paragraphs is bad formatting practice, and you will likely be back to get a macro which removes "empty" paragraphs from your document.

In addition, if you learn about styles, you will find that you can then change the spacing between these paragraphs simply by adjusting the style in one spot, rather than trying to do something with all of these meaningless paragraphs.

Codeblue
07-11-2012, 12:10 PM
I probably didn't make myself very clear I was trying to say I need to add a blank line between paragraphs not a blank paragraph. Right now it is just one large paragraph.

How would I record a macro which finds text which is blue? I tried inserting the cursor before each blue text and bumping it down a line but it isn't recognizing it as blue text i don't believe.

Frosty
07-11-2012, 12:16 PM
You have a single giant paragraph with some blue words in it, and you want to turn that single giant paragraph into multiple paragraphs, each of which start with a blue word?

So...

Yada yada BLUE yada yada yada BLUE yada yada BLUE yada yada

would become...

Yada yada
BLUE yada yada yada
BLUE yada yada
BLUE yada yada

??

You can record a macro to find formatted text simply by recording a macro, and then clicking on Advanced Find (or more-- depending on which version of Word you are using), and then selecting the kind of formatting you want to find. In your case, you would look for Format > Font and then identifying the Font Color.

All you need to do is find the color... don't worry about the rest for the moment.

I could give you a code block, but it's easier long-term if you understand how to do this yourself.

Frosty
07-11-2012, 12:33 PM
To be clear... do you know how to use Word to find text? CTRL + F allows you to type some text "Hello" and then find the first instance of "Hello" in the current document.

This is a very basic use of what can be a very advanced feature. It can become even more advanced when coupled with recording a macro and a little bit of understanding of VBA.

Frosty
07-11-2012, 12:50 PM
Because at the end of the day-- if you're accurately describing what you want, you don't need a macro at all.

All you need to know is how to use the built-in Find feature.

1. CTRL + H
2. Click More
3. Click Format > Font
4. Choose your font color
5. Move your cusor to the "Replace With" box
6. Click Special
7. Choose Paragraph Mark (you'll see ^p appear in your replace box)
8. Click Special
9. Choose Find What Text (you'll see ^& appear in your replace box)
10. Click Replace All

No macro needed.

Now, if you want to record this process, you would have a macro do this multiple times across different documents.

And you may want to add in error handling to deal with not inserting paragraph marks when you already have one there before a bit of blue text -- but there are a lot of ways to deal with this stuff.

At the end of the day, the more you know about Word's built-in feature set, the easier it is to program to fill the gaps in Word, rather than the gaps in your knowledge.

Hope this helps!

macropod
07-11-2012, 03:27 PM
Frosty, the OP wants a blank line, not a blank paragraph, so:
1. CTRL + H
2. Click More
3. Click Format > Font
4. Choose your font color
5. Move your cusor to the "Replace With" box
6. insert ^l^&
7. Click Replace All

fumei
07-11-2012, 06:06 PM
"Frosty, the OP wants a blank line, not a blank paragraph, so:"

Paul, I am not so sure the OP is clear on blank line vs empty paragraph.

Codeblue
07-11-2012, 06:28 PM
Yes, I have used CNTL-F and I agree it would be detrimental to just give me code I prefer to work it out and ask questions once I start banging my head to the wall.

You guys have given me a great start. thanks!

Codeblue
07-13-2012, 06:01 AM
I tried...

1. CTRL + H
2. Click More
3. Click Format > Font
4. Choose your font color
5. Move your cusor to the "Replace With" box
6. insert ^l^&
7. Click Replace All

but am getting an error with ^|^& says it is not a valid character. I think it doesn't like the |. I assume it is the char above the \ key. Getting the error in the "Replace with" box.

Frosty
07-13-2012, 09:16 AM
Try my steps. You're probably getting the "l" character confused. It is a lower case L, not an upper case i

Codeblue
07-13-2012, 11:48 AM
I tried your steps and I got this code...

Sub CR()
'
Selection.Find.ClearFormatting
Selection.Find.Font.Color = 12611584
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "^p^&"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

but it's saying it found 0 items when I run it. The blue text it is to search for may be a different shade of blue it's Red=0 green=0 Blue=255 but those numbers don't work. I think my blue is different from the theme colors that show during the "Choose your font color" step.

Frosty
07-13-2012, 01:06 PM
you can select your text... and then type the following in the immediate pane in the VBA Editor (CTRL+G will display this pain).
?Selection.Font.Color

You'll see a value there, and then you can plug that into your Selection.Find.Font.Color above.

But I'm guessing you're right... you need to find the blue text, which means you need to know which blue :)

Codeblue
07-13-2012, 02:20 PM
This worked!!

Sub CarRet()
'
' CarRet Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Font.Color = wdColorBlue
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Color = wdColorBlue
With Selection.Find
.Text = ""
.Replacement.Text = "^l^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

thanks guys this was most edifying!

Frosty
07-13-2012, 02:51 PM
Glad to help. Just know that the difference between ^l (line break) and ^p(paragraph mark) is that you still have a single massive paragraph. The line break character is the equivalent of pressing SHIFT+ENTER while the paragraph mark is the equivalent of pressing ENTER.

Good luck!

Codeblue
07-13-2012, 03:20 PM
I must be dense because I'm just not seeing the difference between shift/enter and enter as they both seem to bring them paragraph down one line.

macropod
07-13-2012, 03:24 PM
I must be dense because I'm just not seeing the difference between shift/enter and enter as they both seem to bring them paragraph down one line.So, are you hoping someone will argue with the first part of your reply??:whip

The difference is not so much in the appearance, though the use of paragraph breaks can be used to add much more space than a line break, but in the logical structure of the document. A manual line break simply puts a line break into a paragraph. A paragraph break splits it into two paragraphs.

Codeblue
07-13-2012, 03:33 PM
It's a safe statement when I'm not looking for an argument :)

Maybe I've never made it clear that each paragraph I want dilineated already has the blue text at the start of the sentence. That probably isn't clear. The blue text is never mid sentence.

Is there a way to alphabetize? I didn't see it under CNTL-H

Frosty
07-13-2012, 03:42 PM
If you show your paragraph marks, you would see the difference in symbol.

But where you'll really see the difference is if you use CTRL+DOWN ARROW to navigate through your document.

Since that is the default to moving down "by paragraph", in one scenario (line breaks) you will travel much farther than the other scenario (paragraphs).

You may be able to alphabetize if you use the Convert Text To Table function, and then try sorting the column. You'll probably need to play with that feature a bit to get it just right.

macropod
07-13-2012, 03:43 PM
In that case, you should probably be using:
Sub CarRet()
With Selection.Find
.ClearFormatting
.Text = ""
.Font.Color = wdColorBlue
.Replacement.ClearFormatting
.Replacement.Text = "^p^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
As for "a way to alphabetize", I take it you mean sorting. Sorting isn't a part of the Find/Replace process. In Word 2003 & earlier, the sorting tools are on the Tables toolbar. In Word 2007 & later, they're on the Table tab (not much good if you're not using a table) or you can add them to the QAT.

Frosty
07-13-2012, 03:47 PM
Oh, and you keep mixing up the concept of a paragraph and a line break, which is probably why you seem dense ;)

In the way that Word thinks about these things (in as much as a program can think about something), there is considerable difference between a paragraph, and a blank line in the middle of a paragraph.

While a type-writer will have no such problems, because it's just a page with some printed ink on it... a computer program views those two entities entirely differently.

I suspect you should probably separate using paragraph marks rather than line breaks, but I'm basing that assumption on the fact that you've used the word paragraph at all.

I think, strictly speaking, that a "carriage return" translates to a paragraph, while a "line feed" translates to a line break. Of course, I only say that because
Selection.TypeText vbCr
types a paragraph mark, but...
Selection.TypeText vbLf
types a line break

Frosty
07-13-2012, 03:49 PM
And it seems that Paul and I have much the same information and conclusions-- just a different format.

Neener neener, Paul-- ^p was the answer ;)

Hope this helped, CodeBlue! Cheers!

fumei
07-13-2012, 03:50 PM
I must be dense because I'm just not seeing the difference between shift/enter and enter as they both seem to bring them paragraph down one line.Like I said, i am not sure the OP is clear about blank line versus empty paragraph.

Codeblue
07-16-2012, 11:29 AM
you guys have been a major help!
One prob I'm running into is after the page is divided into paragraphs some of the sentences are non-contigious as in part of a sentence is on one line then continued on the next. Is there a way to separate all the words by one char? I didn't see anything under the QLT.

Frosty
07-16-2012, 11:33 AM
Pressing CTRL+SPACE will give you a "non-breaking" space. This is one of the many strategies in getting word to wrap where you want it to, without having to use a manual line break character.

You can search for two spaces and replace all with a single space.

Apart from those ideas, I'm confused what you're talking about. Can you be a little more descriptive?

Codeblue
07-16-2012, 12:16 PM
something like this...see attached. Not sure if that went thru or not.

Frosty
07-16-2012, 03:27 PM
That looks like scanned text. You could replace all paragraph marks with a space, and then all periods with a space and a paragraph mark... but there's no real easy solution to your problem. You need to try using a lot of different find/replaces... none of this is macro stuff, to my mind.

macropod
07-16-2012, 08:13 PM
It would be interesting to see what the document looked like before the previous macros had been run. FWIW, you can clean up the attachment with a wildcard Find/Replace pair, where
Find = ^l
Replace = ^p
Find = ([!^13^11])([^13^11])([!^13^11])
Replace = \1 \3

Codeblue
07-17-2012, 09:31 AM
I'm probably barking up the wrong tree here but I tried this code to find more than 1 space and replace it with one space

Sub FindSpace()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

but it didn't do anything.
Actually I think I see part of the problem. It is looking for two spaces exactly and many times thru this doc it is much more than 2 spaces so it's not seeing anything to change.

Frosty
07-17-2012, 09:40 AM
I don't think it did anything because it looks like you're looking for exactly the same thing (two spaces) as you are replacing (two spaces).
To me, it looks like you're trying to work up some macros to do document clean up on a scanned document. The problem with this approach is that scanned documents do not always have the same particulars... a macro you recorded for one document may be completely wrong for another document.

My suggestion is that you skip the macro recording for awhile until you learn how to use find/replace really well. You may find that you really don't need macros at all... you just need to do a couple of quick find/replaces to get your document cleaned up as much as you need.

When you start to see patterns in your document cleanup, you may find that you can get even faster by recording a couple of your more standard find/replace actions as macros, and then run those quickly with a single click.

But this is all predicated on you understanding how to do basic find/replace a good bit better than you do right now.

Does this make sense?

Codeblue
07-17-2012, 09:55 AM
Yes it makes sense I need an all-around better understanding of formatting et all. I know the code looks like it uses a two space in two places find and replace but actually in the macro it looks for two spaces to be replaced with one. Another formatting issue this time with vba.

thanks for the input!

macropod
07-17-2012, 03:58 PM
Try:
Sub SingleSpace()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[ ]{2,}"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
The above code uses a wildcard Find/Replace to replace all instances of two or more consecutive spaces with a single space in one pass.