以下是使用VBA关闭Excel后台进程的示例代码: Sub CloseExcelProcess() Dim objWMIService As Object Dim colProcesses As Object Dim objProcess As Object Dim strComputer As String Dim strExcel As String '设置要关闭的进程名 strExcel = "excel.exe" '设置计算机名称 strComputer = "." '连接到WMI服务 Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") '获取进程列表 Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process") '循环遍历进程列表 For Each objProcess In colProcesses '关闭Excel进程 If InStr(1, objProcess.CommandLine, strExcel, vbTextCompare) > 0 Then objProcess.Terminate End If Next '释放资源 Set objWMIService = Nothing Set colProcesses = Nothing End Sub 该代码遍历当前计算机上所有的进程名称,并关闭名为“excel.exe”的Excel进程。如果您需要在代码中适当修改进程名称,可以通过更改代码中的“strExcel”变量来实现。
标签:Dim,VBA,objProcess,colProcesses,Excel,进程,strExcel,后台 From: https://www.cnblogs.com/klpwap/p/18125821