G
Greg
I was trying to apply a style to the first sentence of each paragraph
in a document.
For example:
This is paragraph 1 sentence 1. This is paragraph 1 sentence 2. Etc.
This is paragraph 2 sentence 1. This is paragraph 2 sentence 2. Etc.
Using this code you will see the message box accurately reports the
first sentence text, but the .style is applied to the entire paragraph.
Sub Scratchmacro1()
Dim oPar As Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Range.Paragraphs
MsgBox oPar.Range.Sentences(1)
oPar.Range.Sentences(1).Style = "First Sentence"
Next oPar
End Sub
However, if I first select the defined range then apply the style all
is well:
Sub Scratchmacro2()
Dim oPar As Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Range.Paragraphs
oPar.Range.Sentences(1).Select
Selection.Style = "First Sentence"
Next oPar
End Sub
Can anyone explain this behaviour?
Thanks.
in a document.
For example:
This is paragraph 1 sentence 1. This is paragraph 1 sentence 2. Etc.
This is paragraph 2 sentence 1. This is paragraph 2 sentence 2. Etc.
Using this code you will see the message box accurately reports the
first sentence text, but the .style is applied to the entire paragraph.
Sub Scratchmacro1()
Dim oPar As Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Range.Paragraphs
MsgBox oPar.Range.Sentences(1)
oPar.Range.Sentences(1).Style = "First Sentence"
Next oPar
End Sub
However, if I first select the defined range then apply the style all
is well:
Sub Scratchmacro2()
Dim oPar As Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Range.Paragraphs
oPar.Range.Sentences(1).Select
Selection.Style = "First Sentence"
Next oPar
End Sub
Can anyone explain this behaviour?
Thanks.