Option ExplicitDim oWord As Word.Application Dim oDoc As Word.Document Dim oShape As Word.InlineShape Dim oChart As ObjectPrivate Sub Command1_Click() On Error GoTo ErrMsg Dim i As Integer Dim j As Integer
Me.MousePointer = 11 Set oWord = New Word.Application Set oDoc = oWord.Documents.Add("c:\test.doc", False) '在Word中插入Graph图表 Set oShape = oDoc.InlineShapes.AddOLEObject(ClassType:="MSGraph.Chart.8", FileName:="", LinkToFile:=False, DisplayAsIcon:=False) Set oChart = oShape.OLEFormat.Object
'填加随机数 For i = 2 To 9 '显示列标题 oChart.Application.DataSheet.Cells(1, i).Value = "第" & i - Asc("A") + 1 & "列" For j = 2 To 4 '添加数据 oChart.Application.DataSheet.Cells(j, i).Value = Rnd() Next Next '显示行标题 For j = 1 To 3 oChart.Application.DataSheet.Cells(j, 1).Value = "第" & j & "行" Next j
With oChart.Application.Chart.Axes(xlCategory) .HasTitle = True .AxisTitle.Text = "这是横坐标轴上标题的位置" End With
With oChart.Application.Chart.Axes(xlValue) .HasTitle = True .AxisTitle.Text = "这是纵坐标轴上标题的位置" End With
With oChart.Application.Chart '指定图表的标题 .HasTitle = True .ChartTitle.Text = "这是图表标题的置" End With
'指定图表形状 oChart.Application.Chart.ChartType = xl3DPieExploded oChart.Application.Update oChart.Application.Quit '... If desired, you can proceed from here using the Microsoft Graph 'Object model on the oChart object to make additional changes to the 'chart. oShape.Width = oDoc.PageSetup.PageWidth - (oDoc.PageSetup.LeftMargin + oDoc.PageSetup.RightMargin) 'oWord.InchesToPoints(6.25) oShape.Height = oShape.Width * 2 / 3 'oWord.InchesToPoints(3.57) oDoc.SaveAs "c:\test3.doc"
oWord.Application.Visible = True
GoTo ExitLab ErrMsg: MsgBox Err.Description ExitLab: If Not (oWord Is Nothing) Then ' oWord.Quit'退出word End If Set oWord = Nothing '把控制交回Word Set oDoc = Nothing Set oShape = Nothing Set oChart = Nothing Me.MousePointer = 0 End Sub
Dim oDoc As Word.Document
Dim oShape As Word.InlineShape
Dim oChart As ObjectPrivate Sub Command1_Click()
On Error GoTo ErrMsg
Dim i As Integer
Dim j As Integer
Me.MousePointer = 11
Set oWord = New Word.Application
Set oDoc = oWord.Documents.Add("c:\test.doc", False)
'在Word中插入Graph图表
Set oShape = oDoc.InlineShapes.AddOLEObject(ClassType:="MSGraph.Chart.8", FileName:="", LinkToFile:=False, DisplayAsIcon:=False)
Set oChart = oShape.OLEFormat.Object
'填加随机数
For i = 2 To 9
'显示列标题
oChart.Application.DataSheet.Cells(1, i).Value = "第" & i - Asc("A") + 1 & "列"
For j = 2 To 4
'添加数据
oChart.Application.DataSheet.Cells(j, i).Value = Rnd()
Next
Next
'显示行标题
For j = 1 To 3
oChart.Application.DataSheet.Cells(j, 1).Value = "第" & j & "行"
Next j
With oChart.Application.Chart.Axes(xlCategory)
.HasTitle = True
.AxisTitle.Text = "这是横坐标轴上标题的位置"
End With
With oChart.Application.Chart.Axes(xlValue)
.HasTitle = True
.AxisTitle.Text = "这是纵坐标轴上标题的位置"
End With
With oChart.Application.Chart
'指定图表的标题
.HasTitle = True
.ChartTitle.Text = "这是图表标题的置"
End With
'指定图表形状
oChart.Application.Chart.ChartType = xl3DPieExploded
oChart.Application.Update
oChart.Application.Quit
'... If desired, you can proceed from here using the Microsoft Graph
'Object model on the oChart object to make additional changes to the
'chart.
oShape.Width = oDoc.PageSetup.PageWidth - (oDoc.PageSetup.LeftMargin + oDoc.PageSetup.RightMargin) 'oWord.InchesToPoints(6.25)
oShape.Height = oShape.Width * 2 / 3 'oWord.InchesToPoints(3.57) oDoc.SaveAs "c:\test3.doc"
oWord.Application.Visible = True
GoTo ExitLab
ErrMsg:
MsgBox Err.Description
ExitLab:
If Not (oWord Is Nothing) Then
' oWord.Quit'退出word
End If
Set oWord = Nothing '把控制交回Word
Set oDoc = Nothing
Set oShape = Nothing
Set oChart = Nothing
Me.MousePointer = 0
End Sub