【发布时间】: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
【问题讨论】:
-
不要使用“选择”来放置图片