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
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
result thusfar
Code:
Sub test()
'Erik Van Geit
'071117

'format each sentence after empty
'will only work if there is only one column
'(PageSetup.TextColumns.SetCount = 1)

'ScreenUpdating = false gives ugly "screenplay" during macro

Dim ccc As Long
Dim flag As Boolean

ccc = ActiveDocument.Characters.Count

    With Selection
    .HomeKey Unit:=wdStory
    
    Do
    .MoveDown Unit:=wdLine, Count:=1
    .HomeKey Unit:=wdLine
    .MoveRight Unit:=wdSentence, Count:=1, Extend:=wdExtend
    If flag Then .Font.Bold = True: .Font.Size = 14
    flag = .Characters.Count = 1
    Loop While .End < ccc
    
    .HomeKey Unit:=wdStory
    End With

End Sub
this is working, BUT

how to avoid the "cursor movement" and selection ??
 
Upvote 0
Erik

If it's a new paragraph, I can get this to happen:

Try Out
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

We
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

Us
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.


Using:

Code:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 16/11/2007 by Me'
    Selection.HomeKey Unit:=wdStory
    Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
    Selection.Font.Bold = True
    Selection.EndKey Unit:=wdLine
   Do
     'Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "^p^p^$"
           End With
    Selection.Find.Execute
      
    Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
    Selection.Font.Bold = True
     Selection.EndKey Unit:=wdLine
    Loop 
End Sub

But this code won't stop looping. When I try to tell it when to stop , using: Until Selection.EndKey(Unit:=wdStory) or other variations, it stops at the end of the document, but doesn't do the actions.
 
Upvote 0
Hi, Marbles,

Still doing my first steps in Word...

Thanks for your answer. I appreciate the effort :) though I'm not quite happy with it, because my question was:
how to avoid the "cursor movement" and selection ??

NdNoviceHlp refered me to another link in another forum. Saw some new ideas and then suddendly it was easy :)

Code:
Dim P As Paragraph
Dim flag As Boolean

    For Each P In ActiveDocument.Paragraphs
    If flag Then P.Range.Bold = True
    flag = P.Range.Text = vbCr
    Next
no selections & cursorpositions involved

have a nice weekend!
Erik
 
Upvote 0
Erik

I only use Word occasionally, and only got that effort by pottering about with the Macro Recorder. I'll use your's now, and see where it leads to.
 
Upvote 0
Erik

I only use Word occasionally, and only got that effort by pottering about with the Macro Recorder.
Me too :)
only used Word to type some text, generally just one sheet, but a new challenge got my interest. It seems like there is a great need in Belgium music schools for what I'm doing now.

Problem:
When creating program booklets for exams or concerts, there is often a lot of copy paste to do followed by cleaning up the text and formatting over and over again the mess produced by new adds: teachers handle over their programs for concerts and exams in the most strange formats. Another problem is when order has to be changed.

Solution:
I decided to create an Excel project, where all program items can be entered, manually or by code (cleaning up the messy formats), where an order can be entered, sorted, etceterea ... I didn't do all that work yet, but concentrated on the final buttonclick: creating a Word document when the list is finished.

Thanks you for your input :)
Erik
 
Upvote 0
Hi Erik,

The problem you're going to have with this, whether in Word or Excel, is that lines wrap in different places on different computers, depending on the print drivers being used. In both apps, the problem in exacerbated by the fact that what displays on a line often changes when you switch between normal layout, page layout and print preview (though Word doesn't tend to change so much between the latter two).

The other, insurmountable, problem you'll have is that the very act of making the last word/character on a line bold can force it onto the next line. So you end up having all words/charcters bar one on the first line bolded or all plus the first on the next line bolded.

Anyway here's a couple of subs you can play with:
Code:
Sub HiLite1()
Dim oPara As Paragraph
Dim i As Integer
Dim xPos As Single
Application.ScreenUpdating = False
With ActiveDocument
    For Each oPara In .Paragraphs
        With oPara.Range
            xPos = .Characters(1).Information(wdVerticalPositionRelativeToTextBoundary)
            For i = 1 To .Words.Count - 1
                .Words(i).Font.Bold = True
                If .Words(i + 1).Information(wdVerticalPositionRelativeToTextBoundary) _
                    <> xPos Then Exit For
            Next
        End With
    Next
End With
Application.ScreenUpdating = True
End Sub

Sub HiLite2()
Dim oPara As Paragraph
Dim oLine As Range
Application.ScreenUpdating = False
With ActiveDocument
    For Each oPara In .Paragraphs
        oPara.Range.Select
        With Selection
            .Collapse (wdCollapseStart)
            .MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
            .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
            .Font.Bold = True
        End With
    Next
End With
Application.ScreenUpdating = True
End Sub
Cheers
 
Upvote 0
Hi, Macropod,

Thank you for your comments.
At the moment of posting the question, the words I have used might not have been clear for a Word-specialist and perhaps confusing for others. In fact the bold lines are paragraphs, not even sentences and certainly not lines. My bad :oops: as a Word-newbie.
The code I posted has
'format each sentence after empty
...
.MoveRight Unit:=wdSentence
Because that code is looking for sentences, the "linewrap" shouldn't be a problem, unless I'm missing something.

Thank you for your code, which was an invitation to look again in the Helpfiles. I learned something about wdInformation and Collapse, perhaps more tomorrow :)

have a nice weekend!
Erik
 
Upvote 0
Hi Eric,

In that case
Code:
Sub HiLite3()
Dim oPara As Paragraph
Application.ScreenUpdating = False
With ActiveDocument
    For Each oPara In .Paragraphs
        With oPara.Range
            If .Sentences.Count = 1 And Trim(.Words(1).Text) <> vbCr Then .Font.Bold = True
        End With
    Next
End With
Application.ScreenUpdating = True
End Sub
However, there is a better way for dealing with headings, and that's through the use of styles, wherein you simply attach a style with the desired attributesto the paragraph. This lets you control all of the paragraph's formatting in in step and, if you decide you want all paragraphs based on that style to look different later on, you simply change the style definition. For example, here's some code to attach the Heading3 style:
Code:
Sub HiLite4()
Dim oPara As Paragraph
Application.ScreenUpdating = False
With ActiveDocument
    For Each oPara In .Paragraphs
        With oPara.Range
            If .Sentences.Count = 1 And Trim(.Words(1).Text) <> vbCr Then .Style = wdStyleHeading3
        End With
    Next
End With
Application.ScreenUpdating = True
End Sub
Cheers
 
Upvote 0
Hi,

Thanks you for the follow up. I learned again something, by analysing your code. It is not working for me: only some headers are highlighted, but not the body as displayed in the example of my first post.

I tried to explain that those "lines" are all paragraphs. The goal is to bold every first paragraph after an empty one. (thought that would have been clear with the example). The styles idea looks good, but I'm still not "getting" the flow of that. These are my first steps...

I already have a solution, which does the job (see above) but if it can be done with styles - probably without loop - I'm always interested. Do not bother if you're short of time. Anyway I'll dig deeper.

Thank you for the assistance!

hace a nice weekend!
Erik
 
Upvote 0

Forum statistics

Threads
1,216,096
Messages
6,128,809
Members
449,468
Latest member
AGreen17

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