首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Word VBA中将所选内容移动到刚刚粘贴的图像之后

如何在Word VBA中将所选内容移动到刚刚粘贴的图像之后
EN

Stack Overflow用户
提问于 2017-08-11 02:32:16
回答 2查看 1.3K关注 0票数 0

我写了一个宏,将Excel中的图表复制到一个开放的Word文档中.

当用户两次运行宏时会出现问题。在第二轮,公正粘贴的图像仍然被选中,所以新的粘贴取消以前的工作,粘贴在当前的选择之上。

在宏的末尾,如何取消选择刚刚粘贴的图像,以便下一次宏运行将正确工作并粘贴到其右侧?

我的示例代码:

代码语言:javascript
复制
Private Sub ActiveChartPasteToWordMacro(ByVal AndShrinkItToo)
    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Dim WordCurrentPlace As Word.Range

    ' Copy the range as a picture
    Call ActiveChart.CopyPicture(Appearance:=xlScreen, Size:=xlScreen, Format:=xlBitmap)

    ' Refer to the current place in the open Word document:
    Set WordApp = GetObject(, "Word.Application")
    Set WordDoc = WordApp.ActiveDocument
    Set WordCurrentPlace = WordApp.Selection.Range

    ' Paste the Excel chart into Word
    Call WordCurrentPlace.Paste

    ' The user usually calls the macro with some rescaling factor, to fit three charts in a row in Word:
    If AndShrinkItToo Then
         Call WordApp.Selection.Expand(wdParagraph)
         WordApp.Selection.InlineShapes(WordApp.Selection.InlineShapes.count).ScaleWidth = 67
         WordApp.Selection.InlineShapes(WordApp.Selection.InlineShapes.count).ScaleHeight = 67
    End If

    ' ???? HOW DO I NOW SELECT THE SPACE AFTER THE IMAGE FOR THE NEXT RUN OF THE MACRO ????

    ' Clean up
    Set WordCurrentPlace = Nothing
    Set WordDoc = Nothing
    Set WordApp = Nothing
End Sub
EN

Stack Overflow用户

发布于 2017-08-11 06:42:31

下面是并排粘贴的代码

更正图片的路径,然后使用F8键单步执行代码。

代码语言:javascript
复制
Sub pastePictures()

    Dim aaa As Object
    Dim bbb As Range

    Dim pic As String
    pic = "C:\myPicture.png"           ' put your picture's path here


    Set aaa = Selection.InlineShapes.AddPicture( _
        FileName:=pic, _
        LinkToFile:=False, _
        SaveWithDocument:=True)

'    aaa.ScaleHeight = 10
'    aaa.ScaleWidth = 10

    Set bbb = aaa.Range
    bbb.Select                         ' for debug only

    bbb.Collapse wdCollapseEnd         ' this collapses the range taken up by the picture to zero
    bbb.Select                         ' for debug only

    Set aaa = Selection.InlineShapes.AddPicture( _
        FileName:=pic, _
        LinkToFile:=False, _
        SaveWithDocument:=True)

'    aaa.ScaleHeight = 10
'    aaa.ScaleWidth = 10


End Sub
票数 -1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45626051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档