How to say "paragraph the cursor is in"?

B

Barbara

I am working on a macro to format graphics. (I owe it mainly to one of the
Word MVPs, but I have forgotten exactly where I found it. I am grateful to
the author, whoever it was.)

The macro starts by checking whether a floating shape, in-line shape, or
neither is selected. The in-line case is the one where I need some help. I
want to convert it to a floating shape and specify some layout settings. That
part is working fine. I also want to check whether the shape is in an
otherwise empty paragraph and, if so, delete the paragraph, leaving the shape
anchored to the preceding paragraph.

My current strategy is:

If an in-line shape is selected, then,
If the paragraph where the selection starts contains two characters
(namely, the shape and the paragraph break),
move the shape up one paragraph,
delete the now-empty paragraph
end if
convert to a floating shape
end if
etc.

After looking around on this site and the Word MVP site, I have constructed
an expression that works (I think) for "the paragraph where the selection
starts." But it is astonishingly long and obscure.

What is the simplest way of writing this expression? It seems like this is a
rather basic concept (also applicable to "the paragraph that holds this
sentence," "the table that holds this cell," "the section that holds this
character,"...) But I'm not finding any info about how to refer to it.

Thanks for all your help!
 
B

Benjamino5

Barbara,

You can use "Selection.Paragraphs.First" or "Selection.Paragraphs(1)" to
refer to the first paragraph in the selection (this works even if the
"selection" is just the cursor sitting in the paragraph). You can do
something similar for other objects--(mySentence.Paragraphs(1) or
Selection.Sentences(1).

I'd also recommend using the Range object whenever possible. I'm no expert,
but as I've learned more about VBA, I've found the Range object to be
incredibly useful in many of the places where I first thought I had to use
the Selection object. (Obviously here, you're starting with the Selection
object, but in other cases, it can help.) You don't have to worry about
what's selected if you use a Range, and you can use Selection and Range
together to do interesting things.

Hope this helps,
Ben
 
J

Jean-Guy Marcil

Barbara said:
I am working on a macro to format graphics. (I owe it mainly to one of the
Word MVPs, but I have forgotten exactly where I found it. I am grateful to
the author, whoever it was.)

The macro starts by checking whether a floating shape, in-line shape, or
neither is selected. The in-line case is the one where I need some help. I
want to convert it to a floating shape and specify some layout settings. That
part is working fine. I also want to check whether the shape is in an
otherwise empty paragraph and, if so, delete the paragraph, leaving the shape
anchored to the preceding paragraph.

My current strategy is:

If an in-line shape is selected, then,
If the paragraph where the selection starts contains two characters
(namely, the shape and the paragraph break),
move the shape up one paragraph,
delete the now-empty paragraph
end if
convert to a floating shape
end if
etc.

Here is one way of doing it:


Dim inlShConvert As InlineShape
Dim ShpConverted As Shape

If Selection.Type = wdSelectionInlineShape Then
With Selection.Range.Paragraphs(1)
If .Range.Characters.Count = 2 Then
.Previous.Range.Characters(1).FormattedText = _
.Range.Characters(1).FormattedText
.Range.Delete
Set inlShConvert = .Previous.Range.Characters(1).InlineShapes(1)
Else
Set inlShConvert = Selection.InlineShapes(1)
End If
End With
Set ShpConverted = inlShConvert.ConvertToShape
With ShpConverted
.WrapFormat.Type = wdWrapTight
'Other Manipulations
End With
Else
MsgBox "The selection is not an inline shape.", _
vbExclamation, "No Shape"
End If

End Sub
 

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