PDA

View Full Version : Setting Range in For Each Loop



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

gmaxey
01-23-2013, 07:27 PM
Try:

Sub FormattingFun()
Dim oPara As Paragraph
Dim myRange As Range
For Each oPara In ActiveDocument.Paragraphs
Set myRange = oPara.Range
myRange.Collapse wdCollapseStart
myRange.MoveEndUntil Cset:=" "
With myRange
.Bold = True
.Font.AllCaps = True
End With
Next oPara
End Sub

Your original method would be more like this:

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 = InStr(oPara.Range.Text, " ")
Set myRange = oPara.Range
myRange.End = myRange.Start + positionOfFirstSpace - 1
With myRange
.Bold = True
.Font.AllCaps = True
End With
Next oPara
End Sub

Frosty
01-23-2013, 08:24 PM
Just be aware that you will only get the space... Not a hard space or tab character. You could also use a find object for the "^w" special search that finds any white space. Sorry, I'm away from my computer so I can't give a code sample... But none of the above methodologies seem to have a limiter... What happens on a paragraph which has no spaces? And what if the last paragraph in the document has no spaces?

gmaxey
01-24-2013, 06:09 AM
Jason,

You are correct of course. As usual, I typically only look at the question asked where you typically think deeper about what the OP may really need.

The find ^w is a good approach, but it would miss Em and En spaces (as unlikely as it is that they would be encountered), so to be as thorough as I know how:
Sub FormattingFun()
Dim oPar As Paragraph
Dim oRng As Range
For Each oPar In ActiveDocument.Paragraphs
Set oRng = oPar.Range
oRng.Collapse wdCollapseStart
'Move end until first normal space, tab, non-breaking space, _
Em space, En space, 1/4 Em space or end of paragraph
oRng.MoveEndUntil Cset:=Chr(32) _
& Chr(9) _
& Chr(160) _
& ChrW(8194) _
& ChrW(8195) _
& ChrW(8197) _
& Chr(13)
If oRng.Characters.Last.Next = vbCr Then
If MsgBox("This paragraph does not contain a space." _
& vbCr + vbCr & "Do you want to format it anyway?", vbYesNo, "Format??") = vbYes Then
With oRng
.Bold = True
.Font.AllCaps = True
End With
End If
Else
With oRng
.Bold = True
.Font.AllCaps = True
End With
End If
Next oPar
End Sub

sjpeters75
01-26-2013, 10:59 AM
Gentlemen,

Thank you for your replies. I used a combination of your suggestions to get the result I needed. I'm finding it much more difficult to teach myself VBA than Java, for instance, in large part to what seems to be a dearth of good reference material. Again, thanks much for the help.

v/r,
steve

fumei
01-26-2013, 02:41 PM
There is actually a fair amount of VBA material out there, but it can be a little bit of search to find it.

While out-of-date for the new pieces added for 2007/2010 VBA Developers Handbook has a wealth of stuff. I recently saw a copy in a used computer book store for $8. So it is around.