Replacing style of the first word of a paragraph only

H

homeologica

In a document I must convert only the first word, (entry), of a paragraph.
This first word have a specific font different to rest of the paragraph.

Can I do this?

Thanks.

Alejandro Fernandez
 
G

Greg Maxey

Doesn't change the style, but does change the font of the first word.

Sub Test()
Dim oPar
For Each oPar In ActiveDocument.Paragraphs
oPar.Range.Words.First.Font.Name = "Courier New"
Next
End Sub
 
H

homeologica

Thanks Greg. I need to change the style because headings must have StyleRef
DefinedStyle. Only First words must appear in this Headings
 
G

Greg Maxey

Try:

Sub Test()
Dim oPar
For Each oPar In ActiveDocument.Paragraphs
oPar.Range.Words.First.Select
Selection.Style = ActiveDocument.Styles("Heading 1 Char")
Next
End Sub
 
H

homeologica

Greg.

I am trying some modifications in your code. I think this is very close to
the real solution.

Many thanks.

Alejandro Fernandez
 
H

homeologica

Greg
I need to select the first word of each paragraph which be a word formattaed
as Tahoma 11 points font . Not other first words. This code converts to new
style paragraph marks too. I must avoid this.

So, the statement

oPar.Range.Words.First.Select

Must be changed or validated

Thank you for your valuable help.

Alejandro Fernandez
 
G

Greg Maxey

Try:
Sub Test()
Dim oPar
For Each oPar In ActiveDocument.Paragraphs
oPar.Range.Words.First.Select
Selection.MoveEnd wdCharacter, -1
With Selection
If .Font.Name = "Tahoma" And .Font.Size = "11" Then
.Style = ActiveDocument.Styles("Heading 1")
End If
End With
Next
End Sub
 
H

homeologica

Thanks Daiya:

Your procedure format all paragraph instead the first word (Tahoma 11pt)

This word must appear in each page heading.

Alejandro Fernandez
 
D

Daiya Mitchell

Hi Alejandro,

You did say that the word you need to apply the style to already has a
different font from the rest, right? Because that should work, if those
words are the only ones formatted in that font--I hadn't tested it before
but I just did and it works here (MacWord 2004).

However, in the Replace box, the style you choose *must* be a character
style (denoted by underscored "a"), not a paragraph style (denoted by ¶ in
the list in the Style dialog).

If you use a paragraph style for the Replace setting, then the entire
paragraph does change. But it works if you use a character style, and a
character style is what you want for dictionary-style headings anyhow.

Is your DefinedStyle a paragraph or a character style?
 
H

homeologica

Yes Daiya, you're right. Character style works fine.

Thanks a lot. Thanks to Greg too.

Alejandro Fernandez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top