我写了一个宏,将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发布于 2017-08-11 06:42:31
下面是并排粘贴的代码
更正图片的路径,然后使用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 Subhttps://stackoverflow.com/questions/45626051
复制相似问题