# 'Sheets' 方法 (_'Global'物件)失败

Posted by 有梦你会红 on July 28, 1999 at 17:53:13:

请问一下当我执行下面的程式码....
我想利用EXCEL做图表输出...
第一次没问题...不过再执行第二次
就会出现 'Sheets' 方法 (_'Global'物件)失败...的讯息..
我真的都搞不好...请各位前辈帮我看一下...感激不尽...

'放1个CommandBox 和 1个OLE控制项
'专案→设定引用项目 ....中引用Excel的Object Library
'===============程式码=====================
Option Explicit
Dim Excel_App As Excel.Application
Dim Excel_Sheet As Object
Dim Excel_Chart As Object
Private Sub Command1_Click()
'宣告设定使用EXCEL
Set Excel_App = CreateObject("Excel.Application")
Excel_App.Visible = True '显示EXCEL
' 判断EXCEL的版本
Excel_App.Workbooks.Add (1)
Excel_App.ActiveSheet.Name = "报表"
If Val(Excel_App.Application.Version) >= 8 Then
Set Excel_Sheet = Excel_App.ActiveSheet
Else
Set Excel_Sheet = Excel_App
End If
'填入报表资料
With Excel_App
.Cells(1, 1) = "111"
.Cells(2, 1) = "323"
.Cells(3, 1) = "132"
.Cells(4, 1) = "165"
.Cells(5, 1) = "154"
.Cells(6, 1) = "145"
.Cells(7, 1) = "144"
.Cells(8, 1) = "155"
.Cells(9, 1) = "124"
.Cells(10, 1) = "231"
.Cells(1, 2) = "123"
.Cells(2, 2) = "621"
.Cells(3, 2) = "881"
.Cells(4, 2) = "411"
.Cells(5, 2) = "541"
.Cells(6, 2) = "151"
.Cells(7, 2) = "161"
.Cells(8, 2) = "451"
.Cells(9, 2) = "441"
.Cells(10, 2) = "611"
End With
Excel_App.Charts.Add
'sheet name
Excel_App.ActiveChart.Name = "图表"
If Val(Excel_App.Application.Version) >= 8 Then
Set Excel_Chart = Excel_App.ActiveChart
Else
Set Excel_Chart = Excel_App
End If
Excel_Chart.ChartType = xlLine '使用折线图
'设定图表资料为sheet的A1到B10
Excel_Chart.SetSourceData Source:=Sheets("报表").Range("A1:B10"), PlotBy:= _
xlColumns
'开启另一工作页放图表
Excel_Chart.Location Where:=xlLocationAsNewSheet
'储存
Excel_App.ActiveWorkbook.Close True, FileName:=App.Path & "\Chart.xls"
Excel_App.Quit '关闭excel
'将刚刚储存好的报表填入
Set Excel_Sheet = Nothing
Set Excel_Chart = Nothing
Set Excel_App = Nothing
OLE1.CreateEmbed App.Path & "\chart.xls"
End Sub

Follow Ups: