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 / May 2006

Tip: Looking for answers? Try searching our database.

set Shape as range

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
zSplash - 17 Apr 2006 22:27 GMT
How do I set ActiveDocument.Shapes(2) as myRange?

I tried:
   Dim myRange as Range
   set myRange=ActiveDocument.Shapes(2)

...but, of course, got an error, because a shape is a different type than a
range.  I thought about declaring myRange as a shape, but then it wouldn't
be a range, and I couldn't manipulate it the way I want to.  Is the
ShapeRange object what I should be understanding?

TIA
Jezebel - 17 Apr 2006 22:58 GMT
What are you trying to do? If you want a reference to the range to which the
shape is anchored, use

set myRange = ActiveDocument.Shapes(2).Anchor

> How do I set ActiveDocument.Shapes(2) as myRange?
>
[quoted text clipped - 9 lines]
>
> TIA
Jean-Guy Marcil - 17 Apr 2006 22:59 GMT
zSplash was telling us:
zSplash nous racontait que :

> How do I set ActiveDocument.Shapes(2) as myRange?
>
[quoted text clipped - 6 lines]
> it wouldn't be a range, and I couldn't manipulate it the way I want
> to.  Is the ShapeRange object what I should be understanding?

Then declare a Shape object:

   Dim myShape As Shape

   Set myShape = ActiveDocument.Shapes(2)

   myShape.Select

and manipulate that.

You cannot apply range properties/methods to a shape object anyway.

What kind on manipulation do you want to do to shape object that are
normally done to a range object?

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

zSplash - 18 Apr 2006 00:03 GMT
Thanks, guys.

I am trying to "clean up sloppy coding" to replace Selection objects with
Range objects.  A part of my code looped through the shapes, and then
replaced some shapes with other shapes (by opening another Word document and
getting the "new" shapes), using the Selection object.  I just want to put
the "retrieved" shape where the deleted shape was, using a Range object.

Now, my question is, how do I edit the shape's (a textbox) font without the
Selection object... (see With oShape.Anchor.Font, below).

I think I may have solved my initial problem by declaring oShape, as
suggested by Jezebel (thanks!).  Here's my code:
Sub getShapes()
   Dim oShape As Shape, oRng As Range, X As Integer
   Dim myBox As String
   For X = 1 To ActiveDocument.Shapes.Count
       If ActiveDocument.Shapes(X).Name = "myBox" Then
           Set oShape = ActiveDocument.Shapes(X)
           Set oRng = ActiveDocument.Shapes(X).Anchor
           Documents.Open FileName:="c:\myBox.doc"
           ActiveDocument.Shapes(1).Select
           Selection.Copy
           ActiveWindow.Close savechanges:=False
           oShape.Delete
           oRng.Paste
           Set oShape = ActiveDocument.Shapes(X)
           With oShape.Anchor.Font
               .Name = "Arial"
               .Size = 12
               .Bold = True
               .Color = wdColorAqua
           End With
   Next
...
theEnd:
End Sub

TIA, again.

st.

> zSplash was telling us:
> zSplash nous racontait que :
[quoted text clipped - 24 lines]
> What kind on manipulation do you want to do to shape object that are
> normally done to a range object?
Jezebel - 18 Apr 2006 00:36 GMT
Sorry, you've misunderstood what the anchor refers to: the anchor is the
range that the shape is attached to, not the range of the shape's contents.
(Go to Tools > Options, check the 'Object anchors' checkbox, then select the
textbox. You'll see a little anchor in the margin alongside a paragraph --  
usually the nearest. That paragraph is the anchor range.) To get to the
content of the textbox, use MyShape.TextFrame.TextRange.

Rather than copying and pasting the textboxes, it might be simpler to copy
the properties --

Dim pDoc as Word.Document
Dim pSource as Word.Shape
Dim pTarget as Word.Shape

'Get the target
Set pTarget = ActiveDocument.Shapes("myBox")

'Get the source
Set pDoc = Documents.Open(FileName:="c:\myBox.doc")
Set pSource = pDoc.Shapes(1)

'Copy from source to target
pSource.Pickup
pTarget.Apply
pTarget.Width = pSource.Width
pTarget.Height = pSource.Height
pTarget.TextFrame.TextRange = pSource.TextFrame.TextRange

etc

'Close the source doc
Set pSource = nothing
pDoc.Close SaveChanges:=Nothing

> Thanks, guys.
>
[quoted text clipped - 68 lines]
>> What kind on manipulation do you want to do to shape object that are
>> normally done to a range object?
zSplash - 19 Apr 2006 18:14 GMT
Amazing, Jezebel!  (Compacted the code by more than 75%!!)  Your suggestions
(and code) worked great!  And, I learned some new concepts and ways to look
at a solution, through you.

Thank you so very much!

st.

> Sorry, you've misunderstood what the anchor refers to: the anchor is the
> range that the shape is attached to, not the range of the shape's contents.
[quoted text clipped - 102 lines]
> >> What kind on manipulation do you want to do to shape object that are
> >> normally done to a range object?
zSplash - 26 Apr 2006 00:22 GMT
Hej, Jezebel,

There are some mailmerge fields in the textbox that I'm copying.  How would
I copy those mailmerge fields from pSource to pTarget?  (Textfields doesn't
"do it"!)

TIA

st.

> Sorry, you've misunderstood what the anchor refers to: the anchor is the
> range that the shape is attached to, not the range of the shape's contents.
[quoted text clipped - 102 lines]
> >> What kind on manipulation do you want to do to shape object that are
> >> normally done to a range object?
Cindy M  -WordMVP- - 28 Apr 2006 13:55 GMT
Hi ZSplash,

> There are some mailmerge fields in the textbox that I'm copying.  How would
> I copy those mailmerge fields from pSource to pTarget?  (Textfields doesn't
> "do it"!)

Have you tested whether the mergefields actually function in the textboxes?
Generally, I find that a combination to avoid...

Anyway, to pick up the content of an AutoForm that contains text you have to
address its TextFrame.TextRange property. I'd go about it something like
this:

   pTarget.TextFrame.TextRange.FormattedText =
pSource.TextFrame.TextRange.FormattedText

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)
zSplash - 15 May 2006 21:58 GMT
Yes, Cindy.  It's important that the mergefields remain in the textbox.
I will try your suggestion.  Thanks so much for your help!

st.

> Hi ZSplash,
>
[quoted text clipped - 19 lines]
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)
 
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.