PDA

View Full Version : [SOLVED:] How do I insert a blank line before bold text in a selection?



Helen269
06-23-2022, 09:12 PM
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! :-)

macropod
06-24-2022, 01:43 AM
What is the logic that determines when to insert the extra space?

Helen269
06-24-2022, 05:43 AM
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. :-)

Aussiebear
06-24-2022, 06:36 AM
@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.

Helen269
06-24-2022, 07:28 AM
@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...?

Chas Kenyon
06-24-2022, 09:45 AM
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.

Helen269
06-24-2022, 10:04 AM
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?

macropod
06-24-2022, 05:21 PM
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

Helen269
06-24-2022, 06:13 PM
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. :-)

Aussiebear
06-26-2022, 02:55 AM
@Helen 269. Could have a look at Post 1 and Post 7.... anything there that is different?

Helen269
06-26-2022, 04:13 AM
@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.

Aussiebear
06-26-2022, 12:14 PM
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.

Helen269
06-26-2022, 04:19 PM
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.

Aussiebear
06-27-2022, 01:35 AM
Thank you for posting here anyway.

snb
07-01-2022, 05:06 AM
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

Helen269
07-01-2022, 07:08 AM
Thank you for that one, too.