How to select a graphic

R

Robbie

I thought I'd feed this back to the newsgroup, since it
took so long to work out, maybe someone else can benefit.
I'm was in Word 2000, halfway thru a macro that 'cuts' a
bitmap graphic, then pastes the graphic back
(Edit|Paste_Special|Paste_as_Picture) as a "picture".
I wanted to select what has just been pasted, and then do
further action on it, eg move it into place.
What I couldn't work out was the code needed to select the
picture again once it had been pasted into the document.
Word numbers each graphic (eg "Picture 3"), but I couldn't
use that to select it because the numbers may change.

Recording my actions gave me a macro something like
Sub MacroImageMove()
' ImageMove Macro
Selection.Cut
Selection.PasteSpecial Link:=False, _
DataType:=wdPasteMetafilePicture, _
Placement:=wdFloatOverText, DisplayAsIcon:=False
ActiveDocument.Shapes("Picture 3").Select
'What can I use instead of the previous line(s)?
End Sub

The problem had something to do with the difference
between wdFloatOverText and wdInLine (neither of which can
I find an explanation for). But I still didn't know to
select it!

So this is the final answer.

Sub MacroImageMove()
' I want to select the image, then run the macro from the
toolbar so that
' it cuts the graphic and pastes it back as a "picture"
(smaller file size).
' Then I can move it to the right of the page, top of the
line, or else as necessary.
' The problem I faced was how to select the picture that
has just been pasted,
' rather than eg the specific "Picture 3".
Selection.Cut
Selection.PasteSpecial Link:=False,
DataType:=wdPasteMetafilePicture, _
Placement:=wdInLine,
DisplayAsIcon:=False 'Not wdFloatOverText
' ActiveDocument.Shapes("Picture 3").Select 'this is the
line that was problem
Selection.Extend
Selection.MoveLeft
Selection.InlineShapes(1).Select
Selection.InlineShapes(1).ConvertToShape
With Selection.ShapeRange
.Line.Visible = msoFalse
.RelativeHorizontalPosition =
wdRelativeHorizontalPositionMargin
.RelativeVerticalPosition =
wdRelativeVerticalPositionLine
.Left = wdShapeRight
.Top = wdShapeTop
.LockAnchor = False
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapLeft
.WrapFormat.DistanceTop = CentimetersToPoints(0)
.WrapFormat.DistanceBottom = CentimetersToPoints(0)
.WrapFormat.DistanceLeft = CentimetersToPoints(0.1)
.WrapFormat.DistanceRight = CentimetersToPoints
(0.1)
.WrapFormat.Type = wdWrapTight
End With
End Sub

Not elegant, but what code is with VBA?
NB The text is too wide & has got wrapped in this post.

Hope this helps someone else.
Robbie
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Robbie > écrivait :
In this message, < Robbie > wrote:

|| I thought I'd feed this back to the newsgroup, since it
|| took so long to work out, maybe someone else can benefit.
|| I'm was in Word 2000, halfway thru a macro that 'cuts' a
|| bitmap graphic, then pastes the graphic back
|| (Edit|Paste_Special|Paste_as_Picture) as a "picture".
|| I wanted to select what has just been pasted, and then do
|| further action on it, eg move it into place.
|| What I couldn't work out was the code needed to select the
|| picture again once it had been pasted into the document.
|| Word numbers each graphic (eg "Picture 3"), but I couldn't
|| use that to select it because the numbers may change.

Just in case you find the following useful...

You may want to consider using declared objects instead of the Selection
one. On June 4th I posted something in reply to someone who was also
manipulating graphics. I posted two versions (floating and inline graphics).
Also, use as many With .. End.with as possible. It is a good habit to get
into, especially when you write longer macros. If I understood correctly
when someone wrote about this in this group, it cuts down on memory
allocation.

Here is excerpt of the code I posted:

<q>
<snip>
An even better way to manipulate the shape would be:

'_______________________________________
Sub PasteAsPicture()

'Declare the object variables
Dim MyRange As Range
Dim MyShape As Shape

'Paste the picture
Selection.PasteSpecial Link:=False, _
DataType:=wdPasteEnhancedMetafile, _
Placement:=wdFloatOverText, _
DisplayAsIcon:=False

'Set the range to the paragraph where the picture was pasted
Set MyRange = Selection.Paragraphs(1).Range

'Set the shape to the first shape in that range
'presumably the one just pasted
'If there were already some shapes anchored in that paragraph,
'then you would have to play with the number in
'ShapeRange(1) to grab the right shape
Set MyShape = MyRange.ShapeRange(1)

'Manipulate the shape
With MyShape
.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = _
wdRelativeVerticalPositionPage
.Top = InchesToPoints(2)
.Left = InchesToPoints(2)
.ScaleHeight 1.5, msoTrue
.ScaleWidth 1.5, msoTrue
With .Line
.Weight = 3#
.DashStyle = msoLineSolid
.Style = msoLineSingle
.Transparency = 0#
.Visible = msoTrue
End With
End With

'Release the references
Set MyRange = Nothing
Set MyShape = Nothing

End Sub
'_______________________________________

But if you wanted to manipulate an inline shape, the code would be slightly
different:
'_______________________________________
Sub PasteAsPicture()

Dim MyRange As Range
Dim MyShape As InlineShape

Selection.PasteSpecial Link:=False, _
DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, _
DisplayAsIcon:=False

Set MyRange = Selection.Paragraphs(1).Range
Set MyShape = MyRange.InlineShapes(1)

With MyShape
.ScaleHeight = 125
.ScaleWidth = 125
With .Line
.Weight = 3#
.DashStyle = msoLineSolid
.Style = msoLineSingle
.Transparency = 0#
.Visible = msoTrue
End With
End With

Set MyRange = Nothing
Set MyShape = Nothing

End Sub
'_______________________________________

Good luck!
<snip>
<\q>

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

Robbie

Merci, Jean-Guy
That is much more elegant code.
The problem for an amateur like me me is that the VBA
recorder never used "ShapeRange" and I did not know it
existed. And if I'd looked harder< I might have
found "InlineShapes" instead, which would have added to
the confusion!
Anyway, all solved.
Thanks
Robbie
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Robbie > écrivait :
In this message, < Robbie > wrote:

| Merci, Jean-Guy
| That is much more elegant code.
| The problem for an amateur like me me is that the VBA
| recorder never used "ShapeRange" and I did not know it
| existed. And if I'd looked harder< I might have
| found "InlineShapes" instead, which would have added to
| the confusion!

Don't worry, I am an amateur as well, I just have a bit more experience with
VBA than you have...

Do explore Range objects if you have a bit of free time, you will find that
your code will be much more stable and, once you understand how they work,
much easier to control. One big advantage is that you can even work on an
invisible document.

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

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