如何选择刚刚通过 VBA 粘贴到 MS Word 的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7636645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:10:26  来源:igfitidea点击:

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

vbams-word

提问by Bill Software Engineer

Just pasted an image to MS Word in VBA using the following

刚刚使用以下命令将图像粘贴到 VBA 中的 MS Word 中

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

采纳答案by Bill Software Engineer

Here is what I used:

这是我使用的:

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

回答by user2521470

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:

我也在做同样的事情。似乎将图像粘贴到word后,它已经被选中了。您可以使用以下简单代码使用所选对象:

Selection.InlineShapes(1).Select

回答by RoberFJR

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.

我找到的解决方案是使用属性“AlternativeText”将粘贴的形状标记为旧形状,因此只要形状不旧,它就必须是新形状。

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.

不是很干净,但在 VBA 中总是一样的。

回答by Aroddo

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

您可以简单地标记光标位置左侧的元素:

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

回答by Banjoe

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:

我从未在 Word 中使用过 VBA,但这里有一个快速的想法。如果您要内联粘贴图像并立即尝试获取参考,则它应该是 InlineShapes 集合中的最后一项。此代码将为您提供可以使用的参考:

thisDocument.InlineShapes(thisDocument.InlineShapes.Count)

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)