如果要关闭某个正在打开的文件,可以使用如下代码,是我网上找的。
'原理:先用FindWindow找到程序窗体,然后用SendMessage向这个窗口应答。
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "ostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SYSCOMMAND = &H112
Private Const SC_MINIMIZE = &HF020&
Private Const WM_CLOSE = &H10
Private Declare Function DestroyWindow Lib "user32" (ByVal hWnd As Long) As Long
Dim windowsH As Long
Public Sub close_excelfile()
'关闭正在打开的文件,只要获取到 指定文件的caption就可以办到
a = FindWindow(vbNullString, "microsoft excel - 台帐.xls")
Debug.Print a
b = PostMessage(a, WM_CLOSE, 0, 0)
Debug.Print b
End Sub
[此贴子已经被作者于2007-1-29 20:40:54编辑过]
|