VBA: How to tell if selected text starts with first word of a paragraph

  • Thread starter Thread starter david.f.jenkins
  • Start date Start date
D

david.f.jenkins

[I also mistakenly posted this in microsoft.public.office.developer.vba
- meant to put it here all along. Sorry for the double-post.]

I'm attempting to write code that will capitalize the first word of
text in a bullet, but only if selected text incorporates the first word

of the bullet. To do so, I think I need to know:

a) If the selected text is contained in a paragraph that has been
formatted as part of a bullet (I think I can figure that out)
b) If the text that has been selected starts with the first word in the

bulleted paragraph

It's (b) that's got me stumped. Can anyone offer any suggestions as to

how to get this done in VBA?
 
This seems to work, but I'm sure it will screw up something (watch out
for erroneous line breaks inserted by the news system):

Sub CapMe()
Dim oPar As TextRange
Dim parText As String
Dim selectionText As String
If ActiveWindow.Selection.Type = ppSelectionText Then
With ActiveWindow.Selection.TextRange
If .ParagraphFormat.Bullet.Type <> ppBulletNone Then
selectionText = .Text
For Each oPar In
ActiveWindow.Selection.ShapeRange.TextFrame _
.TextRange.Paragraphs
If .Text = oPar.Characters(-1, Len(.Text)).Text Then
oPar.Characters(-1, Len(.Text)).ChangeCase
ppCaseTitle
If .Text <> oPar.Characters(-1, Len(.Text)).Text
Then
oPar.Characters(-1, Len(.Text)).Text = .Text
End If
End If
Next oPar
End If
End With
End If
End Sub


--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/

(e-mail address removed) wrote in @i3g2000cwc.googlegroups.com:
 
Hi David:

Thanks for taking the time to struggle through this with me. I'm
having some difficulty working through your code, and applying it to
the various typical possibilities that may occur in my application.

For instance, I'm curious about the Start = -1 in the Characters()
calls - what does that do? It would seem to me that it will create a
TextRange is the length of ALL the selected text - even if that text
spans individual bullets.

Also, what is the purpose of the innermost If/End if?

And finally, What I really want to do is apply title case only to the
first letter of the first word of a bullet, if that character is part
of a selection. (Keeping always in mind that any selection could start
in the middle of a bullet, but span bullets.) Perhaps stating the
problem that way might make it easier to solve?

Thanks!
This seems to work, but I'm sure it will screw up something (watch out
for erroneous line breaks inserted by the news system):

Sub CapMe()
Dim oPar As TextRange
Dim parText As String
Dim selectionText As String
If ActiveWindow.Selection.Type = ppSelectionText Then
With ActiveWindow.Selection.TextRange
If .ParagraphFormat.Bullet.Type <> ppBulletNone Then
selectionText = .Text
For Each oPar In
ActiveWindow.Selection.ShapeRange.TextFrame _
.TextRange.Paragraphs
If .Text = oPar.Characters(-1, Len(.Text)).Text Then
oPar.Characters(-1, Len(.Text)).ChangeCase
ppCaseTitle
If .Text <> oPar.Characters(-1, Len(.Text)).Text
Then
oPar.Characters(-1, Len(.Text)).Text = .Text
End If
End If
Next oPar
End If
End With
End If
End Sub


--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/

(e-mail address removed) wrote in @i3g2000cwc.googlegroups.com:
[I also mistakenly posted this in microsoft.public.office.developer.vba
- meant to put it here all along. Sorry for the double-post.]

I'm attempting to write code that will capitalize the first word of
text in a bullet, but only if selected text incorporates the first word

of the bullet. To do so, I think I need to know:

a) If the selected text is contained in a paragraph that has been
formatted as part of a bullet (I think I can figure that out)
b) If the text that has been selected starts with the first word in the

bulleted paragraph

It's (b) that's got me stumped. Can anyone offer any suggestions as to

how to get this done in VBA?
 
Here's the code I finally came up with, and it appears to work pretty
effectively. It's not very elegant, however:

dim selPar as Object
dim shpPar as Object
dim tFrame as TextFrame

Set tFrame = tRange.Parent
For Each selPar In tRange.Paragraphs
For Each shpPar In tFrame.TextRange.Paragraphs
If selPar.Start = shpPar.Start Then
' is this paragraph a bullet?
If shpPar.ParagraphFormat.Bullet.Type <> ppBulletNone
Then
[do capitalization stuff]
End If
Exit For
End If
Next shpPar
Next selPar

Hi David:

Thanks for taking the time to struggle through this with me. I'm
having some difficulty working through your code, and applying it to
the various typical possibilities that may occur in my application.

For instance, I'm curious about the Start = -1 in the Characters()
calls - what does that do? It would seem to me that it will create a
TextRange is the length of ALL the selected text - even if that text
spans individual bullets.

Also, what is the purpose of the innermost If/End if?

And finally, What I really want to do is apply title case only to the
first letter of the first word of a bullet, if that character is part
of a selection. (Keeping always in mind that any selection could start
in the middle of a bullet, but span bullets.) Perhaps stating the
problem that way might make it easier to solve?

Thanks!
This seems to work, but I'm sure it will screw up something (watch out
for erroneous line breaks inserted by the news system):

Sub CapMe()
Dim oPar As TextRange
Dim parText As String
Dim selectionText As String
If ActiveWindow.Selection.Type = ppSelectionText Then
With ActiveWindow.Selection.TextRange
If .ParagraphFormat.Bullet.Type <> ppBulletNone Then
selectionText = .Text
For Each oPar In
ActiveWindow.Selection.ShapeRange.TextFrame _
.TextRange.Paragraphs
If .Text = oPar.Characters(-1, Len(.Text)).Text Then
oPar.Characters(-1, Len(.Text)).ChangeCase
ppCaseTitle
If .Text <> oPar.Characters(-1, Len(.Text)).Text
Then
oPar.Characters(-1, Len(.Text)).Text = .Text
End If
End If
Next oPar
End If
End With
End If
End Sub


--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/

(e-mail address removed) wrote in @i3g2000cwc.googlegroups.com:
[I also mistakenly posted this in microsoft.public.office.developer.vba
- meant to put it here all along. Sorry for the double-post.]

I'm attempting to write code that will capitalize the first word of
text in a bullet, but only if selected text incorporates the first word

of the bullet. To do so, I think I need to know:

a) If the selected text is contained in a paragraph that has been
formatted as part of a bullet (I think I can figure that out)
b) If the text that has been selected starts with the first word in the

bulleted paragraph

It's (b) that's got me stumped. Can anyone offer any suggestions as to

how to get this done in VBA?
 
Of course! That is much cleaner. I forgot that the .Start tells where in
the larger TextRange this TextRange starts. That is the perfect solution.
--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/

(e-mail address removed) wrote in
Here's the code I finally came up with, and it appears to work pretty
effectively. It's not very elegant, however:

dim selPar as Object
dim shpPar as Object
dim tFrame as TextFrame

Set tFrame = tRange.Parent
For Each selPar In tRange.Paragraphs
For Each shpPar In tFrame.TextRange.Paragraphs
If selPar.Start = shpPar.Start Then
' is this paragraph a bullet?
If shpPar.ParagraphFormat.Bullet.Type <> ppBulletNone
Then
[do capitalization stuff]
End If
Exit For
End If
Next shpPar
Next selPar

Hi David:

Thanks for taking the time to struggle through this with me. I'm
having some difficulty working through your code, and applying it to
the various typical possibilities that may occur in my application.

For instance, I'm curious about the Start = -1 in the Characters()
calls - what does that do? It would seem to me that it will create a
TextRange is the length of ALL the selected text - even if that text
spans individual bullets.

Also, what is the purpose of the innermost If/End if?

And finally, What I really want to do is apply title case only to the
first letter of the first word of a bullet, if that character is part
of a selection. (Keeping always in mind that any selection could
start in the middle of a bullet, but span bullets.) Perhaps stating
the problem that way might make it easier to solve?

Thanks!
This seems to work, but I'm sure it will screw up something (watch
out for erroneous line breaks inserted by the news system):

Sub CapMe()
Dim oPar As TextRange
Dim parText As String
Dim selectionText As String
If ActiveWindow.Selection.Type = ppSelectionText Then
With ActiveWindow.Selection.TextRange
If .ParagraphFormat.Bullet.Type <> ppBulletNone Then
selectionText = .Text
For Each oPar In
ActiveWindow.Selection.ShapeRange.TextFrame _
.TextRange.Paragraphs
If .Text = oPar.Characters(-1, Len(.Text)).Text
Then
oPar.Characters(-1, Len(.Text)).ChangeCase
ppCaseTitle
If .Text <> oPar.Characters(-1,
Len(.Text)).Text
Then
oPar.Characters(-1, Len(.Text)).Text =
.Text
End If
End If
Next oPar
End If
End With
End If
End Sub


--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/

(e-mail address removed) wrote in @i3g2000cwc.googlegroups.com:

[I also mistakenly posted this in
microsoft.public.office.developer.vba - meant to put it here all
along. Sorry for the double-post.]

I'm attempting to write code that will capitalize the first word
of text in a bullet, but only if selected text incorporates the
first word

of the bullet. To do so, I think I need to know:

a) If the selected text is contained in a paragraph that has been
formatted as part of a bullet (I think I can figure that out)
b) If the text that has been selected starts with the first word
in the

bulleted paragraph

It's (b) that's got me stumped. Can anyone offer any suggestions
as to

how to get this done in VBA?
 
Back
Top