用文字选择粘贴的图像 [英] Select pasted image in word

查看:127
本文介绍了用文字选择粘贴的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个宏,该宏将图像复制为增强型图元文件到Word中,然后将其调整为特定的宽度.

I'm trying to create a macro that copies a image into word as an Enhanced Metafile and the resizing it to a specific width.

我在Excel中有一个类似的宏,如下所示:

I have a similar macro in Excel that looks like this:

Sub Macro1()

    ActiveSheet.PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False _
        , DisplayAsIcon:=False
    Selection.ShapeRange.Width = 255

End Sub

这只是简单地记录在Excel中,并且像一个超级按钮. 但是,似乎不可能用文字做同样的事情.

This was simply recorded in Excel and works like a charm. However, it seems impossible to do the same thing in word.

如果我不使用录制宏,我将粘贴图片并向左移动Shift +向左箭头以选择图片.但是,一旦我开始录制,此操作将无法进行. 我环顾四周,显然与Inlineshapes有关吗?但是我找不到一段代码,它实际上允许我选择刚粘贴的图片,然后再调整大小.

If I don't use record a macro I'll just paste the picture and shift+left arrow to select the picture. But as soon as I start recording this doesn't work. I've looked around and apparantly it has to do with Inlineshapes? But I can't find a piece of code that actually allows me to select the picture I just pasted in, for me to resize afterwards.

为什么这样做,我该如何解决?

Why is this made this way and how do I fix it?

推荐答案

使用VBA粘贴到Word中时,未选择粘贴的内容,显然在Excel中是不同的.也许是因为Shape链接到工作簿单元格,并且该单元格被选中了?

When you paste in Word using VBA the pasted content is not selected, apparently that's different in Excel. Perhaps because the Shape is linked to a workbook cell and the cell is selected?

在任何情况下,以下代码都会成功拾取粘贴的图像以进行进一步处理.它的作用是计算从文​​档开始到当前选择的InlineShapes数量,再加上一个字符.

In any case, the following code successfully picks up the pasted image for further manipulation. What it does is count the number of InlineShapes from the beginning of the document to the current selection, plus one character.

粘贴操作之后,新的InlineShape将被添加到该范围内的项目数中,因此可以通过其索引号-(lNrIls + 1)进行拾取.

After the paste action the new InlineShape will have been added to the number of items in the range, so it can be picked up by its index number - (lNrIls + 1).

为了完整起见,我还以Shape的形式包含了粘贴的变化形式(这意味着图像具有文字换行格式,等等).基本方法与InlineShape相同.

For the sake of completeness I've also included the variation for pasting as a Shape (meaning the image has text wrap formatting, among other things). The basic approach is the same as for an InlineShape.

注意:默认情况下,粘贴图像时,Word将使用文件/选项/高级"中剪切,复制和粘贴"部分中的设置Insert/paste pictures as.因此,在某些机器上,图像可能会粘贴为InlineShape,而在其他机器上可能会粘贴为Shape.如果要专门内联粘贴或使用文本换行粘贴,请使用PasteSpecial和相应的WdOLEPlacement枚举值wdFloatOverTextwdInLine.

Note: By default when an image is pasted Word will use the setting Insert/paste pictures as from File/Options/Advanced, section "Cut, copy and paste". So on some machines an image might be pasted as an InlineShape and on others as a Shape. If you want to paste specifically inline or with text wrap use PasteSpecial with the corresponding WdOLEPlacement enumeration value of either wdFloatOverText or wdInLine.

Sub PasteAndSelectPicture()
    Dim ils As Word.InlineShape
    Dim shp As Word.Shape
    Dim lNrIls As Long
    Dim lNrShp As Long
    Dim rngDoc As Word.Range
    Dim rngSel As Word.Range

    Set rngDoc = ActiveDocument.content
    Set rngSel = Selection.Range
    rngDoc.End = rngSel.End + 1

    'Get an InlineShape
    lNrIls = rngDoc.InlineShapes.Count
    rngSel.Paste
    Debug.Print rngDoc.InlineShapes.Count, lNrIls
    Set ils = rngDoc.InlineShapes(lNrIls + 1)
    ils.width = 255

'Get a pasted Shape
'    lNrShp = rngDoc.ShapeRange.Count
'    rngSel.PasteAndFormat Type:=wdFormatOriginalFormatting
'    Debug.Print lNrShp, rngDoc.ShapeRange.Count
'    Set shp = rngDoc.ShapeRange(rngDoc.ShapeRange.Count)
'    shp.RelativeHorizontalPosition = wdRelativeHorizontalPositionCharacter
'    shp.RelativeVerticalPosition = wdRelativeVerticalPositionLine
'    shp.Left = 10
'    shp.Top = 10
End Sub

这篇关于用文字选择粘贴的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆