vb 关闭excel

2024-11-07 13:55:14
推荐回答(3个)
回答1:

sub aa()
workdir = ActiveWorkbook.Path '获取当前路劲(C:\dcam\pmill2\程序单)
Workbooks.Open Filename:=workdir + "\Temp1.xls" '打开Temp1.xls

Windows("Temp1.xls").Activate
Application.DisplayAlerts = False ‘避免弹出对话框
'ActiveWorkbook.Save '保存表格,此处不保存
ActiveWorkbook.Close '关闭Temp1.xls
Application.DisplayAlerts = True
end sub

回答2:

加参数False,第一个参数就控制是否保存修改的。
举例:
Sub Test()
Dim Wb As Workbook
Workbooks.Add
Set Wb = ActiveWorkbook
Wb.Close False
End Sub

回答3:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Sub Command1_Click()
Dim hWnd As Long
hWnd = FindWindow("XLMAIN", "Microsoft Excel - Temp1.xls")
KillProcess hWnd
End Sub
Private Sub KillProcess(ByVal whWnd As Long)
Dim lpdwProcessId As Long
Dim hProcessHandle As Long
GetWindowThreadProcessId whWnd, lpdwProcessId
hProcessHandle = OpenProcess(&H1F0FFF, True, lpdwProcessId)
Dim success As Long
If hProcessHandle <> 0 Then
success = TerminateProcess(hProcessHandle, ByVal 0&)
End If
If success = 1 Then CloseHandle (hProcessHandle)
End Sub