首先需要在EXCEL中引用Microsoft Excel XX.0 Object Library。
然后在模块文件中: Option Explicit
Public ExcelApp As Excel.Application Public ExcelWorkBook As Excel.Workbook Public ExcelWorkSheet As Excel.Worksheet
最后在Form文件中:
'连接至Excel应用程序 Call Excel_Appliaction(ExcelApp) AppActivate "Microsoft Excel" '------------------------------------------------------------------------ '创建一个新的Excel工作簿文档对象 Set ExcelWorkBook = ExcelApp.Workbooks.Add() '创建一个新的Excel表单对象 Set ExcelWorkSheet = ExcelWorkBook.Sheets.Add() ExcelWorkSheet.Name = "VBA" '下面将数据写入到Excel中 ExcelWorkSheet.Cells(1, 2) = "X": ExcelWorkSheet.Cells(1, 3) = "Y": ExcelWorkSheet.Cells(1, 4) = "Z" ExcelWorkSheet.Cells(2, 1) = "MinPoint": ExcelWorkSheet.Cells(3, 1) = "MaxPoint" ExcelWorkSheet.Cells(2, 2) = MinPoint(0): ExcelWorkSheet.Cells(2, 3) = MinPoint(1): ExcelWorkSheet.Cells(2, 4) = MinPoint(2) ExcelWorkSheet.Cells(3, 2) = MaxPoint(0): ExcelWorkSheet.Cells(3, 3) = MaxPoint(1): ExcelWorkSheet.Cells(3, 4) = MaxPoint(2) '删除以前的重名Excel文档 Dim FSO As Object, ExcelFileName As String ExcelFileName = App.Path + "\" + "Drawing_VBA.xls" Set FSO = CreateObject("Scripting.FileSystemObject") If FSO.FileExists(ExcelFileName) = True Then FSO.DeleteFile ExcelFileName End If '下面保存Excel文档 ExcelWorkBook.SaveAs ExcelFileName '释放Excel的对象 Set ExcelWorkSheet = Nothing Set ExcelWorkBook = Nothing Set ExcelApp = Nothing
|