【问题标题】:How to move the selection to be after the just-pasted image in Word VBA如何将选择移动到 Word VBA 中刚刚粘贴的图像之后
【发布时间】:2017-08-11 14:42:31
【问题描述】:

我编写了一个宏,可以将 Excel 中的图表复制粘贴到打开的 Word 文档中。

当用户运行宏两次时会出现问题。在第二轮中,刚刚粘贴的图像仍然被选中,因此新粘贴通过粘贴当前选择来撤消前一个的工作。

在宏接近尾声时,如何取消选择我刚刚粘贴的图像,以便下一次运行的宏能够正常工作并粘贴到其右侧?

我的示例代码:

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

【问题讨论】:

  • 不要使用“选择”来放置图片

标签: vba excel ms-word


【解决方案1】:

我想我找到了。答案是添加WordApp.Selection.Move这一行

【讨论】:

    【解决方案2】:

    这是并排粘贴的代码

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多