3

我可以从 Powerpoint 将图表导出为 JPG 文件,但无法使用表格执行此操作,据我所知,它仍然是应该能够导出的“形状”。

这是我用来将图表导出为 JPG 的代码的清理版本。

Const imgFilePath as String = "ChartImage.JPG"
Sub ExportChartJPG()
Dim cht as Variant  'this will hold the Chart/Shape object

Set cht = ActivePresentation.Slides(1).Shapes("Chart1").Chart

On Error Resume Next
    Kill imgPath
On Error GoTo 0

cht.Export imgPath, "JPG"

End Sub

我认为这很容易修改,例如:

Sub ExportChartJPG()
Dim cht as Variant  'this will hold the Chart/Shape object

Set cht = ActivePresentation.Slides(1).Shapes("Table1").Table

On Error Resume Next
    Kill imgPath
On Error GoTo 0

cht.Export imgPath, "JPG"

End Sub

但这会引发错误13不匹配。

我也尝试过将尺寸标注cht为 Shape 而不是 Variant,并且 settingcht = ActivePresentation.Slides(1).Shapes("Table1")也未成功。

4

2 回答 2

5

尽管 KazJaw 的解决方案有效,但有点麻烦(复制需要额外的时间来处理,我认为由于没有“等待”足够长的时间来完成复制,剪贴板问题?等等)

http://www.tech-archive.net/pdf/Archive/Office/microsoft.public.office.developer.vba/2006-10/msg00046.pdf

我打开对象浏览器,右键单击并显示隐藏的方法,现在我可以ExportShape.

Sub ExportShapeJPG()
Dim cht as Variant  'this will hold the Chart/Shape object

Set cht = ActivePresentation.Slides(1).Shapes("Table1") '<-- removed .Table and only pass the Shape itself

'Likewise, for charts, omit the .Chart:
' Set cht = ActivePresentation.Slides(1).Shapes("Chart1") 


On Error Resume Next
    Kill imgPath
On Error GoTo 0

cht.Export imgPath, ppShapeFormatJPG  '<-- The export syntax is slightly different using ppShapeFormatJPG instead of "JPG"

End Sub
于 2013-03-21T20:01:03.487 回答
3

我有一个很奇怪的想法。查看第一部分保存图表和第二部分保存表的代码。

Sub ExportinChartAndTable()
Dim imgFilePath As String
    imgFilePath = ActivePresentation.Path & "\chart"

Dim shp As Shape
    Set shp = ActivePresentation.Slides(1).Shapes(1)
Dim shpChart As Chart
    Set shpChart = shp.Chart
'exporting chart
On Error Resume Next
    Kill imgFilePath
On Error GoTo 0
shpChart.Export imgFilePath & "chart.jpg", "JPG"

Stop
Dim chartPart As ChartData
    Set chartPart = shpChart.ChartData

imgFilePath = ActivePresentation.Path & "\dataTable.jpg"

chartPart.Workbook.worksheets("arkusz1").Range("a1:c20").Copy
shpChart.Paste
shpChart.Shapes(1).Width = shp.Width
shpChart.Shapes(1).Height = shp.Height
On Error Resume Next
    Kill imgFilePath
On Error GoTo 0
shpChart.Export imgFilePath, "JPG"


End Sub

您必须想出如何检查表格范围的想法。我希望这CurrentRegion会奏效,但事实并非如此。您可以使用这种可能性来计算表中的行数和列数(这是可能的)。或者也许你有固定的范围,所以这很容易。还有一件事,您必须在调整表格大小时调整尺寸。

编辑由于大卫评论。我保留了上述解决方案,因为可能对其他人有用(请参阅下面的评论)

Sub SolutionSecond()

Dim whereTo As String
    whereTo = ActivePresentation.Path & "\table.jpg"
Dim shp As Shape
Set shp = ActivePresentation.Slides(1).Shapes(1)



Dim chrt As Shape
Set chrt = ActivePresentation.Slides(1).Shapes.AddChart

shp.Copy

'required due to excel opening proces
    chrt.Select
    chrt.Chart.Paste

'set dimensions here
chrt.Chart.Export whereTo, "JPG"

    chrt.Delete
 End Sub

这一个基于相同的逻辑。将表格复制到可以导出(唯一一种形状)的图表中。

于 2013-03-21T17:17:04.117 回答