Article ID: 165872
Article Last Modified on 8/17/2005
Sub ConvertGraphToExcel()
' Used for error trapping.
On Error Resume Next
' Clear the error object.
Err.Clear
' Holds a reference to an object.
Dim oGraph As Object
' Check whether the selection is a Graph 8 object.
With ActiveWindow.Selection.ShapeRange(1).OLEFormat
If Err.Number <> 0 Then
' A run-time error is generated if the selection is not a chart.
' This code exits the macro if the selection is not a chart.
MsgBox "Select one graph and run the macro again.", vbExclamation
' Stop the macro.
End
Else
If .ProgID <> "MSGraph.Chart.8" Then
' A run-time error is generated if the selection is not a
' chart.
' This code exits the macro if the selection is not a chart.
MsgBox "Select one graph and run the macro again.", _
vbExclamation
' Stop the macro.
End
End If
End If
End With
' Reference the Graph object.
Set oGraph = ActiveWindow.Selection.ShapeRange(1).OLEFormat.Object
' Call the CreateExcelChart procedure and pass a reference to the
' Graph 8 object.
CreateExcelChart oGraph
End Sub
Sub CreateExcelChart(oChart As Object)
On Error Resume Next
Dim oExcel As Object
Dim i As Long, j As Long
' Clear the Err object.
Err.Clear
' Reference the Microsoft Excel object model.
Set oExcel = GetObject(, "Excel.Application.8")
If Err.Number <> 0 Then
' Clear the Err object.
Err.Clear
Set oExcel = CreateObject("Excel.Application.8")
' Check whether Microsoft Excel can be started.
If Err.Number <> 0 Then
MsgBox "Unable to start Excel 97. Try starting Excel" _
& " and run the macro again.", vbExclamation
' Stop the macro.
End
End If
' Make Microsoft Excel visible.
oExcel.Visible = True
End If
' Create a new workbook.
oExcel.Workbooks.Add
' Transfer the data in the Graph datasheet to
For i = 1 To 4
For j = 1 To 5
oExcel.Worksheets(1).Cells(j, i).Value = _
oChart.Application.DataSheet.Cells(i, j).Value
Next j
Next i
'Add a chart to the Excel Workbook.
oExcel.Charts.Add
'Set up the chart.
With oExcel.ActiveChart
.ChartType = oChart.ChartType
.Elevation = oChart.Elevation
.Perspective = oChart.Perspective
.Rotation = oChart.Rotation
.RightAngleAxes = oChart.RightAngleAxes
.AutoScaling = oChart.AutoScaling
' Specify the data source.
.SetSourceData Source:=oExcel.Sheets("Sheet1").Range("A1:D5")
' Place the chart on worksheet 1.
.Location Where:=xlLocationAsObject, Name:="Sheet1"
End With
End Sub
NOTE: To use this code with Graph objects that are not the default
chart, modify the macro.
176476 OFF: Office Assistant Not Answering Visual Basic Questions
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: 8.00 ppt8 vba vbe ppt97 xlvbainfo OFF2000 OFF97
Keywords: kbinfo kbinterop kbprogramming kbcode KB165872