【问题标题】:VBA Script to Copy Excel Chart to Word not working in later versions of Word将 Excel 图表复制到 Word 的 VBA 脚本在更高版本的 Word 中不起作用
【发布时间】:2016-08-05 02:55:08
【问题描述】:

我正在尝试将 Excel 图表复制到 Word。我在 Excel 中有以下脚本,它适用于 PC 版 Office 2003 和 Office Mac 2011。在更高版本的 Office (2016) 中,图表在粘贴时不会调整大小,并且它搜索的标记不会被图表替换为它在早期版本中。这是有效但不适用于更高版本的 Office 的脚本。任何帮助将不胜感激。

ActiveChart.ChartArea.Select
ActiveChart.ChartArea.Copy
Set wrd = CreateObject("Word.Application")
wrd.Documents(DocumentName).Activate
        wrd.Selection.Find.ClearFormatting
        With wrd.Selection.Find
            .Text = "insert" & ChartName 'This is the token it is looking for in the Word document and is where the chart should be inserted. 
            .Replacement.Text = ""
            .Forward = True
            .Wrap = 1 'wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute
            If .Found = True Then
                wrd.Selection.PasteAndFormat Type:=13  'wdChartPicture
            End If

在早期版本的 Office 中,此脚本通过查找标记并将其粘贴来获取活动图表并将其复制并粘贴到 Word 中。标记被图表替换并且图表被调整大小。在较新的版本中,标记保留在图表的底部,并且图表没有调整大小。

如果无法弄清楚为什么它不起作用,是否可以编写代码以强制它在所有版本的 Office 中工作?我试图确保所有偏好都相同,但我可能错过了一些可能导致问题的偏好。任何想法都将不胜感激,因为这对我来说是一个相当大的问题。

【问题讨论】:

  • 单步执行此代码时,是否满足.Found = True 条件?
  • 如果是这样,我会查看 ExecuteMSO 方法(here 是一个类似的问题,将单元格范围从 Excel 复制到 Word),这里是 a few more,主要是从 Excel 到 PowerPoint,但是大体思路是一样的,你只需要确定调用哪个动词,那应该是最可靠的。
  • .found 条件确实为真。我尝试用 wrd.CommandBars.ExecuteMso “PasteasPicture” 替换行 wrd.selection.pasteandformat Type:=13 并收到一条错误消息,提示“对象不支持此属性或方法”
  • 注意,我正在使用 Office 2011 在我的 Mac 上对此进行测试。一旦我知道我的更改在此版本上有效,我将在较新的版本上进行测试。
  • 我知道这应该适用于 Excel 2010+,但我不了解 Mac。如果 ExecuteMso 不起作用,我会感到惊讶,再说一遍,它是 Mac,它们不必要地限制了 vba 中的一些东西......

标签: excel vba charts ms-word copy-paste


【解决方案1】:

你基本上有两个问题:

  1. 为什么这在 Excel 2016 中不起作用
  2. 如何使此代码独立于版本和操作系统?

我没有 2016 Excel,也没有 Mac OS,所以我可能无法回答 #1,但我将分享我在 Excel 2010 和 2013 中的一些发现,以防万一帮助您在 2016 年找到解决方案. 我可以回答下面的#2。

Excel 2010/2013 调查结果:

在 2010 Excel 中,我能够看到一些类似的问题,.Execute 实际上并没有替换文本。我观察到.Execute 正在更改选择——本质上是.Execute选择找到的文本。 IOW,它不默认为 replace,它只是 findsselects。为了替换,我需要这样做:

.Execute Replace:=wdReplaceAll

但是,两者都粘贴(PasteAndFormatExecuteMso 都成功替换了新的“选择”。

我在 Excel 2013 中观察到相同的情况which is the latest version documented on MSDN 和大概与 2016 年相同的对象模型。

NB 我使用了.CopyPicture 方法而不是.Copy 方法。使用这种方法可能会更好。值得一试。

对独立于版本(和独立于操作系统的代码)的追求

是否可以以强制其在所有版本的 Office 中工作的方式编写代码?

是的,这需要确定每个版本的工作原理,并使用称为Conditional Compilation 的技术。通常,此技术用于适应对象模型中可能引发编译错误的更改。

例如,假设 这个 代码块在 2003 年工作:

        .Execute
        If .Found = True Then
            wrd.Selection.PasteAndFormat Type:=13  'wdChartPicture
        End If

而这段代码在 2010 年和 2013 年都有效:

        .Execute Replace:=wdReplaceAll
        If .Found = True Then
            wrd.CommandBars.ExecuteMSO "PasteAsPicture"
        End If

还有一些其他代码在 2016+ 年有效(这不是真正的代码,但请耐心等待):

       .Execute Replace:=wdReplaceAll
       If .Found = True Then
           wrd.SOMETHING_FOR_2016
       End If

那么你会这样做:

#If Mac Then
    Debug.Print "Mac"
    .Execute
    If .Found = True Then
        wrd.SOMETHINGFORMAC 'pseudo-code
    End If
#Else
    Debug.Print "Windows"
    Select Case CLng(Application.Version)
        Case 11 'Excel 2003
            Debug.Print 2003
            .Execute
            If .Found = True Then
                wrd.Selection.PasteAndFormat Type:=13  'wdChartPicture
            End If
        Case 14 'Excel 2010
            Debug.Print 2010
            .Execute Replace:=wdReplaceAll
            If .Found = True Then
                wrd.CommandBars.ExecuteMSO "PasteAsPicture"
            End If
        Case 15 'Excel 2013
            Debug.Print 2013
            .Execute Replace:=wdReplaceAll
            If .Found = True Then
                wrd.CommandBars.ExecuteMSO "PasteAsPicture"
            End If
        Case Is > 15 'Excel 2016+
            Debug.Print "2016+"
           .Execute Replace:=wdReplaceAll
           If .Found = True Then
               wrd.SOMETHING_FOR_2016
           End If
        Case Else
            MsgBox "Other versions would require add'l logic..."
    End Select
#End If

【讨论】:

  • 非常感谢。我会试试这个,但直到下周末我才能这样做。我会让你知道会发生什么。
  • 与此同时,我确实在 Mac 上尝试过。我替换了行 If .Found = True Then wrd.Selection.PasteAndFormat Type:=13 'wdChartPicture End If 用以下 If .Found = True Then .Execute Replace:=wdReplaceAll End If 代码执行但什么也没发生,即文本没有被图表替换。我不是专业程序员,所以可能我误解了说明。
  • 我也尝试了copypicture方法,但它说它不存在。该脚本位于试图复制所选图表的 Excel 文件中。复制方法没有问题。
  • .Execute Replace:=wdReplaceAll 不会用图表替换文本,它应该替换为.Replacement.Text(即""),并且您需要另一个命令来粘贴图表。
  • 对于CopyPicture,使用ActiveChart.CopyPicture 而不是ActiveChart.ChartArea.CopyPicture
猜你喜欢
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 2016-11-22
  • 2023-02-08
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
相关资源
最近更新 更多