简单的例子:Option ExplicitPrivate Sub Command1_Click()
Dim oExcelApp As Excel.Application
Dim oWorkbook As Excel.Workbook
Dim oWorksheet As Excel.Worksheet
Dim oCell As Excel.Range
Set oExcelApp = GetExcelApp()
If oExcelApp Is Nothing Then
Exit Sub
End If
oExcelApp.SheetsInNewWorkbook = 1
Set oWorkbook = oExcelApp.Workbooks.Add
Set oWorksheet = oWorkbook.Sheets(1)
If Not oWorksheet Is Nothing Then
Set oCell = oWorksheet.Range("A2")
oCell.Value = "ExcelAuto A2"
Set oCell = oWorksheet.Range("B3")
oCell.Value = "ExcelAuto B3"
Set oCell = oWorksheet.Range("B10")
oCell.Value = "ExcelAuto B10"
Else
MsgBox "Unexpected error", vbExclamation
End If
oWorkbook.SaveAs "c:\test.xls"
End SubPrivate Function GetExcelApp() As Excel.Application
Dim oExcelApp As Excel.Application On Error Resume Next
Set oExcelApp = GetObject(, "Excel.Application")
If oExcelApp Is Nothing Then
Set oExcelApp = New Excel.Application
End If
If oExcelApp Is Nothing Then
MsgBox "You need to install Excel 2000 to run this example", vbInformation
Exit Function
End If
oExcelApp.Visible = True
Set GetExcelApp = oExcelApp
End FunctionPrivate Sub Command2_Click()
Dim oExcelApp As Excel.Application
Dim oWorkbook As Excel.Workbook
Dim oWorksheet As Excel.Worksheet
Dim oCell As Excel.Range
Set oExcelApp = GetExcelApp()
If oExcelApp Is Nothing Then
Exit Sub
End If
Set oWorkbook = oExcelApp.Workbooks.Open("c:\test.xls")
Set oWorksheet = oWorkbook.Sheets(1)
If Not oWorksheet Is Nothing Then
Set oCell = oWorksheet.Range("B10")
MsgBox "Cell value in B10 is " & oCell.Value, vbInformation
Else
MsgBox "Unexpected error", vbExclamation
End If
End Sub
Dim oExcelApp As Excel.Application
Dim oWorkbook As Excel.Workbook
Dim oWorksheet As Excel.Worksheet
Dim oCell As Excel.Range
Set oExcelApp = GetExcelApp()
If oExcelApp Is Nothing Then
Exit Sub
End If
oExcelApp.SheetsInNewWorkbook = 1
Set oWorkbook = oExcelApp.Workbooks.Add
Set oWorksheet = oWorkbook.Sheets(1)
If Not oWorksheet Is Nothing Then
Set oCell = oWorksheet.Range("A2")
oCell.Value = "ExcelAuto A2"
Set oCell = oWorksheet.Range("B3")
oCell.Value = "ExcelAuto B3"
Set oCell = oWorksheet.Range("B10")
oCell.Value = "ExcelAuto B10"
Else
MsgBox "Unexpected error", vbExclamation
End If
oWorkbook.SaveAs "c:\test.xls"
End SubPrivate Function GetExcelApp() As Excel.Application
Dim oExcelApp As Excel.Application On Error Resume Next
Set oExcelApp = GetObject(, "Excel.Application")
If oExcelApp Is Nothing Then
Set oExcelApp = New Excel.Application
End If
If oExcelApp Is Nothing Then
MsgBox "You need to install Excel 2000 to run this example", vbInformation
Exit Function
End If
oExcelApp.Visible = True
Set GetExcelApp = oExcelApp
End FunctionPrivate Sub Command2_Click()
Dim oExcelApp As Excel.Application
Dim oWorkbook As Excel.Workbook
Dim oWorksheet As Excel.Worksheet
Dim oCell As Excel.Range
Set oExcelApp = GetExcelApp()
If oExcelApp Is Nothing Then
Exit Sub
End If
Set oWorkbook = oExcelApp.Workbooks.Open("c:\test.xls")
Set oWorksheet = oWorkbook.Sheets(1)
If Not oWorksheet Is Nothing Then
Set oCell = oWorksheet.Range("B10")
MsgBox "Cell value in B10 is " & oCell.Value, vbInformation
Else
MsgBox "Unexpected error", vbExclamation
End If
End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货