Click to See Complete Forum and Search --> : Excel Object


Kathy
November 3rd, 1999, 08:55 AM
Hi ,
I want to write to an existing Workbook.
If the Workbook is open by the time I do Workbook.Save ,
I'll get error.

My problem is I don't know how to detect if the Workbook is currently open
sothat I'll prompt users to close the Workbook before I make changes to it


Thanks

November 3rd, 1999, 04:52 PM
Use Spy++ (included in VS 6 Enterprise) to find the Class of Window for Workbook, then use FindWidow API finction. If return is 0, there is no open Workbook, otherwise either give a message or close Workbook using another API PostMessage. For MS Word it looks like this

Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (byval lpClassName as string, byval lpWindowName as string)
Declare Function PostMessage& Lib "user32" Alias "PostMessageA" (byval hwnd as Long, byval wMsg as Long, byval wParam as Long, lParam as Any)
private Const WM_CLOSE = &H10
Dim lngReturn as Long
lngReturn = FindWindow("OpusApp", vbNullString)' OpusApp - Window Class for Word
lngReturn = PostMessage(lngHandle1, WM_CLOSE, 0, 0)



HTH
Vlad