Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default Close second instance of excel

Hi!

I have a data gathering programme that when I click the csv button it
creates the data in a csv file and opens it (always R.CSV) in a second
instance of excel.
In my first instance of excel I have some code that extracts the data from
the csv file and formats it differently. At present I have to close the
second instance of excel manually then by code I open in the first instance,
retrieve the data, close it and kill it, ready for the next lot of data.

The problem is if anyone forgets to close the second instance of excel I
cannot kill it.

What I would ideally like to do is in the first instance of excel, test if
there is a second instance running and if so close it. Is this possible?

TIA
Neil


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default Close second instance of excel

Hi again,
I am using Peter Beache's code as follows to test if there is another
instance running but I don't know how to close it if it is

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, lpWindowName As Any) As Long
Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal
hwnd As Long, ByVal wFlag As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA"
(ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long)
As Long
Private Const GW_HWNDNEXT = 2


Sub DupeXLRunning()
Dim lRet&
Dim StrRet$
Dim nLen&

lRet& = FindWindow("XLMain", ByVal 0&)

' Now this will always return a valid HWND, being the handle of this
instance of XL

Do
lRet& = GetNextWindow(lRet&, GW_HWNDNEXT)
If lRet& = 0 Then Exit Do
StrRet$ = Space$(100)
nLen& = GetClassName(lRet&, StrRet$, 100)
StrRet$ = Left$(StrRet$, nLen&)
If UCase$(StrRet$) = "XLMAIN" Then
MsgBox "Duplicate version of XL found"

Exit Sub
End If
Loop
MsgBox "Only one version of XL running"
End Sub

Neil

"Neil" wrote in message
...
Hi!

I have a data gathering programme that when I click the csv button it
creates the data in a csv file and opens it (always R.CSV) in a second
instance of excel.
In my first instance of excel I have some code that extracts the data from
the csv file and formats it differently. At present I have to close the
second instance of excel manually then by code I open in the first

instance,
retrieve the data, close it and kill it, ready for the next lot of data.

The problem is if anyone forgets to close the second instance of excel I
cannot kill it.

What I would ideally like to do is in the first instance of excel, test if
there is a second instance running and if so close it. Is this possible?

TIA
Neil




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default Close second instance of excel

Hi again,
I am using Peter Beache's code as follows to test if there is another
instance running but I don't know how to close it if it is


You can send a WM_CLOSE message to the hWnd in question.
See modified code below.

Regards,
Vic Eldridge



Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
lpWindowName As Any) As Long
Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" _
(ByVal hwnd As Long, _
ByVal wFlag As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Private Const GW_HWNDNEXT = 2
Private Const WM_CLOSE = &H10

Sub DupeXLRunning()
Dim lRet&
Dim StrRet$
Dim nLen&

lRet& = FindWindow("XLMain", ByVal 0&)

' Now this will always return a valid HWND,
' being the handle of this instance of XL

Do
lRet& = GetNextWindow(lRet&, GW_HWNDNEXT)
If lRet& = 0 Then Exit Do
StrRet$ = Space$(100)
nLen& = GetClassName(lRet&, StrRet$, 100)
StrRet$ = Left$(StrRet$, nLen&)
If UCase$(StrRet$) = "XLMAIN" Then
MsgBox "Duplicate version of XL found."
PostMessage lRet&, WM_CLOSE, 0&, 0&
Exit Sub
End If
Loop
MsgBox "Only one version of XL running"
End Sub
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel - New Instance JMay Excel Discussion (Misc queries) 4 November 28th 09 04:55 PM
why do all excel worksheets/workbooks close when I close one? Penny Excel Discussion (Misc queries) 1 November 29th 06 03:49 AM
Excel shoud not close all active books when clicking close button technomike Excel Discussion (Misc queries) 0 June 10th 05 05:35 PM
excel - Windows close button (x) should only close active workboo. CoffeeAdict Setting up and Configuration of Excel 3 February 8th 05 04:30 AM
Second instance of Excel Dom[_2_] Excel Programming 1 August 25th 03 05:37 PM


All times are GMT +1. The time now is 12:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"