sjpeters75
01-23-2013, 07:01 PM
Hello,
I'm trying to loop through each paragraph in a document, set a range equal to everything from the start of the paragraph to the first space, and then apply a particular formatting to that string. range.Words(1) doesn't quite get me there, because dashes or underscores seem to throw it off.
The line that (I think) I'm screwing up is this one:
positionOfFirstSpace = Len(Left(myDocument.oPara.range.Text, _
InStr(myDocument.oPara.range.Text, " ") - 1))
which results in the error: Object Doesn't Support This Property or Method.
Obviously there's a flaw in my understanding, but I could kindly use some help pointing it out. Thank you very much.
steve
Sub FormattingFun()
Dim myDocument As Document
Set myDocument = ActiveDocument
Dim oPara As Paragraph
Dim myRange As range
Dim positionOfFirstSpace As Integer
For Each oPara In myDocument.Paragraphs
positionOfFirstSpace = Len(Left(myDocument.oPara.range.Text, _
InStr(myDocument.oPara.range.Text, " ") - 1))
Set myRange = myDocument.range( _
Start:=oPara.range.Words(1).Start, _
End:=oPara.range.Words(1).Start + positionOfFirstSpace)
With myRange
.Bold = True
.Font.AllCaps = True
End With
Next oPara
End Sub
I'm trying to loop through each paragraph in a document, set a range equal to everything from the start of the paragraph to the first space, and then apply a particular formatting to that string. range.Words(1) doesn't quite get me there, because dashes or underscores seem to throw it off.
The line that (I think) I'm screwing up is this one:
positionOfFirstSpace = Len(Left(myDocument.oPara.range.Text, _
InStr(myDocument.oPara.range.Text, " ") - 1))
which results in the error: Object Doesn't Support This Property or Method.
Obviously there's a flaw in my understanding, but I could kindly use some help pointing it out. Thank you very much.
steve
Sub FormattingFun()
Dim myDocument As Document
Set myDocument = ActiveDocument
Dim oPara As Paragraph
Dim myRange As range
Dim positionOfFirstSpace As Integer
For Each oPara In myDocument.Paragraphs
positionOfFirstSpace = Len(Left(myDocument.oPara.range.Text, _
InStr(myDocument.oPara.range.Text, " ") - 1))
Set myRange = myDocument.range( _
Start:=oPara.range.Words(1).Start, _
End:=oPara.range.Words(1).Start + positionOfFirstSpace)
With myRange
.Bold = True
.Font.AllCaps = True
End With
Next oPara
End Sub