开发者

How do you select an image that was just pasted to MS Word via VBA

开发者 https://www.devze.com 2023-04-10 22:18 出处:网络
Just pasted an image to MS Word in VBA using the following wordApp.Selection.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine

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 c开发者_运维知识库an pin down the image inside the selection?

wordApp.Selection.Expand wdParagraph


Here is what I used:

wordApp.Selection.Find.Execute replace:=2
wordApp.Selection.Expand wdParagraph
wordApp.Selection.InlineShapes(1).Select


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


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)


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.


You can simply mark the element left from your cursor position:

Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号