+ Reply to Thread
Results 1 to 11 of 11

2 instances of excel

  1. #1
    John
    Guest

    2 instances of excel

    Could someone please!!! help me close a second instance of excel. If it
    helps, the file name is ("c:\blp\data\t0983103.xls")...

    Or help me understand if this is on the right track...

    ' Declare necessary API routines:
    Declare Function FindWindow Lib "user32" Alias _
    "FindWindowA" (ByVal lpClassName as String, _
    ByVal lpWindowName As Long) As Long

    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

    Sub GetExcel()
    Dim MyXL As Object ' Variable to hold reference
    ' to Microsoft Excel.
    Dim ExcelWasNotRunning As Boolean ' Flag for final release.

    ' Test to see if there is a copy of Microsoft Excel already running.
    On Error Resume Next ' Defer error trapping.
    ' Getobject function called without the first argument returns a
    ' reference to an instance of the application. If the application isn't
    ' running, an error occurs.
    Set MyXL = Getobject(, "Excel.Application")
    If Err.Number <> 0 Then ExcelWasNotRunning = True
    Err.Clear ' Clear Err object in case error occurred.

    ' Check for Microsoft Excel. If Microsoft Excel is running,
    ' enter it into the Running Object table.
    DetectExcel

    ' Set the object variable to reference the file you want to see.
    Set MyXL = Getobject("c:\vb4\MYTEST.XLS")

    ' Show Microsoft Excel through its Application property. Then
    ' show the actual window containing the file using the Windows
    ' collection of the MyXL object reference.
    MyXL.Application.Visible = True
    MyXL.Parent.Windows(1).Visible = True
    Do manipulations of your file here.
    ' ...
    ' If this copy of Microsoft Excel was not running when you
    ' started, close it using the Application property's Quit method.
    ' Note that when you try to quit Microsoft Excel, the
    ' title bar blinks and a message is displayed asking if you
    ' want to save any loaded files.
    If ExcelWasNotRunning = True Then
    MyXL.Application.Quit
    End IF

    Set MyXL = Nothing ' Release reference to the
    ' application and spreadsheet.
    End Sub

    Sub DetectExcel()
    ' Procedure dectects a running Excel and registers it.
    Const WM_USER = 1024
    Dim hWnd As Long
    ' If Excel is running this API call returns its handle.
    hWnd = FindWindow("XLMAIN", 0)
    If hWnd = 0 Then ' 0 means Excel not running.
    Exit Sub
    Else
    ' Excel is running so use the SendMessage API
    ' function to enter it in the Running Object Table.
    SendMessage hWnd, WM_USER + 18, 0, 0
    End If
    End Sub



  2. #2
    Tom Ogilvy
    Guest

    Re: 2 instances of excel

    I have never seen any code that translates a handle to the application
    window into an object instance of the application so you can manipulate it
    using automation (although I am sure there must be a way to do it).

    Possibly you could use the handle to bring it to the top and use send keys
    to close it.

    You could possibly identify it as a process and kill the process

    However, the more important question may be how it got opened. Perhaps you
    can avoid the problem.

    --
    Regards,
    Tom Ogilvy

    "John" <[email protected]> wrote in message
    news:[email protected]...
    > Could someone please!!! help me close a second instance of excel. If it
    > helps, the file name is ("c:\blp\data\t0983103.xls")...
    >
    > Or help me understand if this is on the right track...
    >
    > ' Declare necessary API routines:
    > Declare Function FindWindow Lib "user32" Alias _
    > "FindWindowA" (ByVal lpClassName as String, _
    > ByVal lpWindowName As Long) As Long
    >
    > 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
    >
    > Sub GetExcel()
    > Dim MyXL As Object ' Variable to hold reference
    > ' to Microsoft Excel.
    > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    >
    > ' Test to see if there is a copy of Microsoft Excel already running.
    > On Error Resume Next ' Defer error trapping.
    > ' Getobject function called without the first argument returns a
    > ' reference to an instance of the application. If the application isn't
    > ' running, an error occurs.
    > Set MyXL = Getobject(, "Excel.Application")
    > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > Err.Clear ' Clear Err object in case error occurred.
    >
    > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > ' enter it into the Running Object table.
    > DetectExcel
    >
    > ' Set the object variable to reference the file you want to see.
    > Set MyXL = Getobject("c:\vb4\MYTEST.XLS")
    >
    > ' Show Microsoft Excel through its Application property. Then
    > ' show the actual window containing the file using the Windows
    > ' collection of the MyXL object reference.
    > MyXL.Application.Visible = True
    > MyXL.Parent.Windows(1).Visible = True
    > Do manipulations of your file here.
    > ' ...
    > ' If this copy of Microsoft Excel was not running when you
    > ' started, close it using the Application property's Quit method.
    > ' Note that when you try to quit Microsoft Excel, the
    > ' title bar blinks and a message is displayed asking if you
    > ' want to save any loaded files.
    > If ExcelWasNotRunning = True Then
    > MyXL.Application.Quit
    > End IF
    >
    > Set MyXL = Nothing ' Release reference to the
    > ' application and spreadsheet.
    > End Sub
    >
    > Sub DetectExcel()
    > ' Procedure dectects a running Excel and registers it.
    > Const WM_USER = 1024
    > Dim hWnd As Long
    > ' If Excel is running this API call returns its handle.
    > hWnd = FindWindow("XLMAIN", 0)
    > If hWnd = 0 Then ' 0 means Excel not running.
    > Exit Sub
    > Else
    > ' Excel is running so use the SendMessage API
    > ' function to enter it in the Running Object Table.
    > SendMessage hWnd, WM_USER + 18, 0, 0
    > End If
    > End Sub
    >
    >




  3. #3
    John
    Guest

    Re: 2 instances of excel

    I found this to work (I made a minor change or two)... that seems to be the
    way it goes, you finally break down and post and then figure it out...

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

    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
    Sub DetectExcel()
    ' Procedure dectects a running Excel and registers it.
    Const WM_USER = 1024
    Dim hWnd As Long
    ' If Excel is running this API call returns its handle.
    hWnd = Window.Close("Microsoft Excel - t0983103.xls")
    If hWnd = 0 Then ' 0 means Excel not running.
    Exit Sub
    Else
    ' Excel is running so use the SendMessage API
    ' function to enter it in the Running Object Table.
    'SendMessage hWnd, WM_USER + 18, 0, 0
    Application.DisplayAlerts = False
    Application.Quit
    Application.DisplayAlerts = True
    End If
    End Sub
    Sub CloseReport()
    Dim MyXL As Object ' Variable to hold reference
    ' to Microsoft Excel.
    Dim ExcelWasNotRunning As Boolean ' Flag for final release.

    ' Test to see if there is a copy of Microsoft Excel already running.
    On Error Resume Next ' Defer error trapping.
    ' Getobject function called without the first argument returns a
    ' reference to an instance of the application. If the application isn't
    ' running, an error occurs.
    Set MyXL = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then ExcelWasNotRunning = True
    Err.Clear ' Clear Err object in case error occurred.

    ' Check for Microsoft Excel. If Microsoft Excel is running,
    ' enter it into the Running Object table.
    DetectExcel

    ' Set the object variable to reference the file you want to see.
    Set MyXL = GetObject("c:\blp\data\t0983103.XLS")

    ' Show Microsoft Excel through its Application property. Then
    ' show the actual window containing the file using the Windows
    ' collection of the MyXL object reference.
    MyXL.Application.Quit
    MyXL.Parent.Windows(1).Visible = True
    'Do manipulations of your file here.
    ' ...
    ' If this copy of Microsoft Excel was not running when you
    ' started, close it using the Application property's Quit method.
    ' Note that when you try to quit Microsoft Excel, the
    ' title bar blinks and a message is displayed asking if you
    ' want to save any loaded files.
    If ExcelWasNotRunning = True Then
    MyXL.Application.Quit
    End If

    Set MyXL = Nothing ' Release reference to the
    ' application and spreadsheet.
    End Sub


    "Tom Ogilvy" wrote:

    > I have never seen any code that translates a handle to the application
    > window into an object instance of the application so you can manipulate it
    > using automation (although I am sure there must be a way to do it).
    >
    > Possibly you could use the handle to bring it to the top and use send keys
    > to close it.
    >
    > You could possibly identify it as a process and kill the process
    >
    > However, the more important question may be how it got opened. Perhaps you
    > can avoid the problem.
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "John" <[email protected]> wrote in message
    > news:[email protected]...
    > > Could someone please!!! help me close a second instance of excel. If it
    > > helps, the file name is ("c:\blp\data\t0983103.xls")...
    > >
    > > Or help me understand if this is on the right track...
    > >
    > > ' Declare necessary API routines:
    > > Declare Function FindWindow Lib "user32" Alias _
    > > "FindWindowA" (ByVal lpClassName as String, _
    > > ByVal lpWindowName As Long) As Long
    > >
    > > 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
    > >
    > > Sub GetExcel()
    > > Dim MyXL As Object ' Variable to hold reference
    > > ' to Microsoft Excel.
    > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > >
    > > ' Test to see if there is a copy of Microsoft Excel already running.
    > > On Error Resume Next ' Defer error trapping.
    > > ' Getobject function called without the first argument returns a
    > > ' reference to an instance of the application. If the application isn't
    > > ' running, an error occurs.
    > > Set MyXL = Getobject(, "Excel.Application")
    > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > Err.Clear ' Clear Err object in case error occurred.
    > >
    > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > ' enter it into the Running Object table.
    > > DetectExcel
    > >
    > > ' Set the object variable to reference the file you want to see.
    > > Set MyXL = Getobject("c:\vb4\MYTEST.XLS")
    > >
    > > ' Show Microsoft Excel through its Application property. Then
    > > ' show the actual window containing the file using the Windows
    > > ' collection of the MyXL object reference.
    > > MyXL.Application.Visible = True
    > > MyXL.Parent.Windows(1).Visible = True
    > > Do manipulations of your file here.
    > > ' ...
    > > ' If this copy of Microsoft Excel was not running when you
    > > ' started, close it using the Application property's Quit method.
    > > ' Note that when you try to quit Microsoft Excel, the
    > > ' title bar blinks and a message is displayed asking if you
    > > ' want to save any loaded files.
    > > If ExcelWasNotRunning = True Then
    > > MyXL.Application.Quit
    > > End IF
    > >
    > > Set MyXL = Nothing ' Release reference to the
    > > ' application and spreadsheet.
    > > End Sub
    > >
    > > Sub DetectExcel()
    > > ' Procedure dectects a running Excel and registers it.
    > > Const WM_USER = 1024
    > > Dim hWnd As Long
    > > ' If Excel is running this API call returns its handle.
    > > hWnd = FindWindow("XLMAIN", 0)
    > > If hWnd = 0 Then ' 0 means Excel not running.
    > > Exit Sub
    > > Else
    > > ' Excel is running so use the SendMessage API
    > > ' function to enter it in the Running Object Table.
    > > SendMessage hWnd, WM_USER + 18, 0, 0
    > > End If
    > > End Sub
    > >
    > >

    >
    >
    >


  4. #4
    Tom Ogilvy
    Guest

    Re: 2 instances of excel

    Your either in for an eventual surprise or you didn't have two instances of
    Excel open. Good luck.

    --
    Regards,
    Tom Ogilvy

    "John" <[email protected]> wrote in message
    news:[email protected]...
    > I found this to work (I made a minor change or two)... that seems to be

    the
    > way it goes, you finally break down and post and then figure it out...
    >
    > Declare Function FindWindow Lib "user32" Alias _
    > "FindWindowA" (ByVal lpClassName As String, _
    > ByVal lpWindowName As Long) As Long
    >
    > 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
    > Sub DetectExcel()
    > ' Procedure dectects a running Excel and registers it.
    > Const WM_USER = 1024
    > Dim hWnd As Long
    > ' If Excel is running this API call returns its handle.
    > hWnd = Window.Close("Microsoft Excel - t0983103.xls")
    > If hWnd = 0 Then ' 0 means Excel not running.
    > Exit Sub
    > Else
    > ' Excel is running so use the SendMessage API
    > ' function to enter it in the Running Object Table.
    > 'SendMessage hWnd, WM_USER + 18, 0, 0
    > Application.DisplayAlerts = False
    > Application.Quit
    > Application.DisplayAlerts = True
    > End If
    > End Sub
    > Sub CloseReport()
    > Dim MyXL As Object ' Variable to hold reference
    > ' to Microsoft Excel.
    > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    >
    > ' Test to see if there is a copy of Microsoft Excel already running.
    > On Error Resume Next ' Defer error trapping.
    > ' Getobject function called without the first argument returns a
    > ' reference to an instance of the application. If the application isn't
    > ' running, an error occurs.
    > Set MyXL = GetObject(, "Excel.Application")
    > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > Err.Clear ' Clear Err object in case error occurred.
    >
    > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > ' enter it into the Running Object table.
    > DetectExcel
    >
    > ' Set the object variable to reference the file you want to see.
    > Set MyXL = GetObject("c:\blp\data\t0983103.XLS")
    >
    > ' Show Microsoft Excel through its Application property. Then
    > ' show the actual window containing the file using the Windows
    > ' collection of the MyXL object reference.
    > MyXL.Application.Quit
    > MyXL.Parent.Windows(1).Visible = True
    > 'Do manipulations of your file here.
    > ' ...
    > ' If this copy of Microsoft Excel was not running when you
    > ' started, close it using the Application property's Quit method.
    > ' Note that when you try to quit Microsoft Excel, the
    > ' title bar blinks and a message is displayed asking if you
    > ' want to save any loaded files.
    > If ExcelWasNotRunning = True Then
    > MyXL.Application.Quit
    > End If
    >
    > Set MyXL = Nothing ' Release reference to the
    > ' application and spreadsheet.
    > End Sub
    >
    >
    > "Tom Ogilvy" wrote:
    >
    > > I have never seen any code that translates a handle to the application
    > > window into an object instance of the application so you can manipulate

    it
    > > using automation (although I am sure there must be a way to do it).
    > >
    > > Possibly you could use the handle to bring it to the top and use send

    keys
    > > to close it.
    > >
    > > You could possibly identify it as a process and kill the process
    > >
    > > However, the more important question may be how it got opened. Perhaps

    you
    > > can avoid the problem.
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > > "John" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > Could someone please!!! help me close a second instance of excel. If

    it
    > > > helps, the file name is ("c:\blp\data\t0983103.xls")...
    > > >
    > > > Or help me understand if this is on the right track...
    > > >
    > > > ' Declare necessary API routines:
    > > > Declare Function FindWindow Lib "user32" Alias _
    > > > "FindWindowA" (ByVal lpClassName as String, _
    > > > ByVal lpWindowName As Long) As Long
    > > >
    > > > 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
    > > >
    > > > Sub GetExcel()
    > > > Dim MyXL As Object ' Variable to hold reference
    > > > ' to Microsoft Excel.
    > > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > > >
    > > > ' Test to see if there is a copy of Microsoft Excel already running.
    > > > On Error Resume Next ' Defer error trapping.
    > > > ' Getobject function called without the first argument returns a
    > > > ' reference to an instance of the application. If the application

    isn't
    > > > ' running, an error occurs.
    > > > Set MyXL = Getobject(, "Excel.Application")
    > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > Err.Clear ' Clear Err object in case error occurred.
    > > >
    > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > ' enter it into the Running Object table.
    > > > DetectExcel
    > > >
    > > > ' Set the object variable to reference the file you want to see.
    > > > Set MyXL = Getobject("c:\vb4\MYTEST.XLS")
    > > >
    > > > ' Show Microsoft Excel through its Application property. Then
    > > > ' show the actual window containing the file using the Windows
    > > > ' collection of the MyXL object reference.
    > > > MyXL.Application.Visible = True
    > > > MyXL.Parent.Windows(1).Visible = True
    > > > Do manipulations of your file here.
    > > > ' ...
    > > > ' If this copy of Microsoft Excel was not running when you
    > > > ' started, close it using the Application property's Quit method.
    > > > ' Note that when you try to quit Microsoft Excel, the
    > > > ' title bar blinks and a message is displayed asking if you
    > > > ' want to save any loaded files.
    > > > If ExcelWasNotRunning = True Then
    > > > MyXL.Application.Quit
    > > > End IF
    > > >
    > > > Set MyXL = Nothing ' Release reference to the
    > > > ' application and spreadsheet.
    > > > End Sub
    > > >
    > > > Sub DetectExcel()
    > > > ' Procedure dectects a running Excel and registers it.
    > > > Const WM_USER = 1024
    > > > Dim hWnd As Long
    > > > ' If Excel is running this API call returns its handle.
    > > > hWnd = FindWindow("XLMAIN", 0)
    > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > Exit Sub
    > > > Else
    > > > ' Excel is running so use the SendMessage API
    > > > ' function to enter it in the Running Object Table.
    > > > SendMessage hWnd, WM_USER + 18, 0, 0
    > > > End If
    > > > End Sub
    > > >
    > > >

    > >
    > >
    > >




  5. #5
    John
    Guest

    Re: 2 instances of excel

    If I don't have two instances of excel open... it will close even the
    "master" file... since I have an application.quit in there. However, as I
    said, it seems to run fine with two seperate excel instances... Why does
    this seem surprising?

    ps - i did not write the code, only modified small portions of it...

    "Tom Ogilvy" wrote:

    > Your either in for an eventual surprise or you didn't have two instances of
    > Excel open. Good luck.
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "John" <[email protected]> wrote in message
    > news:[email protected]...
    > > I found this to work (I made a minor change or two)... that seems to be

    > the
    > > way it goes, you finally break down and post and then figure it out...
    > >
    > > Declare Function FindWindow Lib "user32" Alias _
    > > "FindWindowA" (ByVal lpClassName As String, _
    > > ByVal lpWindowName As Long) As Long
    > >
    > > 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
    > > Sub DetectExcel()
    > > ' Procedure dectects a running Excel and registers it.
    > > Const WM_USER = 1024
    > > Dim hWnd As Long
    > > ' If Excel is running this API call returns its handle.
    > > hWnd = Window.Close("Microsoft Excel - t0983103.xls")
    > > If hWnd = 0 Then ' 0 means Excel not running.
    > > Exit Sub
    > > Else
    > > ' Excel is running so use the SendMessage API
    > > ' function to enter it in the Running Object Table.
    > > 'SendMessage hWnd, WM_USER + 18, 0, 0
    > > Application.DisplayAlerts = False
    > > Application.Quit
    > > Application.DisplayAlerts = True
    > > End If
    > > End Sub
    > > Sub CloseReport()
    > > Dim MyXL As Object ' Variable to hold reference
    > > ' to Microsoft Excel.
    > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > >
    > > ' Test to see if there is a copy of Microsoft Excel already running.
    > > On Error Resume Next ' Defer error trapping.
    > > ' Getobject function called without the first argument returns a
    > > ' reference to an instance of the application. If the application isn't
    > > ' running, an error occurs.
    > > Set MyXL = GetObject(, "Excel.Application")
    > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > Err.Clear ' Clear Err object in case error occurred.
    > >
    > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > ' enter it into the Running Object table.
    > > DetectExcel
    > >
    > > ' Set the object variable to reference the file you want to see.
    > > Set MyXL = GetObject("c:\blp\data\t0983103.XLS")
    > >
    > > ' Show Microsoft Excel through its Application property. Then
    > > ' show the actual window containing the file using the Windows
    > > ' collection of the MyXL object reference.
    > > MyXL.Application.Quit
    > > MyXL.Parent.Windows(1).Visible = True
    > > 'Do manipulations of your file here.
    > > ' ...
    > > ' If this copy of Microsoft Excel was not running when you
    > > ' started, close it using the Application property's Quit method.
    > > ' Note that when you try to quit Microsoft Excel, the
    > > ' title bar blinks and a message is displayed asking if you
    > > ' want to save any loaded files.
    > > If ExcelWasNotRunning = True Then
    > > MyXL.Application.Quit
    > > End If
    > >
    > > Set MyXL = Nothing ' Release reference to the
    > > ' application and spreadsheet.
    > > End Sub
    > >
    > >
    > > "Tom Ogilvy" wrote:
    > >
    > > > I have never seen any code that translates a handle to the application
    > > > window into an object instance of the application so you can manipulate

    > it
    > > > using automation (although I am sure there must be a way to do it).
    > > >
    > > > Possibly you could use the handle to bring it to the top and use send

    > keys
    > > > to close it.
    > > >
    > > > You could possibly identify it as a process and kill the process
    > > >
    > > > However, the more important question may be how it got opened. Perhaps

    > you
    > > > can avoid the problem.
    > > >
    > > > --
    > > > Regards,
    > > > Tom Ogilvy
    > > >
    > > > "John" <[email protected]> wrote in message
    > > > news:[email protected]...
    > > > > Could someone please!!! help me close a second instance of excel. If

    > it
    > > > > helps, the file name is ("c:\blp\data\t0983103.xls")...
    > > > >
    > > > > Or help me understand if this is on the right track...
    > > > >
    > > > > ' Declare necessary API routines:
    > > > > Declare Function FindWindow Lib "user32" Alias _
    > > > > "FindWindowA" (ByVal lpClassName as String, _
    > > > > ByVal lpWindowName As Long) As Long
    > > > >
    > > > > 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
    > > > >
    > > > > Sub GetExcel()
    > > > > Dim MyXL As Object ' Variable to hold reference
    > > > > ' to Microsoft Excel.
    > > > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > > > >
    > > > > ' Test to see if there is a copy of Microsoft Excel already running.
    > > > > On Error Resume Next ' Defer error trapping.
    > > > > ' Getobject function called without the first argument returns a
    > > > > ' reference to an instance of the application. If the application

    > isn't
    > > > > ' running, an error occurs.
    > > > > Set MyXL = Getobject(, "Excel.Application")
    > > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > > Err.Clear ' Clear Err object in case error occurred.
    > > > >
    > > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > > ' enter it into the Running Object table.
    > > > > DetectExcel
    > > > >
    > > > > ' Set the object variable to reference the file you want to see.
    > > > > Set MyXL = Getobject("c:\vb4\MYTEST.XLS")
    > > > >
    > > > > ' Show Microsoft Excel through its Application property. Then
    > > > > ' show the actual window containing the file using the Windows
    > > > > ' collection of the MyXL object reference.
    > > > > MyXL.Application.Visible = True
    > > > > MyXL.Parent.Windows(1).Visible = True
    > > > > Do manipulations of your file here.
    > > > > ' ...
    > > > > ' If this copy of Microsoft Excel was not running when you
    > > > > ' started, close it using the Application property's Quit method.
    > > > > ' Note that when you try to quit Microsoft Excel, the
    > > > > ' title bar blinks and a message is displayed asking if you
    > > > > ' want to save any loaded files.
    > > > > If ExcelWasNotRunning = True Then
    > > > > MyXL.Application.Quit
    > > > > End IF
    > > > >
    > > > > Set MyXL = Nothing ' Release reference to the
    > > > > ' application and spreadsheet.
    > > > > End Sub
    > > > >
    > > > > Sub DetectExcel()
    > > > > ' Procedure dectects a running Excel and registers it.
    > > > > Const WM_USER = 1024
    > > > > Dim hWnd As Long
    > > > > ' If Excel is running this API call returns its handle.
    > > > > hWnd = FindWindow("XLMAIN", 0)
    > > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > > Exit Sub
    > > > > Else
    > > > > ' Excel is running so use the SendMessage API
    > > > > ' function to enter it in the Running Object Table.
    > > > > SendMessage hWnd, WM_USER + 18, 0, 0
    > > > > End If
    > > > > End Sub
    > > > >
    > > > >
    > > >
    > > >
    > > >

    >
    >
    >


  6. #6
    Tom Ogilvy
    Guest

    Re: 2 instances of excel

    It is easy to recognize the code example from the getobject help example.
    GetObject doesn't discriminate between running instances of excel and you
    can't specify which instance nor is there a format that supports that. Nor
    is there a way to step sequentially through instances. If it closed the
    correct instance or returned a reference to the correct instance, then you
    were lucky.
    --
    Regards,
    Tom Ogilvy





    "John" <[email protected]> wrote in message
    news:[email protected]...
    > If I don't have two instances of excel open... it will close even the
    > "master" file... since I have an application.quit in there. However, as I
    > said, it seems to run fine with two seperate excel instances... Why does
    > this seem surprising?
    >
    > ps - i did not write the code, only modified small portions of it...
    >
    > "Tom Ogilvy" wrote:
    >
    > > Your either in for an eventual surprise or you didn't have two instances

    of
    > > Excel open. Good luck.
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > > "John" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > I found this to work (I made a minor change or two)... that seems to

    be
    > > the
    > > > way it goes, you finally break down and post and then figure it out...
    > > >
    > > > Declare Function FindWindow Lib "user32" Alias _
    > > > "FindWindowA" (ByVal lpClassName As String, _
    > > > ByVal lpWindowName As Long) As Long
    > > >
    > > > 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
    > > > Sub DetectExcel()
    > > > ' Procedure dectects a running Excel and registers it.
    > > > Const WM_USER = 1024
    > > > Dim hWnd As Long
    > > > ' If Excel is running this API call returns its handle.
    > > > hWnd = Window.Close("Microsoft Excel - t0983103.xls")
    > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > Exit Sub
    > > > Else
    > > > ' Excel is running so use the SendMessage API
    > > > ' function to enter it in the Running Object Table.
    > > > 'SendMessage hWnd, WM_USER + 18, 0, 0
    > > > Application.DisplayAlerts = False
    > > > Application.Quit
    > > > Application.DisplayAlerts = True
    > > > End If
    > > > End Sub
    > > > Sub CloseReport()
    > > > Dim MyXL As Object ' Variable to hold reference
    > > > ' to Microsoft Excel.
    > > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > > >
    > > > ' Test to see if there is a copy of Microsoft Excel already running.
    > > > On Error Resume Next ' Defer error trapping.
    > > > ' Getobject function called without the first argument returns a
    > > > ' reference to an instance of the application. If the application

    isn't
    > > > ' running, an error occurs.
    > > > Set MyXL = GetObject(, "Excel.Application")
    > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > Err.Clear ' Clear Err object in case error occurred.
    > > >
    > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > ' enter it into the Running Object table.
    > > > DetectExcel
    > > >
    > > > ' Set the object variable to reference the file you want to see.
    > > > Set MyXL = GetObject("c:\blp\data\t0983103.XLS")
    > > >
    > > > ' Show Microsoft Excel through its Application property. Then
    > > > ' show the actual window containing the file using the Windows
    > > > ' collection of the MyXL object reference.
    > > > MyXL.Application.Quit
    > > > MyXL.Parent.Windows(1).Visible = True
    > > > 'Do manipulations of your file here.
    > > > ' ...
    > > > ' If this copy of Microsoft Excel was not running when you
    > > > ' started, close it using the Application property's Quit method.
    > > > ' Note that when you try to quit Microsoft Excel, the
    > > > ' title bar blinks and a message is displayed asking if you
    > > > ' want to save any loaded files.
    > > > If ExcelWasNotRunning = True Then
    > > > MyXL.Application.Quit
    > > > End If
    > > >
    > > > Set MyXL = Nothing ' Release reference to the
    > > > ' application and spreadsheet.
    > > > End Sub
    > > >
    > > >
    > > > "Tom Ogilvy" wrote:
    > > >
    > > > > I have never seen any code that translates a handle to the

    application
    > > > > window into an object instance of the application so you can

    manipulate
    > > it
    > > > > using automation (although I am sure there must be a way to do it).
    > > > >
    > > > > Possibly you could use the handle to bring it to the top and use

    send
    > > keys
    > > > > to close it.
    > > > >
    > > > > You could possibly identify it as a process and kill the process
    > > > >
    > > > > However, the more important question may be how it got opened.

    Perhaps
    > > you
    > > > > can avoid the problem.
    > > > >
    > > > > --
    > > > > Regards,
    > > > > Tom Ogilvy
    > > > >
    > > > > "John" <[email protected]> wrote in message
    > > > > news:[email protected]...
    > > > > > Could someone please!!! help me close a second instance of excel.

    If
    > > it
    > > > > > helps, the file name is ("c:\blp\data\t0983103.xls")...
    > > > > >
    > > > > > Or help me understand if this is on the right track...
    > > > > >
    > > > > > ' Declare necessary API routines:
    > > > > > Declare Function FindWindow Lib "user32" Alias _
    > > > > > "FindWindowA" (ByVal lpClassName as String, _
    > > > > > ByVal lpWindowName As Long) As Long
    > > > > >
    > > > > > 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
    > > > > >
    > > > > > Sub GetExcel()
    > > > > > Dim MyXL As Object ' Variable to hold reference
    > > > > > ' to Microsoft Excel.
    > > > > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > > > > >
    > > > > > ' Test to see if there is a copy of Microsoft Excel already

    running.
    > > > > > On Error Resume Next ' Defer error trapping.
    > > > > > ' Getobject function called without the first argument returns a
    > > > > > ' reference to an instance of the application. If the application

    > > isn't
    > > > > > ' running, an error occurs.
    > > > > > Set MyXL = Getobject(, "Excel.Application")
    > > > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > > > Err.Clear ' Clear Err object in case error occurred.
    > > > > >
    > > > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > > > ' enter it into the Running Object table.
    > > > > > DetectExcel
    > > > > >
    > > > > > ' Set the object variable to reference the file you want to see.
    > > > > > Set MyXL = Getobject("c:\vb4\MYTEST.XLS")
    > > > > >
    > > > > > ' Show Microsoft Excel through its Application property. Then
    > > > > > ' show the actual window containing the file using the Windows
    > > > > > ' collection of the MyXL object reference.
    > > > > > MyXL.Application.Visible = True
    > > > > > MyXL.Parent.Windows(1).Visible = True
    > > > > > Do manipulations of your file here.
    > > > > > ' ...
    > > > > > ' If this copy of Microsoft Excel was not running when you
    > > > > > ' started, close it using the Application property's Quit method.
    > > > > > ' Note that when you try to quit Microsoft Excel, the
    > > > > > ' title bar blinks and a message is displayed asking if you
    > > > > > ' want to save any loaded files.
    > > > > > If ExcelWasNotRunning = True Then
    > > > > > MyXL.Application.Quit
    > > > > > End IF
    > > > > >
    > > > > > Set MyXL = Nothing ' Release reference to the
    > > > > > ' application and spreadsheet.
    > > > > > End Sub
    > > > > >
    > > > > > Sub DetectExcel()
    > > > > > ' Procedure dectects a running Excel and registers it.
    > > > > > Const WM_USER = 1024
    > > > > > Dim hWnd As Long
    > > > > > ' If Excel is running this API call returns its handle.
    > > > > > hWnd = FindWindow("XLMAIN", 0)
    > > > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > > > Exit Sub
    > > > > > Else
    > > > > > ' Excel is running so use the SendMessage API
    > > > > > ' function to enter it in the Running Object Table.
    > > > > > SendMessage hWnd, WM_USER + 18, 0, 0
    > > > > > End If
    > > > > > End Sub
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >




  7. #7
    Tom Ogilvy
    Guest

    Re: 2 instances of excel

    Although I did some testing and using the file name appeared to work each
    time - so maybe using the filename is the way to do it - but that is not
    what the help says.

    --
    Regards,
    Tom Ogilvy



    "Tom Ogilvy" <[email protected]> wrote in message
    news:[email protected]...
    > It is easy to recognize the code example from the getobject help example.
    > GetObject doesn't discriminate between running instances of excel and you
    > can't specify which instance nor is there a format that supports that.

    Nor
    > is there a way to step sequentially through instances. If it closed the
    > correct instance or returned a reference to the correct instance, then you
    > were lucky.
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    >
    >
    >
    > "John" <[email protected]> wrote in message
    > news:[email protected]...
    > > If I don't have two instances of excel open... it will close even the
    > > "master" file... since I have an application.quit in there. However, as

    I
    > > said, it seems to run fine with two seperate excel instances... Why

    does
    > > this seem surprising?
    > >
    > > ps - i did not write the code, only modified small portions of it...
    > >
    > > "Tom Ogilvy" wrote:
    > >
    > > > Your either in for an eventual surprise or you didn't have two

    instances
    > of
    > > > Excel open. Good luck.
    > > >
    > > > --
    > > > Regards,
    > > > Tom Ogilvy
    > > >
    > > > "John" <[email protected]> wrote in message
    > > > news:[email protected]...
    > > > > I found this to work (I made a minor change or two)... that seems to

    > be
    > > > the
    > > > > way it goes, you finally break down and post and then figure it

    out...
    > > > >
    > > > > Declare Function FindWindow Lib "user32" Alias _
    > > > > "FindWindowA" (ByVal lpClassName As String, _
    > > > > ByVal lpWindowName As Long) As Long
    > > > >
    > > > > 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
    > > > > Sub DetectExcel()
    > > > > ' Procedure dectects a running Excel and registers it.
    > > > > Const WM_USER = 1024
    > > > > Dim hWnd As Long
    > > > > ' If Excel is running this API call returns its handle.
    > > > > hWnd = Window.Close("Microsoft Excel - t0983103.xls")
    > > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > > Exit Sub
    > > > > Else
    > > > > ' Excel is running so use the SendMessage API
    > > > > ' function to enter it in the Running Object Table.
    > > > > 'SendMessage hWnd, WM_USER + 18, 0, 0
    > > > > Application.DisplayAlerts = False
    > > > > Application.Quit
    > > > > Application.DisplayAlerts = True
    > > > > End If
    > > > > End Sub
    > > > > Sub CloseReport()
    > > > > Dim MyXL As Object ' Variable to hold reference
    > > > > ' to Microsoft Excel.
    > > > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > > > >
    > > > > ' Test to see if there is a copy of Microsoft Excel already running.
    > > > > On Error Resume Next ' Defer error trapping.
    > > > > ' Getobject function called without the first argument returns a
    > > > > ' reference to an instance of the application. If the application

    > isn't
    > > > > ' running, an error occurs.
    > > > > Set MyXL = GetObject(, "Excel.Application")
    > > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > > Err.Clear ' Clear Err object in case error occurred.
    > > > >
    > > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > > ' enter it into the Running Object table.
    > > > > DetectExcel
    > > > >
    > > > > ' Set the object variable to reference the file you want to see.
    > > > > Set MyXL = GetObject("c:\blp\data\t0983103.XLS")
    > > > >
    > > > > ' Show Microsoft Excel through its Application property. Then
    > > > > ' show the actual window containing the file using the Windows
    > > > > ' collection of the MyXL object reference.
    > > > > MyXL.Application.Quit
    > > > > MyXL.Parent.Windows(1).Visible = True
    > > > > 'Do manipulations of your file here.
    > > > > ' ...
    > > > > ' If this copy of Microsoft Excel was not running when you
    > > > > ' started, close it using the Application property's Quit method.
    > > > > ' Note that when you try to quit Microsoft Excel, the
    > > > > ' title bar blinks and a message is displayed asking if you
    > > > > ' want to save any loaded files.
    > > > > If ExcelWasNotRunning = True Then
    > > > > MyXL.Application.Quit
    > > > > End If
    > > > >
    > > > > Set MyXL = Nothing ' Release reference to the
    > > > > ' application and spreadsheet.
    > > > > End Sub
    > > > >
    > > > >
    > > > > "Tom Ogilvy" wrote:
    > > > >
    > > > > > I have never seen any code that translates a handle to the

    > application
    > > > > > window into an object instance of the application so you can

    > manipulate
    > > > it
    > > > > > using automation (although I am sure there must be a way to do

    it).
    > > > > >
    > > > > > Possibly you could use the handle to bring it to the top and use

    > send
    > > > keys
    > > > > > to close it.
    > > > > >
    > > > > > You could possibly identify it as a process and kill the process
    > > > > >
    > > > > > However, the more important question may be how it got opened.

    > Perhaps
    > > > you
    > > > > > can avoid the problem.
    > > > > >
    > > > > > --
    > > > > > Regards,
    > > > > > Tom Ogilvy
    > > > > >
    > > > > > "John" <[email protected]> wrote in message
    > > > > > news:[email protected]...
    > > > > > > Could someone please!!! help me close a second instance of

    excel.
    > If
    > > > it
    > > > > > > helps, the file name is ("c:\blp\data\t0983103.xls")...
    > > > > > >
    > > > > > > Or help me understand if this is on the right track...
    > > > > > >
    > > > > > > ' Declare necessary API routines:
    > > > > > > Declare Function FindWindow Lib "user32" Alias _
    > > > > > > "FindWindowA" (ByVal lpClassName as String, _
    > > > > > > ByVal lpWindowName As Long) As Long
    > > > > > >
    > > > > > > 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
    > > > > > >
    > > > > > > Sub GetExcel()
    > > > > > > Dim MyXL As Object ' Variable to hold reference
    > > > > > > ' to Microsoft Excel.
    > > > > > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > > > > > >
    > > > > > > ' Test to see if there is a copy of Microsoft Excel already

    > running.
    > > > > > > On Error Resume Next ' Defer error trapping.
    > > > > > > ' Getobject function called without the first argument returns a
    > > > > > > ' reference to an instance of the application. If the

    application
    > > > isn't
    > > > > > > ' running, an error occurs.
    > > > > > > Set MyXL = Getobject(, "Excel.Application")
    > > > > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > > > > Err.Clear ' Clear Err object in case error occurred.
    > > > > > >
    > > > > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > > > > ' enter it into the Running Object table.
    > > > > > > DetectExcel
    > > > > > >
    > > > > > > ' Set the object variable to reference the file you want to see.
    > > > > > > Set MyXL = Getobject("c:\vb4\MYTEST.XLS")
    > > > > > >
    > > > > > > ' Show Microsoft Excel through its Application property. Then
    > > > > > > ' show the actual window containing the file using the Windows
    > > > > > > ' collection of the MyXL object reference.
    > > > > > > MyXL.Application.Visible = True
    > > > > > > MyXL.Parent.Windows(1).Visible = True
    > > > > > > Do manipulations of your file here.
    > > > > > > ' ...
    > > > > > > ' If this copy of Microsoft Excel was not running when you
    > > > > > > ' started, close it using the Application property's Quit

    method.
    > > > > > > ' Note that when you try to quit Microsoft Excel, the
    > > > > > > ' title bar blinks and a message is displayed asking if you
    > > > > > > ' want to save any loaded files.
    > > > > > > If ExcelWasNotRunning = True Then
    > > > > > > MyXL.Application.Quit
    > > > > > > End IF
    > > > > > >
    > > > > > > Set MyXL = Nothing ' Release reference to the
    > > > > > > ' application and spreadsheet.
    > > > > > > End Sub
    > > > > > >
    > > > > > > Sub DetectExcel()
    > > > > > > ' Procedure dectects a running Excel and registers it.
    > > > > > > Const WM_USER = 1024
    > > > > > > Dim hWnd As Long
    > > > > > > ' If Excel is running this API call returns its handle.
    > > > > > > hWnd = FindWindow("XLMAIN", 0)
    > > > > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > > > > Exit Sub
    > > > > > > Else
    > > > > > > ' Excel is running so use the SendMessage API
    > > > > > > ' function to enter it in the Running Object Table.
    > > > > > > SendMessage hWnd, WM_USER + 18, 0, 0
    > > > > > > End If
    > > > > > > End Sub
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > > >
    > > >
    > > >
    > > >

    >
    >




  8. #8
    John
    Guest

    Re: 2 instances of excel

    Yes, I have understand this is not supposed to be able to work... and I
    honestly don't know enough about VBA or this code to be able to say why it
    works. I can guess, but... I dunno. Overall, it is interesting, and maybe
    others who are more expirienced than I will find this helpful/interesting for
    future reference.

    By the way, thanks for helping make this a great site for info!



    "Tom Ogilvy" wrote:

    > Although I did some testing and using the file name appeared to work each
    > time - so maybe using the filename is the way to do it - but that is not
    > what the help says.
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    >
    > "Tom Ogilvy" <[email protected]> wrote in message
    > news:[email protected]...
    > > It is easy to recognize the code example from the getobject help example.
    > > GetObject doesn't discriminate between running instances of excel and you
    > > can't specify which instance nor is there a format that supports that.

    > Nor
    > > is there a way to step sequentially through instances. If it closed the
    > > correct instance or returned a reference to the correct instance, then you
    > > were lucky.
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > >
    > >
    > >
    > >
    > > "John" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > If I don't have two instances of excel open... it will close even the
    > > > "master" file... since I have an application.quit in there. However, as

    > I
    > > > said, it seems to run fine with two seperate excel instances... Why

    > does
    > > > this seem surprising?
    > > >
    > > > ps - i did not write the code, only modified small portions of it...
    > > >
    > > > "Tom Ogilvy" wrote:
    > > >
    > > > > Your either in for an eventual surprise or you didn't have two

    > instances
    > > of
    > > > > Excel open. Good luck.
    > > > >
    > > > > --
    > > > > Regards,
    > > > > Tom Ogilvy
    > > > >
    > > > > "John" <[email protected]> wrote in message
    > > > > news:[email protected]...
    > > > > > I found this to work (I made a minor change or two)... that seems to

    > > be
    > > > > the
    > > > > > way it goes, you finally break down and post and then figure it

    > out...
    > > > > >
    > > > > > Declare Function FindWindow Lib "user32" Alias _
    > > > > > "FindWindowA" (ByVal lpClassName As String, _
    > > > > > ByVal lpWindowName As Long) As Long
    > > > > >
    > > > > > 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
    > > > > > Sub DetectExcel()
    > > > > > ' Procedure dectects a running Excel and registers it.
    > > > > > Const WM_USER = 1024
    > > > > > Dim hWnd As Long
    > > > > > ' If Excel is running this API call returns its handle.
    > > > > > hWnd = Window.Close("Microsoft Excel - t0983103.xls")
    > > > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > > > Exit Sub
    > > > > > Else
    > > > > > ' Excel is running so use the SendMessage API
    > > > > > ' function to enter it in the Running Object Table.
    > > > > > 'SendMessage hWnd, WM_USER + 18, 0, 0
    > > > > > Application.DisplayAlerts = False
    > > > > > Application.Quit
    > > > > > Application.DisplayAlerts = True
    > > > > > End If
    > > > > > End Sub
    > > > > > Sub CloseReport()
    > > > > > Dim MyXL As Object ' Variable to hold reference
    > > > > > ' to Microsoft Excel.
    > > > > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > > > > >
    > > > > > ' Test to see if there is a copy of Microsoft Excel already running.
    > > > > > On Error Resume Next ' Defer error trapping.
    > > > > > ' Getobject function called without the first argument returns a
    > > > > > ' reference to an instance of the application. If the application

    > > isn't
    > > > > > ' running, an error occurs.
    > > > > > Set MyXL = GetObject(, "Excel.Application")
    > > > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > > > Err.Clear ' Clear Err object in case error occurred.
    > > > > >
    > > > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > > > ' enter it into the Running Object table.
    > > > > > DetectExcel
    > > > > >
    > > > > > ' Set the object variable to reference the file you want to see.
    > > > > > Set MyXL = GetObject("c:\blp\data\t0983103.XLS")
    > > > > >
    > > > > > ' Show Microsoft Excel through its Application property. Then
    > > > > > ' show the actual window containing the file using the Windows
    > > > > > ' collection of the MyXL object reference.
    > > > > > MyXL.Application.Quit
    > > > > > MyXL.Parent.Windows(1).Visible = True
    > > > > > 'Do manipulations of your file here.
    > > > > > ' ...
    > > > > > ' If this copy of Microsoft Excel was not running when you
    > > > > > ' started, close it using the Application property's Quit method.
    > > > > > ' Note that when you try to quit Microsoft Excel, the
    > > > > > ' title bar blinks and a message is displayed asking if you
    > > > > > ' want to save any loaded files.
    > > > > > If ExcelWasNotRunning = True Then
    > > > > > MyXL.Application.Quit
    > > > > > End If
    > > > > >
    > > > > > Set MyXL = Nothing ' Release reference to the
    > > > > > ' application and spreadsheet.
    > > > > > End Sub
    > > > > >
    > > > > >
    > > > > > "Tom Ogilvy" wrote:
    > > > > >
    > > > > > > I have never seen any code that translates a handle to the

    > > application
    > > > > > > window into an object instance of the application so you can

    > > manipulate
    > > > > it
    > > > > > > using automation (although I am sure there must be a way to do

    > it).
    > > > > > >
    > > > > > > Possibly you could use the handle to bring it to the top and use

    > > send
    > > > > keys
    > > > > > > to close it.
    > > > > > >
    > > > > > > You could possibly identify it as a process and kill the process
    > > > > > >
    > > > > > > However, the more important question may be how it got opened.

    > > Perhaps
    > > > > you
    > > > > > > can avoid the problem.
    > > > > > >
    > > > > > > --
    > > > > > > Regards,
    > > > > > > Tom Ogilvy
    > > > > > >
    > > > > > > "John" <[email protected]> wrote in message
    > > > > > > news:[email protected]...
    > > > > > > > Could someone please!!! help me close a second instance of

    > excel.
    > > If
    > > > > it
    > > > > > > > helps, the file name is ("c:\blp\data\t0983103.xls")...
    > > > > > > >
    > > > > > > > Or help me understand if this is on the right track...
    > > > > > > >
    > > > > > > > ' Declare necessary API routines:
    > > > > > > > Declare Function FindWindow Lib "user32" Alias _
    > > > > > > > "FindWindowA" (ByVal lpClassName as String, _
    > > > > > > > ByVal lpWindowName As Long) As Long
    > > > > > > >
    > > > > > > > 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
    > > > > > > >
    > > > > > > > Sub GetExcel()
    > > > > > > > Dim MyXL As Object ' Variable to hold reference
    > > > > > > > ' to Microsoft Excel.
    > > > > > > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > > > > > > >
    > > > > > > > ' Test to see if there is a copy of Microsoft Excel already

    > > running.
    > > > > > > > On Error Resume Next ' Defer error trapping.
    > > > > > > > ' Getobject function called without the first argument returns a
    > > > > > > > ' reference to an instance of the application. If the

    > application
    > > > > isn't
    > > > > > > > ' running, an error occurs.
    > > > > > > > Set MyXL = Getobject(, "Excel.Application")
    > > > > > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > > > > > Err.Clear ' Clear Err object in case error occurred.
    > > > > > > >
    > > > > > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > > > > > ' enter it into the Running Object table.
    > > > > > > > DetectExcel
    > > > > > > >
    > > > > > > > ' Set the object variable to reference the file you want to see.
    > > > > > > > Set MyXL = Getobject("c:\vb4\MYTEST.XLS")
    > > > > > > >
    > > > > > > > ' Show Microsoft Excel through its Application property. Then
    > > > > > > > ' show the actual window containing the file using the Windows
    > > > > > > > ' collection of the MyXL object reference.
    > > > > > > > MyXL.Application.Visible = True
    > > > > > > > MyXL.Parent.Windows(1).Visible = True
    > > > > > > > Do manipulations of your file here.
    > > > > > > > ' ...
    > > > > > > > ' If this copy of Microsoft Excel was not running when you
    > > > > > > > ' started, close it using the Application property's Quit

    > method.
    > > > > > > > ' Note that when you try to quit Microsoft Excel, the
    > > > > > > > ' title bar blinks and a message is displayed asking if you
    > > > > > > > ' want to save any loaded files.
    > > > > > > > If ExcelWasNotRunning = True Then
    > > > > > > > MyXL.Application.Quit
    > > > > > > > End IF
    > > > > > > >
    > > > > > > > Set MyXL = Nothing ' Release reference to the
    > > > > > > > ' application and spreadsheet.
    > > > > > > > End Sub
    > > > > > > >
    > > > > > > > Sub DetectExcel()
    > > > > > > > ' Procedure dectects a running Excel and registers it.
    > > > > > > > Const WM_USER = 1024
    > > > > > > > Dim hWnd As Long
    > > > > > > > ' If Excel is running this API call returns its handle.
    > > > > > > > hWnd = FindWindow("XLMAIN", 0)
    > > > > > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > > > > > Exit Sub
    > > > > > > > Else
    > > > > > > > ' Excel is running so use the SendMessage API
    > > > > > > > ' function to enter it in the Running Object Table.
    > > > > > > > SendMessage hWnd, WM_USER + 18, 0, 0
    > > > > > > > End If
    > > > > > > > End Sub
    > > > > > > >
    > > > > > > >
    > > > > > >
    > > > > > >
    > > > > > >
    > > > >
    > > > >
    > > > >

    > >
    > >

    >
    >
    >


  9. #9
    Tushar Mehta
    Guest

    Re: 2 instances of excel

    I am not sure what you are trying to do or what you are doing, but it
    seems to be overly complicated.

    First of all, start by instantiating the application object and not the
    workbook. Then, ensure all references to the instantiated application
    go through your variable. This will ensure that no behind-the-scenes
    copy of the application exists when you believe you are done with it.

    See
    How to safely instantiate another Office application and close it only
    if you started it
    http://support.microsoft.com/default...b;en-us;555191

    and

    Program won't quit
    http://www.tushar-mehta.com/excel/vb...quit/index.htm

    --
    Regards,

    Tushar Mehta
    www.tushar-mehta.com
    Excel, PowerPoint, and VBA add-ins, tutorials
    Custom MS Office productivity solutions

    In article <[email protected]>,
    [email protected] says...
    > Could someone please!!! help me close a second instance of excel. If it
    > helps, the file name is ("c:\blp\data\t0983103.xls")...
    >
    > Or help me understand if this is on the right track...
    >
    > ' Declare necessary API routines:
    > Declare Function FindWindow Lib "user32" Alias _
    > "FindWindowA" (ByVal lpClassName as String, _
    > ByVal lpWindowName As Long) As Long
    >
    > 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
    >
    > Sub GetExcel()
    > Dim MyXL As Object ' Variable to hold reference
    > ' to Microsoft Excel.
    > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    >
    > ' Test to see if there is a copy of Microsoft Excel already running.
    > On Error Resume Next ' Defer error trapping.
    > ' Getobject function called without the first argument returns a
    > ' reference to an instance of the application. If the application isn't
    > ' running, an error occurs.
    > Set MyXL = Getobject(, "Excel.Application")
    > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > Err.Clear ' Clear Err object in case error occurred.
    >
    > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > ' enter it into the Running Object table.
    > DetectExcel
    >
    > ' Set the object variable to reference the file you want to see.
    > Set MyXL = Getobject("c:\vb4\MYTEST.XLS")
    >
    > ' Show Microsoft Excel through its Application property. Then
    > ' show the actual window containing the file using the Windows
    > ' collection of the MyXL object reference.
    > MyXL.Application.Visible = True
    > MyXL.Parent.Windows(1).Visible = True
    > Do manipulations of your file here.
    > ' ...
    > ' If this copy of Microsoft Excel was not running when you
    > ' started, close it using the Application property's Quit method.
    > ' Note that when you try to quit Microsoft Excel, the
    > ' title bar blinks and a message is displayed asking if you
    > ' want to save any loaded files.
    > If ExcelWasNotRunning = True Then
    > MyXL.Application.Quit
    > End IF
    >
    > Set MyXL = Nothing ' Release reference to the
    > ' application and spreadsheet.
    > End Sub
    >
    > Sub DetectExcel()
    > ' Procedure dectects a running Excel and registers it.
    > Const WM_USER = 1024
    > Dim hWnd As Long
    > ' If Excel is running this API call returns its handle.
    > hWnd = FindWindow("XLMAIN", 0)
    > If hWnd = 0 Then ' 0 means Excel not running.
    > Exit Sub
    > Else
    > ' Excel is running so use the SendMessage API
    > ' function to enter it in the Running Object Table.
    > SendMessage hWnd, WM_USER + 18, 0, 0
    > End If
    > End Sub
    >
    >
    >


  10. #10
    NickHK
    Guest

    Re: 2 instances of excel

    John,
    Maybe I am missing something, but I just don't get the point of this code.
    If GetObject(,"Excel.Application") fails, how is FindWindow("XLMAIN", 0)
    going to succeed ?

    And Set MyXL = GetObject("c:\blp\data\t0983103.XLS") will always succeed,
    assuming the filename is valid.

    And this is Excel VBA:
    Application.DisplayAlerts = False
    Application.Quit
    Application.DisplayAlerts = True
    So how can GetObject(,"Excel.Application") possibly fail ?

    Also, what is "Window.Close(......" ?

    Confused.

    NickHK

    "John" <[email protected]> wrote in message
    news:[email protected]...
    > Yes, I have understand this is not supposed to be able to work... and I
    > honestly don't know enough about VBA or this code to be able to say why it
    > works. I can guess, but... I dunno. Overall, it is interesting, and

    maybe
    > others who are more expirienced than I will find this helpful/interesting

    for
    > future reference.
    >
    > By the way, thanks for helping make this a great site for info!
    >
    >
    >
    > "Tom Ogilvy" wrote:
    >
    > > Although I did some testing and using the file name appeared to work

    each
    > > time - so maybe using the filename is the way to do it - but that is not
    > > what the help says.
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > >
    > >
    > > "Tom Ogilvy" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > It is easy to recognize the code example from the getobject help

    example.
    > > > GetObject doesn't discriminate between running instances of excel and

    you
    > > > can't specify which instance nor is there a format that supports that.

    > > Nor
    > > > is there a way to step sequentially through instances. If it closed

    the
    > > > correct instance or returned a reference to the correct instance, then

    you
    > > > were lucky.
    > > > --
    > > > Regards,
    > > > Tom Ogilvy
    > > >
    > > >
    > > >
    > > >
    > > >
    > > > "John" <[email protected]> wrote in message
    > > > news:[email protected]...
    > > > > If I don't have two instances of excel open... it will close even

    the
    > > > > "master" file... since I have an application.quit in there.

    However, as
    > > I
    > > > > said, it seems to run fine with two seperate excel instances... Why

    > > does
    > > > > this seem surprising?
    > > > >
    > > > > ps - i did not write the code, only modified small portions of it...
    > > > >
    > > > > "Tom Ogilvy" wrote:
    > > > >
    > > > > > Your either in for an eventual surprise or you didn't have two

    > > instances
    > > > of
    > > > > > Excel open. Good luck.
    > > > > >
    > > > > > --
    > > > > > Regards,
    > > > > > Tom Ogilvy
    > > > > >
    > > > > > "John" <[email protected]> wrote in message
    > > > > > news:[email protected]...
    > > > > > > I found this to work (I made a minor change or two)... that

    seems to
    > > > be
    > > > > > the
    > > > > > > way it goes, you finally break down and post and then figure it

    > > out...
    > > > > > >
    > > > > > > Declare Function FindWindow Lib "user32" Alias _
    > > > > > > "FindWindowA" (ByVal lpClassName As String, _
    > > > > > > ByVal lpWindowName As Long) As Long
    > > > > > >
    > > > > > > 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
    > > > > > > Sub DetectExcel()
    > > > > > > ' Procedure dectects a running Excel and registers it.
    > > > > > > Const WM_USER = 1024
    > > > > > > Dim hWnd As Long
    > > > > > > ' If Excel is running this API call returns its handle.
    > > > > > > hWnd = Window.Close("Microsoft Excel - t0983103.xls")
    > > > > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > > > > Exit Sub
    > > > > > > Else
    > > > > > > ' Excel is running so use the SendMessage API
    > > > > > > ' function to enter it in the Running Object Table.
    > > > > > > 'SendMessage hWnd, WM_USER + 18, 0, 0
    > > > > > > Application.DisplayAlerts = False
    > > > > > > Application.Quit
    > > > > > > Application.DisplayAlerts = True
    > > > > > > End If
    > > > > > > End Sub
    > > > > > > Sub CloseReport()
    > > > > > > Dim MyXL As Object ' Variable to hold reference
    > > > > > > ' to Microsoft Excel.
    > > > > > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > > > > > >
    > > > > > > ' Test to see if there is a copy of Microsoft Excel already

    running.
    > > > > > > On Error Resume Next ' Defer error trapping.
    > > > > > > ' Getobject function called without the first argument returns a
    > > > > > > ' reference to an instance of the application. If the

    application
    > > > isn't
    > > > > > > ' running, an error occurs.
    > > > > > > Set MyXL = GetObject(, "Excel.Application")
    > > > > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > > > > Err.Clear ' Clear Err object in case error occurred.
    > > > > > >
    > > > > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > > > > ' enter it into the Running Object table.
    > > > > > > DetectExcel
    > > > > > >
    > > > > > > ' Set the object variable to reference the file you want to see.
    > > > > > > Set MyXL = GetObject("c:\blp\data\t0983103.XLS")
    > > > > > >
    > > > > > > ' Show Microsoft Excel through its Application property. Then
    > > > > > > ' show the actual window containing the file using the Windows
    > > > > > > ' collection of the MyXL object reference.
    > > > > > > MyXL.Application.Quit
    > > > > > > MyXL.Parent.Windows(1).Visible = True
    > > > > > > 'Do manipulations of your file here.
    > > > > > > ' ...
    > > > > > > ' If this copy of Microsoft Excel was not running when you
    > > > > > > ' started, close it using the Application property's Quit

    method.
    > > > > > > ' Note that when you try to quit Microsoft Excel, the
    > > > > > > ' title bar blinks and a message is displayed asking if you
    > > > > > > ' want to save any loaded files.
    > > > > > > If ExcelWasNotRunning = True Then
    > > > > > > MyXL.Application.Quit
    > > > > > > End If
    > > > > > >
    > > > > > > Set MyXL = Nothing ' Release reference to the
    > > > > > > ' application and spreadsheet.
    > > > > > > End Sub
    > > > > > >
    > > > > > >
    > > > > > > "Tom Ogilvy" wrote:
    > > > > > >
    > > > > > > > I have never seen any code that translates a handle to the
    > > > application
    > > > > > > > window into an object instance of the application so you can
    > > > manipulate
    > > > > > it
    > > > > > > > using automation (although I am sure there must be a way to do

    > > it).
    > > > > > > >
    > > > > > > > Possibly you could use the handle to bring it to the top and

    use
    > > > send
    > > > > > keys
    > > > > > > > to close it.
    > > > > > > >
    > > > > > > > You could possibly identify it as a process and kill the

    process
    > > > > > > >
    > > > > > > > However, the more important question may be how it got opened.
    > > > Perhaps
    > > > > > you
    > > > > > > > can avoid the problem.
    > > > > > > >
    > > > > > > > --
    > > > > > > > Regards,
    > > > > > > > Tom Ogilvy
    > > > > > > >
    > > > > > > > "John" <[email protected]> wrote in message
    > > > > > > > news:[email protected]...
    > > > > > > > > Could someone please!!! help me close a second instance of

    > > excel.
    > > > If
    > > > > > it
    > > > > > > > > helps, the file name is ("c:\blp\data\t0983103.xls")...
    > > > > > > > >
    > > > > > > > > Or help me understand if this is on the right track...
    > > > > > > > >
    > > > > > > > > ' Declare necessary API routines:
    > > > > > > > > Declare Function FindWindow Lib "user32" Alias _
    > > > > > > > > "FindWindowA" (ByVal lpClassName as String, _
    > > > > > > > > ByVal lpWindowName As Long) As Long
    > > > > > > > >
    > > > > > > > > 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
    > > > > > > > >
    > > > > > > > > Sub GetExcel()
    > > > > > > > > Dim MyXL As Object ' Variable to hold reference
    > > > > > > > > ' to Microsoft Excel.
    > > > > > > > > Dim ExcelWasNotRunning As Boolean ' Flag for final

    release.
    > > > > > > > >
    > > > > > > > > ' Test to see if there is a copy of Microsoft Excel already
    > > > running.
    > > > > > > > > On Error Resume Next ' Defer error trapping.
    > > > > > > > > ' Getobject function called without the first argument

    returns a
    > > > > > > > > ' reference to an instance of the application. If the

    > > application
    > > > > > isn't
    > > > > > > > > ' running, an error occurs.
    > > > > > > > > Set MyXL = Getobject(, "Excel.Application")
    > > > > > > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > > > > > > Err.Clear ' Clear Err object in case error occurred.
    > > > > > > > >
    > > > > > > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > > > > > > ' enter it into the Running Object table.
    > > > > > > > > DetectExcel
    > > > > > > > >
    > > > > > > > > ' Set the object variable to reference the file you want to

    see.
    > > > > > > > > Set MyXL = Getobject("c:\vb4\MYTEST.XLS")
    > > > > > > > >
    > > > > > > > > ' Show Microsoft Excel through its Application property.

    Then
    > > > > > > > > ' show the actual window containing the file using the

    Windows
    > > > > > > > > ' collection of the MyXL object reference.
    > > > > > > > > MyXL.Application.Visible = True
    > > > > > > > > MyXL.Parent.Windows(1).Visible = True
    > > > > > > > > Do manipulations of your file here.
    > > > > > > > > ' ...
    > > > > > > > > ' If this copy of Microsoft Excel was not running when you
    > > > > > > > > ' started, close it using the Application property's Quit

    > > method.
    > > > > > > > > ' Note that when you try to quit Microsoft Excel, the
    > > > > > > > > ' title bar blinks and a message is displayed asking if you
    > > > > > > > > ' want to save any loaded files.
    > > > > > > > > If ExcelWasNotRunning = True Then
    > > > > > > > > MyXL.Application.Quit
    > > > > > > > > End IF
    > > > > > > > >
    > > > > > > > > Set MyXL = Nothing ' Release reference to the
    > > > > > > > > ' application and spreadsheet.
    > > > > > > > > End Sub
    > > > > > > > >
    > > > > > > > > Sub DetectExcel()
    > > > > > > > > ' Procedure dectects a running Excel and registers it.
    > > > > > > > > Const WM_USER = 1024
    > > > > > > > > Dim hWnd As Long
    > > > > > > > > ' If Excel is running this API call returns its handle.
    > > > > > > > > hWnd = FindWindow("XLMAIN", 0)
    > > > > > > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > > > > > > Exit Sub
    > > > > > > > > Else
    > > > > > > > > ' Excel is running so use the SendMessage API
    > > > > > > > > ' function to enter it in the Running Object Table.
    > > > > > > > > SendMessage hWnd, WM_USER + 18, 0, 0
    > > > > > > > > End If
    > > > > > > > > End Sub
    > > > > > > > >
    > > > > > > > >
    > > > > > > >
    > > > > > > >
    > > > > > > >
    > > > > >
    > > > > >
    > > > > >
    > > >
    > > >

    > >
    > >
    > >




  11. #11
    John
    Guest

    Re: 2 instances of excel

    Nick,

    All I was trying to do was close an open file in a second instance of
    excel... I had no idea how to do that... and looked for some code to be able
    to do that. I came across the code I posted and then began to play with it
    myself. I still do not understand it, as I said earlier, but somehow it
    works for what I was trying to do. I could be and I am sure it is, very
    inefficient, but I have only been working with VBA for 2.5 months and so I
    really couldn't say what is going on with it fully...

    Sorry for the confusion, but I think it is cool because something in there
    works!


    "NickHK" wrote:

    > John,
    > Maybe I am missing something, but I just don't get the point of this code.
    > If GetObject(,"Excel.Application") fails, how is FindWindow("XLMAIN", 0)
    > going to succeed ?
    >
    > And Set MyXL = GetObject("c:\blp\data\t0983103.XLS") will always succeed,
    > assuming the filename is valid.
    >
    > And this is Excel VBA:
    > Application.DisplayAlerts = False
    > Application.Quit
    > Application.DisplayAlerts = True
    > So how can GetObject(,"Excel.Application") possibly fail ?
    >
    > Also, what is "Window.Close(......" ?
    >
    > Confused.
    >
    > NickHK
    >
    > "John" <[email protected]> wrote in message
    > news:[email protected]...
    > > Yes, I have understand this is not supposed to be able to work... and I
    > > honestly don't know enough about VBA or this code to be able to say why it
    > > works. I can guess, but... I dunno. Overall, it is interesting, and

    > maybe
    > > others who are more expirienced than I will find this helpful/interesting

    > for
    > > future reference.
    > >
    > > By the way, thanks for helping make this a great site for info!
    > >
    > >
    > >
    > > "Tom Ogilvy" wrote:
    > >
    > > > Although I did some testing and using the file name appeared to work

    > each
    > > > time - so maybe using the filename is the way to do it - but that is not
    > > > what the help says.
    > > >
    > > > --
    > > > Regards,
    > > > Tom Ogilvy
    > > >
    > > >
    > > >
    > > > "Tom Ogilvy" <[email protected]> wrote in message
    > > > news:[email protected]...
    > > > > It is easy to recognize the code example from the getobject help

    > example.
    > > > > GetObject doesn't discriminate between running instances of excel and

    > you
    > > > > can't specify which instance nor is there a format that supports that.
    > > > Nor
    > > > > is there a way to step sequentially through instances. If it closed

    > the
    > > > > correct instance or returned a reference to the correct instance, then

    > you
    > > > > were lucky.
    > > > > --
    > > > > Regards,
    > > > > Tom Ogilvy
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > > "John" <[email protected]> wrote in message
    > > > > news:[email protected]...
    > > > > > If I don't have two instances of excel open... it will close even

    > the
    > > > > > "master" file... since I have an application.quit in there.

    > However, as
    > > > I
    > > > > > said, it seems to run fine with two seperate excel instances... Why
    > > > does
    > > > > > this seem surprising?
    > > > > >
    > > > > > ps - i did not write the code, only modified small portions of it...
    > > > > >
    > > > > > "Tom Ogilvy" wrote:
    > > > > >
    > > > > > > Your either in for an eventual surprise or you didn't have two
    > > > instances
    > > > > of
    > > > > > > Excel open. Good luck.
    > > > > > >
    > > > > > > --
    > > > > > > Regards,
    > > > > > > Tom Ogilvy
    > > > > > >
    > > > > > > "John" <[email protected]> wrote in message
    > > > > > > news:[email protected]...
    > > > > > > > I found this to work (I made a minor change or two)... that

    > seems to
    > > > > be
    > > > > > > the
    > > > > > > > way it goes, you finally break down and post and then figure it
    > > > out...
    > > > > > > >
    > > > > > > > Declare Function FindWindow Lib "user32" Alias _
    > > > > > > > "FindWindowA" (ByVal lpClassName As String, _
    > > > > > > > ByVal lpWindowName As Long) As Long
    > > > > > > >
    > > > > > > > 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
    > > > > > > > Sub DetectExcel()
    > > > > > > > ' Procedure dectects a running Excel and registers it.
    > > > > > > > Const WM_USER = 1024
    > > > > > > > Dim hWnd As Long
    > > > > > > > ' If Excel is running this API call returns its handle.
    > > > > > > > hWnd = Window.Close("Microsoft Excel - t0983103.xls")
    > > > > > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > > > > > Exit Sub
    > > > > > > > Else
    > > > > > > > ' Excel is running so use the SendMessage API
    > > > > > > > ' function to enter it in the Running Object Table.
    > > > > > > > 'SendMessage hWnd, WM_USER + 18, 0, 0
    > > > > > > > Application.DisplayAlerts = False
    > > > > > > > Application.Quit
    > > > > > > > Application.DisplayAlerts = True
    > > > > > > > End If
    > > > > > > > End Sub
    > > > > > > > Sub CloseReport()
    > > > > > > > Dim MyXL As Object ' Variable to hold reference
    > > > > > > > ' to Microsoft Excel.
    > > > > > > > Dim ExcelWasNotRunning As Boolean ' Flag for final release.
    > > > > > > >
    > > > > > > > ' Test to see if there is a copy of Microsoft Excel already

    > running.
    > > > > > > > On Error Resume Next ' Defer error trapping.
    > > > > > > > ' Getobject function called without the first argument returns a
    > > > > > > > ' reference to an instance of the application. If the

    > application
    > > > > isn't
    > > > > > > > ' running, an error occurs.
    > > > > > > > Set MyXL = GetObject(, "Excel.Application")
    > > > > > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > > > > > Err.Clear ' Clear Err object in case error occurred.
    > > > > > > >
    > > > > > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > > > > > ' enter it into the Running Object table.
    > > > > > > > DetectExcel
    > > > > > > >
    > > > > > > > ' Set the object variable to reference the file you want to see.
    > > > > > > > Set MyXL = GetObject("c:\blp\data\t0983103.XLS")
    > > > > > > >
    > > > > > > > ' Show Microsoft Excel through its Application property. Then
    > > > > > > > ' show the actual window containing the file using the Windows
    > > > > > > > ' collection of the MyXL object reference.
    > > > > > > > MyXL.Application.Quit
    > > > > > > > MyXL.Parent.Windows(1).Visible = True
    > > > > > > > 'Do manipulations of your file here.
    > > > > > > > ' ...
    > > > > > > > ' If this copy of Microsoft Excel was not running when you
    > > > > > > > ' started, close it using the Application property's Quit

    > method.
    > > > > > > > ' Note that when you try to quit Microsoft Excel, the
    > > > > > > > ' title bar blinks and a message is displayed asking if you
    > > > > > > > ' want to save any loaded files.
    > > > > > > > If ExcelWasNotRunning = True Then
    > > > > > > > MyXL.Application.Quit
    > > > > > > > End If
    > > > > > > >
    > > > > > > > Set MyXL = Nothing ' Release reference to the
    > > > > > > > ' application and spreadsheet.
    > > > > > > > End Sub
    > > > > > > >
    > > > > > > >
    > > > > > > > "Tom Ogilvy" wrote:
    > > > > > > >
    > > > > > > > > I have never seen any code that translates a handle to the
    > > > > application
    > > > > > > > > window into an object instance of the application so you can
    > > > > manipulate
    > > > > > > it
    > > > > > > > > using automation (although I am sure there must be a way to do
    > > > it).
    > > > > > > > >
    > > > > > > > > Possibly you could use the handle to bring it to the top and

    > use
    > > > > send
    > > > > > > keys
    > > > > > > > > to close it.
    > > > > > > > >
    > > > > > > > > You could possibly identify it as a process and kill the

    > process
    > > > > > > > >
    > > > > > > > > However, the more important question may be how it got opened.
    > > > > Perhaps
    > > > > > > you
    > > > > > > > > can avoid the problem.
    > > > > > > > >
    > > > > > > > > --
    > > > > > > > > Regards,
    > > > > > > > > Tom Ogilvy
    > > > > > > > >
    > > > > > > > > "John" <[email protected]> wrote in message
    > > > > > > > > news:[email protected]...
    > > > > > > > > > Could someone please!!! help me close a second instance of
    > > > excel.
    > > > > If
    > > > > > > it
    > > > > > > > > > helps, the file name is ("c:\blp\data\t0983103.xls")...
    > > > > > > > > >
    > > > > > > > > > Or help me understand if this is on the right track...
    > > > > > > > > >
    > > > > > > > > > ' Declare necessary API routines:
    > > > > > > > > > Declare Function FindWindow Lib "user32" Alias _
    > > > > > > > > > "FindWindowA" (ByVal lpClassName as String, _
    > > > > > > > > > ByVal lpWindowName As Long) As Long
    > > > > > > > > >
    > > > > > > > > > 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
    > > > > > > > > >
    > > > > > > > > > Sub GetExcel()
    > > > > > > > > > Dim MyXL As Object ' Variable to hold reference
    > > > > > > > > > ' to Microsoft Excel.
    > > > > > > > > > Dim ExcelWasNotRunning As Boolean ' Flag for final

    > release.
    > > > > > > > > >
    > > > > > > > > > ' Test to see if there is a copy of Microsoft Excel already
    > > > > running.
    > > > > > > > > > On Error Resume Next ' Defer error trapping.
    > > > > > > > > > ' Getobject function called without the first argument

    > returns a
    > > > > > > > > > ' reference to an instance of the application. If the
    > > > application
    > > > > > > isn't
    > > > > > > > > > ' running, an error occurs.
    > > > > > > > > > Set MyXL = Getobject(, "Excel.Application")
    > > > > > > > > > If Err.Number <> 0 Then ExcelWasNotRunning = True
    > > > > > > > > > Err.Clear ' Clear Err object in case error occurred.
    > > > > > > > > >
    > > > > > > > > > ' Check for Microsoft Excel. If Microsoft Excel is running,
    > > > > > > > > > ' enter it into the Running Object table.
    > > > > > > > > > DetectExcel
    > > > > > > > > >
    > > > > > > > > > ' Set the object variable to reference the file you want to

    > see.
    > > > > > > > > > Set MyXL = Getobject("c:\vb4\MYTEST.XLS")
    > > > > > > > > >
    > > > > > > > > > ' Show Microsoft Excel through its Application property.

    > Then
    > > > > > > > > > ' show the actual window containing the file using the

    > Windows
    > > > > > > > > > ' collection of the MyXL object reference.
    > > > > > > > > > MyXL.Application.Visible = True
    > > > > > > > > > MyXL.Parent.Windows(1).Visible = True
    > > > > > > > > > Do manipulations of your file here.
    > > > > > > > > > ' ...
    > > > > > > > > > ' If this copy of Microsoft Excel was not running when you
    > > > > > > > > > ' started, close it using the Application property's Quit
    > > > method.
    > > > > > > > > > ' Note that when you try to quit Microsoft Excel, the
    > > > > > > > > > ' title bar blinks and a message is displayed asking if you
    > > > > > > > > > ' want to save any loaded files.
    > > > > > > > > > If ExcelWasNotRunning = True Then
    > > > > > > > > > MyXL.Application.Quit
    > > > > > > > > > End IF
    > > > > > > > > >
    > > > > > > > > > Set MyXL = Nothing ' Release reference to the
    > > > > > > > > > ' application and spreadsheet.
    > > > > > > > > > End Sub
    > > > > > > > > >
    > > > > > > > > > Sub DetectExcel()
    > > > > > > > > > ' Procedure dectects a running Excel and registers it.
    > > > > > > > > > Const WM_USER = 1024
    > > > > > > > > > Dim hWnd As Long
    > > > > > > > > > ' If Excel is running this API call returns its handle.
    > > > > > > > > > hWnd = FindWindow("XLMAIN", 0)
    > > > > > > > > > If hWnd = 0 Then ' 0 means Excel not running.
    > > > > > > > > > Exit Sub
    > > > > > > > > > Else
    > > > > > > > > > ' Excel is running so use the SendMessage API
    > > > > > > > > > ' function to enter it in the Running Object Table.
    > > > > > > > > > SendMessage hWnd, WM_USER + 18, 0, 0
    > > > > > > > > > End If
    > > > > > > > > > End Sub
    > > > > > > > > >
    > > > > > > > > >
    > > > > > > > >
    > > > > > > > >
    > > > > > > > >
    > > > > > >
    > > > > > >
    > > > > > >
    > > > >
    > > > >
    > > >
    > > >
    > > >

    >
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1