Search code examples
vbams-wordadobe-indesign

Is there a macro or ways to convert a charts to images inside of word document?


Hello everyone I have a word document It has 300 pages and every single page having a chart and text when i make place or import to this document into InDesign These charts do not appear or place in the template! is there way or macro to convert all of the charts into images Greetings to all

I found this macro its make copy of any charts but this copy it's very small pictures

Sub EmbedAllCharts()
    Dim ILS As InlineShape
    Dim Shp As Shape

    For Each ILS In ActiveDocument.InlineShapes
        If ILS.Type = wdInlineShapeChart Then
            ILS.Chart.Export Environ$("temp") & "\chart" & ".png", "PNG"
            ILS.Select
            Selection.InlineShapes.AddPicture FileName:=Environ$("temp") & "\chart" & ".png", _
                LinkToFile:=False, SaveWithDocument:=True
        End If
    Next ILS

    For Each Shp In ActiveDocument.Shapes
        If Shp.Type = msoChart Then
            Shp.Chart.Export Environ$("temp") & "\chart" & ".png", "PNG"
            Shp.Select
            Selection.InlineShapes.AddPicture FileName:=Environ$("temp") & "\chart" & ".png", _
                LinkToFile:=False, SaveWithDocument:=True
            Shp.Delete
        End If
    Next Shp
End Sub

enter image description here
enter image description here

ways or macro to convert charts into images


Solution

  • I don't have InDesign now, so I can't test that part, but I've known that before using the Export method, you can zoom in on the Chart and then export it to get a larger, clearer image. Something like this:

    Sub EportAllChartsInLargeSize()
        Dim ILS As InlineShape
        Dim Shp As Shape
        
        Dim i As Long, ur As Word.UndoRecord
    
        Set ur = Word.Application.UndoRecord
        ur.StartCustomRecord "EportAllChartsInLargeSize"
        
        For Each ILS In ActiveDocument.InlineShapes
            If ILS.Type = wdInlineShapeChart Then
            
                Rem Before using the Export method, you can zoom in on the Chart and then export it to get a larger, clearer image.
                ZoomInlineShape ILS, 2
                
                ILS.Chart.Export Environ$("temp") & "\chart" & i & ".png", "PNG"
                
                'ILS.Range.Document.Undo 'restore the size
                
    '            ILS.Select
    '            Selection.InlineShapes.AddPicture FileName:=Environ$("temp") & "\chart" & ".png", _
                    LinkToFile:=False, SaveWithDocument:=True
                    
                i = i + 1
            End If
        Next ILS
    
        For Each Shp In ActiveDocument.Shapes
            If Shp.Type = msoChart Then
                Rem Before using the Export method, you can zoom in on the Chart and then export it to get a larger, clearer image.
                ZoomShape Shp, 2
                
                Shp.Chart.Export Environ$("temp") & "\chart" & i & ".png", "PNG"
                
                'Shp.Anchor.Document.Undo 'restore the size
                
    '            Shp.Select
    '            Selection.InlineShapes.AddPicture FileName:=Environ$("temp") & "\chart" & ".png", _
    '                LinkToFile:=False, SaveWithDocument:=True
    '            Shp.Delete
    
                i = i + 1
            End If
        Next Shp
        
        ur.EndCustomRecord
        ActiveDocument.Undo 'restore the size
        
    End Sub
    Sub ZoomInlineShape(ByRef ILS As InlineShape, percentage As Single)
        With ILS
            .LockAspectRatio = msoTrue
            .Width = .Width * percentage
    '        .Height = .Height * percentage
        End With
    End Sub
    Sub ZoomShape(ByRef Shp As Shape, percentage As Single)
        With Shp
            .LockAspectRatio = msoTrue
            .Width = .Width * percentage
    '        .Height = .Height * percentage
        End With
    End Sub
    Sub ExportChart()
        Dim ILS As InlineShape
        Dim objChart As Chart 'Object
        Dim strFilePath As String
        Dim strFileName As String
        Dim objInDesign As Object 'InDesign.Application
        Dim objDoc As Object 'InDesign.Document
        
        Rem Before using the Export method, you can zoom in on the Chart and then export it to get a larger, clearer image.
        Set ILS = ActiveDocument.InlineShapes(2) ' This is just for my test, you can adjust to yours
        ZoomInlineShape ILS, 2
        
        ' Get the active chart object in Word
        Set objChart = ActiveDocument.InlineShapes(2).Chart
    
        ' Get the file path and name for the exported chart
        strFilePath = VBA.Environ("userprofile") + "\Documents\" '"C:\\Temp\\"
        strFileName = "chartTest.png"
    
        
        ' Export the chart to the specified file location
        objChart.Export FileName:=strFilePath & strFileName, FilterName:="PNG"
    
        ILS.Range.Document.Undo 'restore the size
        
        Stop
        
        Rem This code below is the answer of YouChat and I don't have InDesign so I can't test. Sorry. Maybe you can try
        ' Open the InDesign application
        Set objInDesign = CreateObject("InDesign.Application")
    
        ' Open the InDesign document where you want to import the chart
        Set objDoc = objInDesign.Open("C:\\Users\\UserName\\Documents\\MyDocument.indd")
    
        ' Insert the exported chart into the InDesign document
        objDoc.Pages(1).Place strFilePath & strFileName
    
        ' Save and close the InDesign document
        objDoc.Save
        objDoc.Close
    
        ' Clean up
        Set objChart = Nothing
        Set objInDesign = Nothing
        Set objDoc = Nothing
    End Sub