set Shape as range

Z

zSplash

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
 
J

Jezebel

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
 
J

Jean-Guy Marcil

zSplash was telling us:
zSplash nous racontait que :
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?

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?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
Z

zSplash

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

Jezebel

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
 
Z

zSplash

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.
 
Z

zSplash

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.
 
C

Cindy M -WordMVP-

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 :)
 
Z

zSplash

Yes, Cindy. It's important that the mergefields remain in the textbox.
I will try your suggestion. Thanks so much for your help!

st.
 

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