Word document: format every first line

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi, guys & galls,

Trying to master Word now :biggrin:

Say you have a text with empty lines. How can you format (using VBA if necessary) every line after the empty ones?

START WITH
Some Text
Other Text can be any length.
More .......................

Starting new line after an empty. More text.
Text Text Text Text Text Text Text Text Text Text Text Text Text Text

Text Text Text Text Text
Text More More More More More More
More More More

TO GET
Some Text
Other Text can be any length.
More .......................

Starting new line after an empty. More text.
Text Text Text Text Text Text Text Text Text Text Text Text Text Text

Text Text Text Text Text
Text More More More More More More
More More More

I searched trough characters, paragraphs, line (textstream??) ...

Can somebody point me to a useful link, or provide a codesample which would put me on the way?

Thanks for reading,
Erik
 
Hi Erik,

My latest code didn't "bold every first paragraph after an empty one". Rather, it bolded every paragraph that consists of a single sentence. In a well-formatted Word document, you wouldn't have these empty paragraphs - you'd format the heading paragraph to put in the appropriate leader space, which might not be the same as you'd get with an empty paragraph. Here's how you might take a poorly formatted document (with empty paragraphs before the intended headings) and clean it up and apply a heading style at the same time:
Code:
Sub HiLite5()
Dim i As Integer
Application.ScreenUpdating = False
With ActiveDocument
    .Styles("Heading 3").ParagraphFormat.SpaceBefore = 18
    For i = .Paragraphs.Count - 1 To 1 Step -1
        If Trim(.Paragraphs(i).Range.Text) = vbCr Then
            .Paragraphs(i + 1).Style = wdStyleHeading3
            .Paragraphs(i).Range.Delete
        End If
    Next
End With
Application.ScreenUpdating = True
End Sub
Now, if you wanted to change the way all those headings looked, you could simply use Format|Style to change the font, spacing, alignment, etc, and have it take effect throughout the document without having to touch a single paragraph.

Cheers
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Wow, this is interesting!! I tried out, edited the heading: worked as expected. Thank you for this.

Although the project is already working nicely, I will change that part. Remember, I'm getting data from Excel and creating a Worddocument and thought I needed to insert NewLine to separate the items. Instead I can now define a style. (rows 3, 6, 8 & 9)

   A            B      C                            D              
 3 Pete Jones   flute  Big Ben                      P. Hartmann    
 4                     Piece1                       J. Irvine      
 5                     Piece2                       D. Balakirev   
 6 Julia Curtis gitaar Little Game                  F. Butterfield 
 7                     Fast Dance                   P. Dubois      
 8 Paul Masur   piano  7 nice little pieces opus 15 E. Faesy       
 9 Peter Mineur flute  Nobody knows                 A. Guilmant    
10                     Nobody cares                 J. Jensen      
11                     Nobody comes                 T. Sindici     
12                     Nobody goes                  G. Troyer      

data

[Table-It] version 07 by Erik Van Geit

THANK YOU :)
have a nice weekend!
Erik
 
Upvote 0

Forum statistics

Threads
1,215,471
Messages
6,125,002
Members
449,202
Latest member
Pertotal

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top