Selecting InlineShapes

D

Derek

Hi,

I'm trying to loop through all the inlineshapes in a MS Word 2010
document and convert them to jpg (to make the file size smaller).
However, when I try to select the inlineshape, it is always the first
inlineshape that is selected. It never moves to the next inlineshape.
Also, the macro never finishes...

Thanks,
Derek

Sub PasteAsJPEG()
'
' Cut each picture and paste it back in as a JPEG
'
Dim Pic As InlineShape

For Each Pic In ActiveDocument.InlineShapes
Pic.Select
With Selection
.Cut
.PasteSpecial DataType:=15
End With
Next Pic
End Sub
 
J

Jay Freedman

This is a general problem: When you cut or delete a member of a collection, behind your back VBA redoes the internal index of the collection that it uses to keep track of the For Each iterations. The
recommended way is to use an explicit index, and work from the end of the collection back to the beginning. Try it this way:

Sub PasteAsJPEG()
'
' Cut each picture and paste it back in as a JPEG
'
' Dim Pic As InlineShape
Dim i As Long

For i = ActiveDocument.InlineShapes.Count To 1 Step -1
ActiveDocument.InlineShapes(i).Select
With Selection
.Cut
.PasteSpecial DataType:=15
End With
Next i
End Sub
 

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