有没有宏或方法可以将图表转换为Word文档内的图像?

huangapple go评论66阅读模式
英文:

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

问题

以下是翻译好的部分:

大家好
我有一个Word文档,共有300页,每一页都有图表和文本。
当我将此文档放置或导入到InDesign中时,这些图表不会显示或放置在模板中!
是否有方法或宏将所有图表转换为图像
向大家问好

我找到了这个宏,它可以复制任何图表,但这个复制品是非常小的图片

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

将图表转换为图像的方法或宏。

英文:

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

答案1

得分: 1

我现在没有InDesign,所以无法测试这部分,但我知道在使用Export方法之前,你可以放大图表然后导出以获得更大、更清晰的图像。类似于这样:

英文:

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

huangapple
  • 本文由 发表于 2023年5月29日 20:01:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定