Exporting a Chart to a Word Document

Cet exemple prend un graphique nommé « Chart 1 » de la feuille 1 et l’exporte en tant que fichier .gif. Ensuite, il insère le fichier .gif dans un document Word existant nommé « Rapport de graphique » à l’emplacement nommé « ChartReport ».

Exemple de code fourni par : Dennis Wallentin, VSTO & .NET & Excel

Sub Export_Chart_Word()

    'Name of an existing Word document, and the name the chart will have when exported.
    Const stWordDocument As String = "Chart Report.docx"
    Const stChartName As String = "ChartReport.gif"
    
    'Word objects.
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim wdbmRange As Word.Range
    
    'Excel objects.
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    Dim ChartObj As ChartObject
    
    'Initialize the Excel objects.
    Set wbBook = ThisWorkbook
    Set wsSheet = wbBook.Worksheets("Sheet1")
    Set ChartObj = wsSheet.ChartObjects("Chart 1")
    
    'Turn off screen updating.
    Application.ScreenUpdating = False
    
    'Export the chart to the current directory, using the specified name, and save the chart as a .gif
    ChartObj.Chart.Export _
                   Filename:=wbBook.Path & "\" & stChartName, _
                   FilterName:="GIF"
    
    'Initialize the Word objects to the existing Word document and bookmark.
    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\" & stWordDocument)
    Set wdbmRange = wdDoc.Bookmarks("ChartReport").Range
    
    'If there is already an inline shape, that means the macro has been run before - clean up any artifacts.
    On Error Resume Next
    With wdDoc.InlineShapes(1)
        .Select
        .Delete
    End With
    On Error GoTo 0
    
    'Add the .gif file to the document at the bookmarked location,
    'and ensure that it is saved inside the Word doc.
    With wdbmRange
        .Select
        .InlineShapes.AddPicture _
        Filename:=wbBook.Path & "\" & stChartName, _
        LinkToFile:=False, _
        savewithdocument:=True
    End With
    
    'Save and close the Word document.
    With wdDoc
        .Save
        .Close
    End With
    
    'Quit Word.
    wdApp.Quit
    
    'Clear the variables.
    Set wdbmRange = Nothing
    Set wdDoc = Nothing
    Set wdApp = Nothing
    
    'Delete the temporary .gif file.
    On Error Resume Next
    Kill wbBook.Path & "\" & stChartName
    On Error GoTo 0
    
    MsgBox "Chart exported successfully to " & stWordDocument

End Sub

À propos du collaborateur

Dennis Wallentin est l’auteur de VSTO & .NET & Excel, un blog consacré aux solutions .NET Framework pour Excel et Excel Services. Dennis développe des solutions Excel depuis plus de 20 ans et a également co-écrit « Professional Excel Development: The Definitive Guide to Developing Applications Using Microsoft Excel, VBA and .NET (2nd Edition) ».

Assistance et commentaires

Avez-vous des questions ou des commentaires sur Office VBA ou sur cette documentation ? Consultez la rubrique concernant l’assistance pour Office VBA et l’envoi de commentaires afin d’obtenir des instructions pour recevoir une assistance et envoyer vos commentaires.