Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I select a picture in MS Word using VBA 3

Status
Not open for further replies.

JensKKK

Technical User
May 8, 2007
119
GB
Hi there,

I think I have a very simple question. I am copy pasting graphs from MS Excel into MS Word (the graphs are transferred via clipboard)

Once pasted i want to change the size of the picture.

I have tried to record a macro, but the the macro recorder does not allow me to select a picture. My work around for this was to select the picture before recording the macro and then select the picture command from the Format menu. this gave me the code below, but I get everything to work I need to find out how to select a picture in VBA.

Many thanks for reading till hers.


Selection.InlineShapes(1).Fill.Visible = msoFalse
Selection.InlineShapes(1).Fill.Solid
Selection.InlineShapes(1).Fill.Transparency = 0#
Selection.InlineShapes(1).Line.Weight = 0.75
Selection.InlineShapes(1).Line.Transparency = 0#
Selection.InlineShapes(1).Line.Visible = msoFalse
Selection.InlineShapes(1).LockAspectRatio = msoTrue
Selection.InlineShapes(1).Height = 136.65
Selection.InlineShapes(1).Width = 190.5
Selection.InlineShapes(1).PictureFormat.Brightness = 0.5
Selection.InlineShapes(1).PictureFormat.Contrast = 0.5
Selection.InlineShapes(1).PictureFormat.ColorType = msoPictureAutomatic
Selection.InlineShapes(1).PictureFormat.CropLeft = 0#
Selection.InlineShapes(1).PictureFormat.CropRight = 0#
Selection.InlineShapes(1).PictureFormat.CropTop = 0#
Selection.InlineShapes(1).PictureFormat.CropBottom = 0#
 




Hi,

The item that you just pasted into Word can be identified...
Code:
With ThisDocument.Inlineshapes(ThisDocument.Inlineshapes.Count)
    With .Fill
        .Visible = msoFalse
        .Solid
        .Transparency = 0#
    End With
    With .Line
        .Weight = 0.75
        .Transparency = 0#
        .Visible = msoFalse
    End With
    With .PictureFormat
        .Brightness = 0.5
        .Contrast = 0.5
        .ColorType = msoPictureAutomatic
        .CropLeft = 0#
        .CropRight = 0#
        .CropTop = 0#
        .CropBottom = 0#
    End With
    .LockAspectRatio = msoTrue
    .Height = 136.65
    .Width = 190.5
End with


Skip,
[sub]
[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue][/sub]
 

Whan I start to record a macro, click on the picture to select it, and then stop the macro, I get:
Code:
ActiveDocument.Shapes(1).Select
Would that work for you?

Have fun.

---- Andy
 
Unfortunately,

do both suggestion generate error messages in VBA.

I am using MS Word 2002 on Windows XP Prof.

Andy. It is intersting that you can select a picture when recording a macro. I tried to do that on my computer again, but VBA would not select a picture when it is recording the macro.

When I run Andy's code. I get the following error message.

The index into the specified collection is out of bounds.

Any suggestions?
 




Change Inlineshapes to shapes and try.

Skip,
[sub]
[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue][/sub]
 
The error seems to be related to shapes the program does not run through till inlineshapes.
 

Acctually, the macro gave me:
Code:
ActiveDocument.Shapes([blue]"Picture 2"[/blue]).Select
but it will accept an index instead of the name of the picture. But I do not know how Word sets the index or name of the Picture in the document. You will have to play with it.

Have fun.

---- Andy
 




???

So it's not a Shape, but an InlineShape?

Did my original code not work?

Skip,
[sub]
[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue][/sub]
 
Skip your code produced another error.

count =0 is something VBA did not like. Any thoughts?

With ThisDocument.Inlineshapes(ThisDocument.Inlineshapes.Count)
 




If ThisDocument.Inlineshapes.Count equals ZERO, then you do not have ANY inline shapes.

Step into your code and use the watch window to see exactly what you do have.

faq707-4594

Skip,
[sub]
[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue][/sub]
 




"But I do not know how Word sets the index or name of the Picture in the document. "

The index is the order in which a particular object has veen added to the document. That is why you can use...
Code:
document.objectcollection(document.objectcollection.count)
to identify the LAST object in the objectcollection, added to the document.

Skip,
[sub]
[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue][/sub]
 
You really need to determine if it is a Shape vs. InlineShape.

Notice Andy's code is refering to a Shape, not an InlineShape. They are very very different.

Skip is, of course, correct. If you want to access the last inline shape, you use the .Count of the inlineShape collection.
Code:
Dim myIn As InlineShape
Set myIn = ActiveDocument.InlineShapes(ActiveDocument.InlineShapes.Count)

With myIn

    etc.
It should work with ThisDocument.

If you do this right after pasting into Word, and that action is by code:
Code:
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    With Selection.InlineShapes(1)
should work.

The naming of Shapes and InlineShapes in Word is arcane...at best.


faq219-2884

Gerry
My paintings and sculpture
 
Thanks for the many suggestions. I will test them and will report back.

Thanks again.
 

This is the code that worked best for me.


With ActiveDocument.InlineShapes(ActiveDocument.InlineShapes.Count)
.Fill.Visible = msoFalse
.Fill.Solid
.Fill.Transparency = 0#
.Line.Weight = 0.75
.Line.Transparency = 0#
.Line.Visible = msoFalse
.LockAspectRatio = msoTrue
.Height = 149.65
.Width = 204.4
.PictureFormat.Brightness = 0.5
.PictureFormat.Contrast = 0.5
.PictureFormat.ColorType = msoPictureAutomatic
.PictureFormat.CropLeft = 0#
.PictureFormat.CropRight = 0#
.PictureFormat.CropTop = 0#
.PictureFormat.CropBottom = 0#
.LockAspectRatio = msoTrue
.Height = 136
.Width = 190
'Selection.InlineShapes(1).Left = 0#
'Selection.InlineShapes(1).Top = 0#
End With
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top