With myexcel
ActiveWorkbook.SaveAs FileName:=App.Path & ".\导出维修单列表_" & Format(DLDT1, "YYYYMMDD") & "_" & Hour(Time) & Minute(Time) & Second(Time) & ".xls" 这里提示“”实时错误 91 对象变量或 With 块变量未设置
End With另外:Public myexcel As New Excel.Application
Public mybook As New Excel.Workbook
Public mysheet As New Excel.WorksheetPublic Sub OpenExcel()
Set myexcel = CreateObject("Excel.Application")
Set mybook = myexcel.Workbooks.Add
Set mysheet = mybook.Worksheets(1)
End SubPublic Sub CloseExcel()
Set mysheet = Nothing
mybook.Close
Set mybook = Nothing
Set myexcel = Nothing
myexcel.Quit
End Sub
ActiveWorkbook.SaveAs FileName:=App.Path & ".\导出维修单列表_" & Format(DLDT1, "YYYYMMDD") & "_" & Hour(Time) & Minute(Time) & Second(Time) & ".xls" 这里提示“”实时错误 91 对象变量或 With 块变量未设置
End With另外:Public myexcel As New Excel.Application
Public mybook As New Excel.Workbook
Public mysheet As New Excel.WorksheetPublic Sub OpenExcel()
Set myexcel = CreateObject("Excel.Application")
Set mybook = myexcel.Workbooks.Add
Set mysheet = mybook.Worksheets(1)
End SubPublic Sub CloseExcel()
Set mysheet = Nothing
mybook.Close
Set mybook = Nothing
Set myexcel = Nothing
myexcel.Quit
End Sub
关闭EXCEL应该类似以下这样:
excel_app.Quit
Set excel_app = Nothing给你段我以前写的程序,你对照看看吧:
VB导出EXCEL
Private Sub Command1_Click()
'On Error GoTo myErr
Dim excel_app As Object
Dim Filename1 As String, FilePath As String
'设置EXCEL文件名
Filename1 = "temp"
'建立 Excel 应用程序
Set excel_app = CreateObject("Excel.Application")
'显示Excel应用程序
excel_app.Visible = False
'添加新工作簿:
excel_app.WorkBooks.Add
'检测Excel版本
If Val(excel_app.Application.Version) >= 12 Then
FilePath = Filename1 & ".xlsx"
Else
FilePath = Filename1 & ".xls"
End If
'设置一个鼠标形状
Screen.MousePointer = vbHourglass
'设置第1个工作表为活动工作表:
excel_app.Sheets("sheet1").Select
'设置指定列的宽度(单位:字符个数)
excel_app.ActiveSheet.Columns(1).ColumnWidth = 4
excel_app.ActiveSheet.Columns(2).ColumnWidth = 6
'初始化列对齐方式
Dim iiCol As Integer
For iiCol = 1 To 2
With excel_app.ActiveSheet
'4右对齐,3居中
.Columns(iiCol).HorizontalAlignment = 4
End With
Next iiCol Dim a(29, 1) As Integer '你的数组
Dim i As Integer, j As Integer
For i = 0 To 29 '初始化你的数据
For j = 0 To 1
a(i, j) = i + 1
Next j
Next i '写入EXCEL
For i = 1 To 30
For j = 1 To 2
excel_app.cells(i, j) = a(i - 1, j - 1)
Next j
DoEvents
Next i
'工作表另存为:
If Not excel_app.ActiveWorkBook.Saved Then
excel_app.ActiveWorkBook.SaveAs FileName:=FilePath
End If
'关闭Excel:
excel_app.Quit
Set excel_app = Nothing
'还原鼠标形状:
Screen.MousePointer = vbDefault
MsgBox "导出了" & UBound(a) + 1 & "条记录", , "导出成功"
Exit Sub
myErr:
Screen.MousePointer = vbDefault
If Err.Number = 429 Then
Screen.MousePointer = vbDefault
MsgBox "请先安装EXCEL!", , "导出错误"
Exit Sub
End If
excel_app.DisplayAlerts = False '关闭时不提示保存
excel_app.Quit '关闭EXCEL
excel_app.DisplayAlerts = True '关闭时提示保存
Set excel_app = Nothing
MsgBox " 导出数据到 Excel 时出错! ", , "导出错误"
End Sub
MsgBox App.Path & ".\导出维修单列表_" & Format(DLDT1, "YYYYMMDD") & "_" & Hour(Time) & Minute(Time) & Second(Time) & ".xls"""
With myexcel.Selection
ActiveWorkbook.SaveAs FileName:=App.Path & "\导出维修单列表_" & Format(DLDT1, "YYYYMMDD") & "_" & Hour(Time) & Minute(Time) & Second(Time) & ".xls"
End WithD:\Downloads\backup\车辆维修项目\友诚日丰维修管理软件\导出维修单列表_20090822_17522.xls
mybook.Close
Set mysheet = Nothing
Set mybook = Nothing
myexcel.Quit
Set myexcel = Nothing
End Sub
再帮我看看还有那不对。
Public mybook As New Excel.Workbook
Public mysheet As New Excel.WorksheetPublic Sub OpenExcel()
Set myexcel = CreateObject("Excel.Application")
Set mybook = myexcel.Workbooks.Add
Set mysheet = mybook.Worksheets(1)
End Sub'Public Sub CloseExcel()
'Set mysheet = Nothing
'mybook.Close
'Set mybook = Nothing
'Set myexcel = Nothing
'myexcel.Quit
'End SubPublic Sub CloseExcel()
mybook.Close
Set mysheet = Nothing
Set mybook = Nothing
myexcel.Quit
Set myexcel = Nothing
End Sub
请问能不能导出后把最后一个excel进程也关闭呢?
在你的程序最后加上这句 Shell "taskkill /im EXCEL.exe /f", vbHide