VBA 要复制的脚本 Excel 图表到 Word 在更高版本的 Word 中不起作用

VBA Script to Copy Excel Chart to Word not working in later versions of Word

我正在尝试将 Excel 图表复制到 Word。我在 Excel 中有以下脚本,它适用于 Office 2003 for PC 和 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 中工作的方式编写代码?我试图确保所有偏好都相同,但我可能错过了一些可能导致问题的偏好。任何想法将不胜感激,因为这对我来说是个大问题。

你基本上有两个问题:

  1. 为什么这在 Excel 2016
  2. 中不起作用
  3. 如何使此代码版本独立且 OS 独立?

我没有 2016 Excel,也没有 Mac OS,所以我可能无法回答 #1,但我会在 Excel 2010 年和 2013 年可能会帮助您在 2016 年找到解决方案。我可以在下面回答 #2。

Excel 2010/2013 调查结果:

2010 年 Excel,我看到了一些类似的问题,.Execute 实际上并没有替换文本。我观察到 .Execute 正在更改 selection - 本质上是 .Execute selects 找到的文本。 IOW,它不会默认为 replace,它只是 findsselects。为了替换,我需要做:

.Execute Replace:=wdReplaceAll

但是,无论是 PasteAndFormat 还是 ExecuteMso 都成功地替换了新的“选择”。

我在 Excel 2013which is the latest version documented on MSDN 中观察到相同的情况,并且可能与 2016 年的对象模型相同。

NB 我用的是.CopyPicture方法,而不是.Copy方法。使用该方法可能会更好。值得一试。

寻求独立于版本(和 OS 独立代码)

is it possible to write the code in a way that forces it to work in all versions of 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