用VB结束Excel的进程

前端之家收集整理的这篇文章主要介绍了用VB结束Excel的进程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. '窗体中放一个默认的按钮
  2. PrivateDeclareFunctionCreateToolhelp32SnapshotLib"kernel32"(ByValdwFlagsAsLong,ByValth32ProcessIDAsLong)AsLong
  3. PrivateDeclareFunctionProcess32FirstLib"kernel32"(ByValhSnapshotAsLong,lppeAsPROCESSENTRY32)AsLong
  4. PrivateDeclareFunctionProcess32NextLib"kernel32"(ByValhSnapshotAsLong,lppeAsPROCESSENTRY32)AsLong
  5. PrivateDeclareFunctionCloseHandleLib"kernel32"(ByValhObjectAsLong)AsLong
  6. PrivateDeclareFunctionOpenProcessLib"kernel32"(ByValdwDesiredAccessAsLong,_
  7. ByValblnheritHandleAsLong,ByValdwAppProcessIdAsLong)AsLong
  8. PrivateDeclareFunctionTerminateProcessLib"kernel32"(ByValApPHProcessAsLong,_
  9. ByValuExitCodeAsLong)AsLong
  10. PrivateTypePROCESSENTRY32
  11. dwSizeAsLong
  12. cntUsageAsLong
  13. th32ProcessIDAsLong
  14. th32DefaultHeapIDAsLong
  15. th32ModuleIDAsLong
  16. cntThreadsAsLong
  17. th32ParentProcessIDAsLong
  18. pcPriClassBaseAsLong
  19. dwFlagsAsLong
  20. szExeFileAsString*1024
  21. EndType
  22. ConstTH32CS_SNAPHEAPLIST=
  23. ConstTH32CS_SNAPPROCESS=
  24. ConstTH32CS_SNAPTHREAD=
  25. ConstTH32CS_SNAPMODULE=
  26. ConstTH32CS_SNAPALL=(TH32CS_SNAPHEAPLISTOrTH32CS_SNAPPROCESSOrTH32CS_SNAPTHREADOrTH32CS_SNAPMODULE)
  27. ConstTH32CS_INHERIT=
  28. PrivateSubCommand1_Click()
  29. DimmyAsPROCESSENTRY32
  30. DimlAsLong
  31. Diml1AsLong
  32. DimFileNameAsString*9
  33. DimmProcIDAsLong
  34. l=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0)
  35. IflThen
  36. my.dwSize=1060
  37. If(Process32First(l,my))Then'遍历第一个进程
  38. Do
  39. FileName=my.szExeFile
  40. IfUCase(FileName)="EXCEL.EXE"Then'当前的进程是Excel
  41. mProcID=OpenProcess(1&,-1&,my.th32ProcessID)
  42. TerminateProcessmProcID,0&'结束Excel进程
  43. Debug.Print"结束了"&FileName
  44. DoEvents
  45. EndIf
  46. LoopUntil(Process32Next(l,my)<1)'遍历所有进程直到返回值为False
  47. EndIf
  48. l1=CloseHandle(l)
  49. EndIf
  50. EndSub

用VB结束所有Excel的进程,用VB开发Exxcel的应用时,如果出现意外退出,Excel进程会驻留,以后再调用Excel时不能正常使用,所以有时有必要将所有Excel全部结束

原文链接:https://www.f2er.com/vb/264194.html

猜你在找的VB相关文章