代码之家  ›  专栏  ›  技术社区  ›  lebelinoz

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

  •  0
  • lebelinoz  · 技术社区  · 9 年前

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

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

    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
    
    2 回复  |  直到 9 年前
        1
  •  -1
  •   lebelinoz    9 年前

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

        2
  •  -1
  •   jsotola    9 年前

    更正图片的路径,然后使用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
    
    推荐文章