excelvba

Excel VBA Macro to Export Charts to Word


I want to export all charts, as pictures, from an Excel file to a Word document, one following the previous one. However the pictures are kept pasted on top of the previous one so I only have the last picture in the Word document. I would also need to have the pictures centered through the Excel macro, if feasible.

Sub ExportChartsToWord()
    ' Declare variables
    Dim WdApp As Object
    Dim WdDoc As Object
    Dim Ws As Worksheet
    Dim chrt As ChartObject
    Dim chrtName As String
    Dim i As Integer

    ' Initialize Word application
    Set WdApp = CreateObject("Word.Application")
    WdApp.Visible = True
    Set WdDoc = WdApp.Documents.Add
    
    File = "E:\Documents - Misc\Charts to Word.docx"
    'Word session creation
   
    'word will be closed while running
   ' WordApp.Visible = False
    'open the .doc file
    Set WdDoc = WdApp.Documents.Open(File)
 
    'Loop through each worksheet
    For Each Ws In ThisWorkbook.Worksheets
        
    'Loop through each chart in the worksheet
        For Each chrt In Ws.ChartObjects
            ' Copy the chart
            chrt.Chart.CopyPicture Appearance:=xlScreen, Format:=xlPicture
 
            ' Paste the chart into Word
            WdDoc.Content.InsertAfter ""
            'Selection.TypeParagraph
            'Selection.MoveDown Unit:=wdLine, Count:=3
            WdDoc.Content.PasteAndFormat (wdFormatOriginalFormatting)
            WdDoc.Content.ParagraphFormat.Alignment = wdAlignParagraphCenter
            WdDoc.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
            'WdDoc.Content.Alignment = wdAlignCenter
            'Selection.TypeParagraph

            'Add a new page after each chart
            WdDoc.Content.InsertParagraphAfter
            WdDoc.Content.ParagraphFormat.SpaceAfter = 6
            WdDoc.Content.ParagraphFormat.SpaceBeforeAuto = True
            ' Loop through all inline shapes (images) in the document

           'wdDoc.Content.MoveDown
           'wdDoc.Selection.MoveDown Unit:=wdLine, Count:=2
           'wdDoc.Content.TypeParagraph
           'wdDoc.Selection.Find.Execute Replace:=2
           'wdDoc.Selection.Expand wdParagraph
           'wdDoc.Selection.InlineShapes(1).Select
           'wdDoc.Selection.InsertParagraphAfter
        Next chrt
    Next Ws
 
    ' Save the Word document
    WdDoc.SaveAs2 "E:\Documents - Misc\Charts to Word.docx"
 
    ' Clean up
    'wdDoc.Close
    'wdApp.Quit
    'Set wdDoc = Nothing
    'Set wdApp = Nothing
 
    MsgBox "Charts exported successfully!"
End Sub

Excel file: enter image description here Word Document: enter image description here


Solution

  • The script demonstrates exporting all Excel charts from the ActiveSheet to a new Word document, with each chart placed on a separate page.

    Sub ExportChartsToWord()
        Dim wdApp As Object 'Word.Application
        Dim wdDoc As Object 'Word.Document
        Dim chartObj As ChartObject
        Dim chartCount As Integer
        Dim currentPath As String
        Dim fileName As String
        
        'Get current workbook path
        currentPath = ThisWorkbook.Path
        If currentPath = "" Then
            MsgBox "Please save the workbook first", vbExclamation
            Exit Sub
        End If
        
        fileName = currentPath & "\Charts.docx"
        
        'Check if any charts exist
        If ActiveSheet.ChartObjects.Count = 0 Then
            MsgBox "No charts found in active sheet!", vbExclamation
            Exit Sub
        End If
        
        'Initialize Word application
        On Error Resume Next
        Set wdApp = GetObject(, "Word.Application")
        If wdApp Is Nothing Then
            Set wdApp = CreateObject("Word.Application")
        End If
        On Error GoTo 0
        
        'Create new document
        Set wdDoc = wdApp.Documents.Add
        wdApp.Visible = True
        
        'Process all charts
        chartCount = 0
        For Each chartObj In ActiveSheet.ChartObjects
            chartCount = chartCount + 1
            
            'Copy chart as picture
            chartObj.Chart.CopyPicture Appearance:=xlScreen, Format:=xlPicture
            
            'Paste to Word and center align
            wdApp.Selection.Paste
            wdApp.Selection.ParagraphFormat.Alignment = 1 'Center alignment
            
            'Add page break if not last chart
            If chartCount < ActiveSheet.ChartObjects.Count Then
                wdApp.Selection.InsertBreak Type:=7 'wdPageBreak
            End If
        Next chartObj
        
        'Save Word document
        On Error Resume Next
        wdDoc.SaveAs2 fileName
        If Err.Number <> 0 Then
            MsgBox "Error saving file: " & Err.Description, vbExclamation
        Else
            MsgBox "Successfully exported " & chartCount & " charts to:" & vbCrLf & fileName, vbInformation
        End If
        On Error GoTo 0
        
        'Clean up
        wdDoc.Close
        Set wdDoc = Nothing
        Set wdApp = Nothing
    End Sub
    
    

    enter image description here