Reputation: 7782
Just pasted an image to MS Word in VBA using the following
wordApp.Selection.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine
My thinking is to move one char left and then select next object, but I don't know how to do this.
EDIT:
Well here are some encoraging development, using the following line, I was able to select the paragraph which include the image, but I can't manipulate it because it's selecting a range. Do anyone know how I can pin down the image inside the selection?
wordApp.Selection.Expand wdParagraph
Upvotes: 5
Views: 21277
Reputation: 93
You can simply mark the element left from your cursor position:
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Upvotes: 0
Reputation: 1
The solution i found is to use the property "AlternativeText" to mark the pasted shapes as old, so whenever a shape is not old it has to be the New one.
I use the following:
Dim pShape as InLineShape' The shape i'm looking for
Dim iShape as InlineShape
For Each iShape In ActiveDocument.InlineShapes
If iShape.AlternativeText = "" Then
Set pShape = iShape
pShape.AlternativeText = "old"
Exit For
End If
Next
Not very clean, but in VBA is always the same.
Upvotes: 0
Reputation: 21
I was also doing the same. It seems that after pasting an image into word, its already selected. You can just use the selected object with the simple code below:
Selection.InlineShapes(1).Select
Upvotes: 1
Reputation: 7782
Here is what I used:
wordApp.Selection.Find.Execute replace:=2
wordApp.Selection.Expand wdParagraph
wordApp.Selection.InlineShapes(1).Select
Upvotes: 1
Reputation: 1768
I've never used VBA in Word but here's a quick thought. If you're pasting the image inline and immediately trying to get a reference it should be the last item in the InlineShapes collection. This code will give you a reference you can use:
thisDocument.InlineShapes(thisDocument.InlineShapes.Count)
For example, to set the width of last pasted image you would use the following:
thisDocument.InlineShapes(thisDocument.InlineShapes.Count).Width = 100
To store the shape in a variable:
Dim pastedImage As InlineShape
Set pastedImage = ThisDocument.InlineShapes(ThisDocument.InlineShapes.Count)
Upvotes: 0