VBA: How to recognize if the first word of a bulleted par. has been selected

D

david.f.jenkins

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?
 
J

Jonathan West

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?

If Selection.Start = Selection.Paragraphs(1).Range.Start Then
'selection is at start of paragraph
Else
'it isn't
End If


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

david.f.jenkins

Jonathan said:
If Selection.Start = Selection.Paragraphs(1).Range.Start Then
'selection is at start of paragraph
Else
'it isn't
End If

Thanks, Jon. But having a little difficulty implementing your
suggestion. Suppose I have a set of bullets A, B and C:

A. Text for bullet A
B. Text for bullet B
C. Text for bullet C

and the user has selected "bullet B" from bullet B and "Text" from
bullet C.

I need to know if the "bullet B" selection is at the start of its
associated real paragraph (bullet) and the same thing about "Text".
However, the .start for the complete TextRange is the same as the
..start for the first paragraph for TextRange for selected text out
bullet B, and the selection is clearly not at the start of bullet B.
The same test would hold true for the selection of "Text" from bullet
C, but that's moot at this point.

So I guess I'm missing the thrust of what's behind your suggestion -
could you explain in a little more detail?

Thanks!
 
J

Jonathan West

Thanks, Jon. But having a little difficulty implementing your
suggestion. Suppose I have a set of bullets A, B and C:

A. Text for bullet A
B. Text for bullet B
C. Text for bullet C

and the user has selected "bullet B" from bullet B and "Text" from
bullet C.

I need to know if the "bullet B" selection is at the start of its
associated real paragraph (bullet) and the same thing about "Text".
However, the .start for the complete TextRange is the same as the
.start for the first paragraph for TextRange for selected text out
bullet B, and the selection is clearly not at the start of bullet B.
The same test would hold true for the selection of "Text" from bullet
C, but that's moot at this point.

So I guess I'm missing the thrust of what's behind your suggestion -
could you explain in a little more detail?


OK, Selection.Start gives you the character position of the start of the
selection. Selection.Paragraphs is the collection of all paragraphs (or
parts thereof) within the selection.

If Selection.Paragraphs.Count is greater than 1, then you know you have a
selection which straddles 2 or more paragraphs.

Whether the start of the selection is at the start of a paragraph can be
tested by comparing the character position of the selection's start with the
character position of the start of the first paragraph selected. If they are
the same, the selection is at the start of the paragraph.

If the selection has more than one paragraph, and you want to process the
start of the second and subsequent selected paragraphs, then you can iterate
through the paragraphs collection. Each paragraph has a Range property which
you can think of as being like a Selection, but which marks the paragraph.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

david.f.jenkins

Jonathan said:
OK, Selection.Start gives you the character position of the start of the
selection. Selection.Paragraphs is the collection of all paragraphs (or
parts thereof) within the selection.

If Selection.Paragraphs.Count is greater than 1, then you know you have a
selection which straddles 2 or more paragraphs.

Whether the start of the selection is at the start of a paragraph can be
tested by comparing the character position of the selection's start with the
character position of the start of the first paragraph selected. If they are
the same, the selection is at the start of the paragraph.

If the selection has more than one paragraph, and you want to process the
start of the second and subsequent selected paragraphs, then you can iterate
through the paragraphs collection. Each paragraph has a Range property which
you can think of as being like a Selection, but which marks the paragraph.

Thanks again, Jon.

Here are some problems I'm having implementing your suggestion
1. A Selection object doesn't seem to have a Start property
2. If I modfy the code to reference the TextRange derived from the
Selection, the Start property I can then access seems to be the
starting character of the selected text relative to all of the text in
the enclosing shape.
3. And Lastly, if I examine the Paragraphs of the selected text, they
don't include an entire bullet unless the selection actually included
the entire bullet. That is, if the selection (referring to my example
in the original post) were "for bullet A" then the only paragraph for
that selection would be the text "for bullet A" For instance, if I
write this:

Dim oPar As TextRange
For Each oPar In ActiveWindow.Selection.TextRange.Paragraphs
oPar.Text = oPar.Text
Next oPar

then at each iteration (only 1 in the case of my example) the paragraph
seems to consist of only the selected text form the bullet - not the
entire bullet from which the text was selected.

Sorry to be so dense on all this, but VBA Shapes, TextRanges,
Paragraphs, and Sentences have always given me fits!
 
S

Steve Rindsberg

Here are some problems I'm having implementing your suggestion

I'll add what I figure is the main one:

You're asking in PowerPoint, I suspect, and Jonathan's answering in Word.
That's a very good way to annoy one another. ;-)

Since this is a generic Office group, it's always a good thing to begin by
stating which applications you're working in.
 
D

david.f.jenkins

Yikes! Of course, I'm speaking PPT. I posted here first, by mistake,
and having discovered that error, went over to the PPT group and
reposted. I forgot to come back here and turn off the valve after I
did that. Then, when I saw what appeared to be such an elegant
solution here, I got so excited I forgot about the possbility it might
have been answered in terms of some other Office App.

My apologies for the wasted bandwidth, and I'll now slink back over to
the other group.

(Still have the problem, though...)
 

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