将多个链接的Excel图表粘贴到Word中,返回运行时错误5345 Word无法获取数据

时间:2018-12-13 15:28:17

标签: excel vba ms-word

我正在尝试复制多个Excel图表并将其粘贴到单独页面上的Word文档中,数据类型为链接的OLEObject 但是我遇到了运行时错误。

  

运行时错误'5343':

     

Word无法获取有关   {00020832-0000-0000-C000-000000000046链接。

enter image description here

这是我过去使用过的代码,但实际上,我在此代码中所做的唯一更改是添加了一个外循环,该外循环用于处理活动工作簿中的工作表。外循环不再起作用,这对我来说有点奇怪,因为我看不到有什么不同。

它适用于第一张纸(当前处于活动状态),但是当循环移至下一张纸时失败。图表是否粘贴有链接都没有关系。

以下是完整的代码供您参考:

Sub ExportingToWord_MultipleCharts()

'Declare Word Variables
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Dim SecCnt As Integer

'Declare Excel Variables
Dim ChrtObj As ChartObject
Dim Rng As Range

'Create a new instance of Word
Set WrdApp = New Word.Application
    WrdApp.Visible = True
    WrdApp.Activate

'Create a new word document
Set WrdDoc = WrdApp.Documents.Add

'Loop through each worksheet in the active workbook.
For Each WrkSht In ActiveWorkbook.Worksheets

    'Loop through the charts on the active sheet
    For Each ChrtObj In WrkSht.ChartObjects

        'Copy the chart
        ChrtObj.Chart.ChartArea.Copy

        'Paste the Chart in the Word Document
        With WrdApp.Selection
            .PasteSpecial Link:=True, DataType:=wdPasteOLEObject, Placement:=wdInLine
        End With

        'Add a new page to the document.
        WrdApp.ActiveDocument.Sections.Add

        'Go to the newly created page.
        WrdApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext

    Next ChrtObj

Next WrkSht

End Sub

它在以下行返回错误

'Paste the Chart in the Word Document
With WrdApp.Selection
     .PasteSpecial Link:=True, DataType:=wdPasteOLEObject, Placement:=wdInLine
End With

1 个答案:

答案 0 :(得分:1)

我找到了一种解决方法,但它仍然不能解释错误发生的原因。我要做的是激活循环中的实际工作表

'***ACTIVATE THE WORKSHEET IN ORDER TO REMOVE THE ERROR***
 WrkSht.Activate

无论出于何种原因,这似乎都可以消除该错误。但是,我发现这很奇怪,因为当我从 PowerPoint中导出图表时,不需要激活工作表来进行复制。这是经过调整的代码,我将其称为我添加的部分。

Sub ExportingToWord_MultipleCharts()

    'Declare Word Variables
    Dim WrdApp As Word.Application
    Dim WrdDoc As Word.Document
    Dim SecCnt As Integer

    'Declare Excel Variables
    Dim ChrtObj As ChartObject
    Dim Rng As Range

    'Create a new instance of Word
    Set WrdApp = New Word.Application
        WrdApp.Visible = True
        WrdApp.Activate

    'Create a new word document
    Set WrdDoc = WrdApp.Documents.Add

    'Loop through each worksheet in the active workbook.
    For Each WrkSht In ActiveWorkbook.Worksheets

        '***ACTIVATE THE WORKSHEET IN ORDER TO REMOVE THE ERROR***
         WrkSht.Activate

        'Loop through the charts on the active sheet
        For Each ChrtObj In WrkSht.ChartObjects

            'Copy the chart
            ChrtObj.Chart.ChartArea.Copy

            'Paste the Chart in the Word Document
            With WrdApp.Selection
                .PasteSpecial Link:=False, DataType:=wdPasteOLEObject, Placement:=wdInLine
            End With

            'Add a new page to the document.
            WrdApp.ActiveDocument.Sections.Add

            'Go to the newly created page.
            WrdApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext

        Next ChrtObj

    Next WrkSht

End Sub