Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 32

Thread: Solved: Macro for carriage return

  1. #1

    Solved: Macro for carriage return

    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.

  2. #2
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    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.

  3. #3
    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.

  4. #4
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    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.

  5. #5
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    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.

  6. #6
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    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!

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "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.

  9. #9
    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!

  10. #10
    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.

  11. #11
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Try my steps. You're probably getting the "l" character confused. It is a lower case L, not an upper case i

  12. #12
    I tried your steps and I got this code...
    [vba]
    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
    [/vba]
    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.

  13. #13
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    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

  14. #14
    This worked!!
    [vba]
    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
    [/vba]
    thanks guys this was most edifying!

  15. #15
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    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!

  16. #16
    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.

  17. #17
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Codeblue
    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??

    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  18. #18
    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

  19. #19
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    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.

  20. #20
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    In that case, you should probably be using:
    [VBA]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[/VBA]
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •