View Single Post
  #17 (permalink)  
Old 03-26-04, 14:54
sacase sacase is offline
Registered User
 
Join Date: Mar 2004
Location: Jacksonville, FL
Posts: 1
Closing Excel

I was having the same problem and solved it by adding the following lines of code to get the window handle and then activate it and after I saved the workbook I sent the WM_CLOSE message:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Dim WinHandle As Long
Dim curApp As Excel.Application

Do While (FindWindow("XLMain", vbNullString)) <> 0
WinHandle = FindWindow("XLMain", vbNullString)
ShowWindow WinWnd1, SW_SHOWNORMAL

Set curApp = GetObject(, "Excel.Application")
curApp.ActiveWorkbook.SaveAs "C:\backup\Excel" & i & "_"
i = i + 1

SendMessage WinHandle, WM_CLOSE, 0, 0

Set curApp = Nothing

Loop

Let me know if this helps.

Steve
Reply With Quote