程序功能:通过代码生成xls文件
问题点
1.为什么我连续生成好几次就会出现这个实时462错误呢,第一次肯定是好的
就是Command1_Click执行完,接着再执行,就会出现上面的错误,请问这是什么原因,应该怎样来解决这个问题呢,万分感谢!!
2.调用了VBA之后,EXCEL.EXE这个进程就一直存在,我用了自定义的过程xlsKill来杀进程,调用api函数的,就可以了,各位可以把最后的那句话注释掉看看,有没有更方便的方法呢
程序如下form中响应onclick事件
PrivateSubCommand1_Click()
DimxlbookAsExcel.Workbook
DimxlsheetAsExcel.Worksheet
DimxlappAsExcel.Application
Setxlapp=CreateObject("Excel.Application")
Setxlbook=xlapp.Workbooks.Add
Setxlsheet=xlbook.Worksheets(1)
xlsheet.Cells(1,1).Value="a1"
xlsheet.Cells(1,2).Value="b1"
xlsheet.Cells(2,1).Value="a2"
xlsheet.Cells(2,2).Value="b2"
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,1)).Select
WithSelection
.NumberFormatLocal="G/通用格式"
.HorizontalAlignment=xlRight
.VerticalAlignment=xlBottom
EndWith
xlbook.SaveAsApp.Path+"\tmp.xls"
xlbook.Close
xlapp.Quit
Setxlsheet=Nothing
Setxlbook=Nothing
Setxlapp=Nothing
MsgBox"excelok",vbOKOnly+vbInformation,"(:"
xlsKill
EndSub
killProcess.bas程序如下
OptionExplicit
PrivateDeclareFunctionCreateToolhelpSnapshotLib"kernel32"Alias"CreateToolhelp32Snapshot"(ByVallFlagsAsLong,ByVallProcessIDAsLong)AsLong
PrivateDeclareFunctionProcessFirstLib"kernel32"Alias"Process32First"(ByValhSnapShotAsLong,uProcessAsPROCESSENTRY32)AsLong
PrivateDeclareFunctionProcessNextLib"kernel32"Alias"Process32Next"(ByValhSnapShotAsLong,uProcessAsPROCESSENTRY32)AsLong
PrivateConstMAX_PATHAsLong=260
PrivateTypePROCESSENTRY32
dwSizeAsLong
cntUsageAsLong
th32ProcessIDAsLong
th32DefaultHeapIDAsLong
th32ModuleIDAsLong
cntThreadsAsLong
th32ParentProcessIDAsLong
pcPriClassBaseAsLong
dwFlagsAsLong
szExeFileAsString*MAX_PATH
EndType
PrivateDeclareFunctionTerminateProcessLib"kernel32"(ByValhProcessAsLong,ByValuExitCodeAsLong)AsLong
PrivateDeclareFunctionOpenProcessLib"kernel32"(ByValdwDesiredAccessAsLong,ByValbInheritHandleAsLong,ByValdwProcessIdAsLong)AsLong
PrivateDeclareFunctionCloseHandleLib"kernel32"(ByValhObjectAsLong)AsLong
PrivateConstTH32CS_SNAPPROCESS=&H2
PrivateConstTH32CS_SNAPheaplist=&H1
PrivateConstTH32CS_SNAPthread=&H4
PrivateConstTH32CS_SNAPmodule=&H8
PrivateConstTH32CS_SNAPall=TH32CS_SNAPPROCESS+TH32CS_SNAPheaplist+TH32CS_SNAPthread+TH32CS_SNAPmodule
PrivateConstPROCESS_TERMINATEAsLong=(&H1)
PublicSubxlsKill()
DimhandAsLong
DimiAsLong
DimprocAsPROCESSENTRY32
DimsnapAsLong
DimexenameAsString
DimtheloopAsLong
snap=CreateToolhelpSnapshot(TH32CS_SNAPall,0)
proc.dwSize=Len(proc)
theloop=ProcessFirst(snap,proc)
i=0
Whiletheloop<>0
exename=proc.szExeFile
IfLeft(Trim(exename),9)="EXCEL.EXE"Then
hand=OpenProcess(PROCESS_TERMINATE,True,proc.th32ProcessID)"获取进程句柄
TerminateProcesshand,0"关闭进程
ExitSub
EndIf
i=i+1
theloop=ProcessNext(snap,proc)
Wend
CloseHandlesnap
EndSub