Consulting

Results 1 to 16 of 16

Thread: How do I insert a blank line before bold text in a selection?

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location

    How do I insert a blank line before bold text in a selection?

    Hi, I've been trying to solve this one myself by lots of Googling and experimenting and I can't get anything to work.

    I have a document where some lines have bold text like this:

    Text
    Text
    Text
    Text
    Text
    Text
    Text
    Text
    Text

    I would like to manually select some of these lines and run a macro that would insert a line before each bolded line in the selction so it looks like this

    Text
    Text
    Text
    Text

    Text
    Text

    Text

    Text
    Text
    Text

    I know it's simple, I just can't figure it out. Can anyone help, please?

    Thanks! :-)

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    What is the logic that determines when to insert the extra space?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location
    Quote Originally Posted by macropod View Post
    What is the logic that determines when to insert the extra space?
    I used the old trick of recording a macro then trying to edit the code. What I did was set the Find to ^p with Bold on. Pressing Find Next got me to the end of the first line that was in Bold text.

    I think it then needs to maybe adapt the previous line above that and add an additional ^p to the one already there? Not sure how you would do that. This is isn't an urgent, just a would-be-nice; so if it can't be done it's no biggie. :-)

  4. #4
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    @Helen269. You were asked what is the logic that determines when to insert the extra space. Your response is weird at best. We don't need to know how you arrived at your "solution' but rather the method of determining when you need a space included.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location
    Quote Originally Posted by Aussiebear View Post
    @Helen269. You were asked what is the logic that determines when to insert the extra space. Your response is weird at best. We don't need to know how you arrived at your "solution' but rather the method of determining when you need a space included.
    Sorry, I misunderstood.

    And maybe it's because I've slept on it or the coffee I just had, but I found some code that I edited slightly to do what I need. The only trouble is it continues past the selection to the end of the doc:

    Sub LineBeforeBold()
        With Selection.Find
            'Set search criteria for bold font
            .Font.Bold = True
            'Find next occurrence
            .Execute
    'Each time bold text is found add a line break before it then find the next one
            Do While .Found
                Selection.Text = vbCrLf + Selection.Text
                .Execute
            Loop
    End With
    End sub
    I now just need to figure out how to make it work only on a selection and then stop and not do the rest of the doc. I've tried inserting
    .Wrap = wdFindStop

    at various points, but that doesn't work. Something to do with Range, maybe...?
    Last edited by Aussiebear; 06-24-2022 at 02:24 PM. Reason: Added code tags to supplied code

  6. #6
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    118
    Location
    So, you want to select a block of text. Any time a line in the selection begins with bold text, you want to add a blank paragraph before that line?
    You only want this for the selected text.

    Note, better might be to change the space before setting for that paragraph. Even better, to apply a paragraph style that has that setting. This uses features built into Word. Blank paragraphs are a bad idea in a Word document.

  7. #7
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location
    Mmm, that's an approach I hadn't thought of. To clarify, I want to make this:

    Some text
    Some text
    Some text
    Some text
    Some text
    Some text
    Some text
    Some text
    Some text

    Look like this:
    Some text
    Some text

    Some text
    Some text
    Some text

    Some text
    Some text
    Some text
    Some text

    But only in a selection, not the whole doc.

    As I've posted above, I've found how to do it with VBA, but it either does the whole document or it continues past the selection. I just need it to do that in a manual selection.

    How would I apply a paragraph style that would this? How would I highlight a load of bold/not-bold text, and tell it to only increase the spacing before the bold text?

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    For example:
    Sub Insert_Lines()
    Application.ScreenUpdating = False
    With Selection
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Wrap = wdFindStop
        .Text = ""
        .Replacement.Text = "^p^&"
        .Font.Bold = True
        .Execute Replace:=wdReplaceAll
      End With
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location
    Oh wow, yes, thank you, that works! :-)
    And I think I can see why this works, too, and why it stops at the end of a selection. It seems I had some of the pieces but just in the wrong order and you've filled in the rest of the pieces.

    Thanks, macropod. You've helped me out and added a little bit more to my understanding of how VBA works. :-)

  10. #10
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    @Helen 269. Could have a look at Post 1 and Post 7.... anything there that is different?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  11. #11
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location
    Quote Originally Posted by Aussiebear View Post
    @Helen 269. Could have a look at Post 1 and Post 7.... anything there that is different?
    If you're referring to what I'm asking how to do, in post 7 I'm querying post 6 where it's suggested I apply a paragraph style to achieve what I want and I'm questioning whether that's even possible. Sorry, I should have included a quote and made it clear I'm doubting it as opposed to asking how to actually do it.

  12. #12
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    The significant difference between the posts is that in Post #1 you requested that not every Bold text needed to have a space above it yet in Post #7 you do. In Posts #2 & 4 you were asked to define the logic in determining how it was to apply a rule to produce the result in the provided sample in Post #1. I'm extremely confident that if the sample in Post #7 was what you had initially provided Paul could have easily solved the issue.

    You are more than welcome to post in the forum again, but please have a good think about how you post ( particularly the data within a sample) as this can be misleading.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  13. #13
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location
    Yes, I'll be more careful about wording my future requests. Sorry about that. Thanks again to Paul for his help, and sorry about any confusion.

  14. #14
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    Thank you for posting here anyway.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  15. #15
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    Using VBA's Object based language:

    Sub M_snb()
       For j = 1 To Selection.Paragraphs.Count
          If Selection.Paragraphs(j).Range.Characters.Last.Bold Then Selection.Paragraphs(j).Range.InsertAfter vbCr
       Next
    End Sub

  16. #16
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location
    Thank you for that one, too.

Posting Permissions

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