I am using an api function to open excel and then tell my program when the user closes excel.
Declared constants and functions
Code:Const SYNCHRONIZE = &H100000 Const INFINITE = &HFFFF 'Wait forever Const WAIT_OBJECT_0 = 0 'The state of the specified object is signaled Const WAIT_TIMEOUT = &H102 'The time-out interval elapsed & the object’s state is nonsignaled. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _ ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _ ByVal dwMilliseconds As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
The problem is in this code here. Inorder to open to open the excel use the api code i have to use the shell command. But for the functionallity of my program, i need it to open a workbook. So I have done this. The problem is that it is opening 2 workbooks. Mine and a blank one, i need to eliminate the blank one or alter the code to work properly. Any Ideas. Thanks
Code:Private Sub ExcelOpen() Dim lPid As Long Dim lHnd As Long Dim lRet As Long lPid = Shell("C:\Program Files\Microsoft Office\Office\EXCEL.EXE", vbNormalFocus) Set obExcelApp = CreateObject("Excel.Application") Set obWorkBook = obExcelApp.Workbooks.Open(App.Path & "\line.xls") obExcelApp.Visible = True If lPid <> 0 Then 'Get a handle to the shelled process. lHnd = OpenProcess(SYNCHRONIZE, 0, lPid) 'If successful, wait for the application to end and close the handle. If lHnd <> 0 Then lRet = WaitForSingleObject(lHnd, INFINITE) CloseHandle (lHnd) End If MsgBox "Just terminated.", vbInformation, "Shelled Application" 'cmdSave_Click End If On Error GoTo Line1 obWorkBook.Close Line1: Set obWorkBook = Nothing Set obExcelApp = Nothing End Sub




Reply With Quote