Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

How to say "paragraph the cursor is in"?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Barbara - 11 Mar 2008 16:43 GMT
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!
Signature

Barbara Hill

Benjamino5 - 11 Mar 2008 16:54 GMT
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

> 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
[quoted text clipped - 29 lines]
>
> Thanks for all your help!
Barbara - 11 Mar 2008 19:43 GMT
Thanks very much, Ben! Like you, I am learning the utility of setting a Range.
Signature

Barbara Hill

> Barbara,
>
[quoted text clipped - 48 lines]
> >
> > Thanks for all your help!
Jean-Guy Marcil - 11 Mar 2008 18:38 GMT
> 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
[quoted text clipped - 18 lines]
> 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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.