VBA to set focus back to exel

Discussion in 'Microsoft Excel Programming' started by Waxaholic, Jan 14, 2008.

  1. Waxaholic

    Waxaholic Guest

    I am launching an external application from excel and need to obtain
    control of excel after the external program launches. This is all done
    via VBA. VBA wants to wait for the installation to finish before
    giving me excel back. I need to overcome this and have immediate
    control of excel so i can close the workbook. Any ideas.

    thanks

    Brian
     
    Waxaholic, Jan 14, 2008
    #1
    1. Advertisements

  2. Waxaholic

    RB Smissaert Guest

    Try this:

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

    Sub XLFocus()

    Dim lExcelHwnd As Long

    lExcelHwnd = FindWindow("XLMAIN", Application.Caption)
    SetForegroundWindow lExcelHwnd

    End Sub


    RBS


    "Waxaholic" <> wrote in message
    news:...
    >I am launching an external application from excel and need to obtain
    > control of excel after the external program launches. This is all done
    > via VBA. VBA wants to wait for the installation to finish before
    > giving me excel back. I need to overcome this and have immediate
    > control of excel so i can close the workbook. Any ideas.
    >
    > thanks
    >
    > Brian
     
    RB Smissaert, Jan 14, 2008
    #2
    1. Advertisements

  3. Untested...

    AppActivate Application.Caption

    Waxaholic wrote:
    >
    > I am launching an external application from excel and need to obtain
    > control of excel after the external program launches. This is all done
    > via VBA. VBA wants to wait for the installation to finish before
    > giving me excel back. I need to overcome this and have immediate
    > control of excel so i can close the workbook. Any ideas.
    >
    > thanks
    >
    > Brian


    --

    Dave Peterson
     
    Dave Peterson, Jan 14, 2008
    #3
  4. Waxaholic

    Waxaholic Guest

    Thanks to all. I have tried both recommendations above and they do
    work, however, they are real slow (about 30sec or more) in returning
    focus. This is way too long and would allow the end user to screw
    things up. I am trying to get an instantaneous return of focus as soon
    as the external app launches.

    What i have is some vba that checks for an update to the excel
    workbook. It then allows the user to download the update and the
    additional option of installing it. This is where i launch the
    installer and want to return to excel and close the workbook before
    the user can continue with the installer.
     
    Waxaholic, Jan 15, 2008
    #4
  5. I don't have another suggestion.

    Waxaholic wrote:
    >
    > Thanks to all. I have tried both recommendations above and they do
    > work, however, they are real slow (about 30sec or more) in returning
    > focus. This is way too long and would allow the end user to screw
    > things up. I am trying to get an instantaneous return of focus as soon
    > as the external app launches.
    >
    > What i have is some vba that checks for an update to the excel
    > workbook. It then allows the user to download the update and the
    > additional option of installing it. This is where i launch the
    > installer and want to return to excel and close the workbook before
    > the user can continue with the installer.


    --

    Dave Peterson
     
    Dave Peterson, Jan 15, 2008
    #5
  6. Waxaholic

    RB Smissaert Guest

    Maybe you could post your code.

    RBS


    "Waxaholic" <> wrote in message
    news:...
    > Thanks to all. I have tried both recommendations above and they do
    > work, however, they are real slow (about 30sec or more) in returning
    > focus. This is way too long and would allow the end user to screw
    > things up. I am trying to get an instantaneous return of focus as soon
    > as the external app launches.
    >
    > What i have is some vba that checks for an update to the excel
    > workbook. It then allows the user to download the update and the
    > additional option of installing it. This is where i launch the
    > installer and want to return to excel and close the workbook before
    > the user can continue with the installer.
    >
     
    RB Smissaert, Jan 15, 2008
    #6
  7. Waxaholic

    Waxaholic Guest

    Version 1:
    Sub Launcher()
    Dim lExcelHwnd As Long

    lExcelHwnd = FindWindow("UMR Creator v0_32wu", Application.Caption)

    Shell ThisWorkbook.Path & "\" & Sheets("MenuSheet").Range("K2") &
    ".exe", vbNormalNoFocus

    SetForegroundWindow lExcelHwnd

    End Sub

    Version 2:
    ----------------------------------------------------
    Sub Launcher2()

    Dim MyAppID, ReturnValue
    Dim UMRCID As Long

    MyAppID = ("UMR Creator v0_32wu")

    UMRCID = Shell(ThisWorkbook.Path & "\" &
    Sheets("MenuSheet").Range("K2") & ".exe", 6)
    AppActivate MyAppID, 0

    End Sub


    Both of these work for me but the delay coming back to excel is way
    too long.



    On Jan 15, 1:38 am, "RB Smissaert" <>
    wrote:
    > Maybe you could post your code.
    >
    > RBS
    >
    > "Waxaholic" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > Thanks to all. I have tried both recommendations above and they do
    > > work, however, they are real slow (about 30sec or more) in returning
    > > focus. This is way too long and would allow the end user to screw
    > > things up. I am trying to get an instantaneous return of focus as soon
    > > as the external app launches.

    >
    > > What i have is some vba that checks for an update to the excel
    > > workbook. It then allows the user to download the update and the
    > > additional option of installing it. This is where i launch the
    > > installer and want to return to excel and close the workbook before
    > > the user can continue with the installer.- Hide quoted text -

    >
    > - Show quoted text -
     
    Waxaholic, Jan 15, 2008
    #7
  8. Waxaholic

    RB Smissaert Guest

    A few things:

    What is this: "UMR Creator v0_32wu" ?
    Did you try with "XLMAIN" ?

    Try a DoEvents before SetForegroundWindow lExcelHwnd


    RBS


    "Waxaholic" <> wrote in message
    news:...
    > Version 1:
    > Sub Launcher()
    > Dim lExcelHwnd As Long
    >
    > lExcelHwnd = FindWindow("UMR Creator v0_32wu", Application.Caption)
    >
    > Shell ThisWorkbook.Path & "\" & Sheets("MenuSheet").Range("K2") &
    > ".exe", vbNormalNoFocus
    >
    > SetForegroundWindow lExcelHwnd
    >
    > End Sub
    >
    > Version 2:
    > ----------------------------------------------------
    > Sub Launcher2()
    >
    > Dim MyAppID, ReturnValue
    > Dim UMRCID As Long
    >
    > MyAppID = ("UMR Creator v0_32wu")
    >
    > UMRCID = Shell(ThisWorkbook.Path & "\" &
    > Sheets("MenuSheet").Range("K2") & ".exe", 6)
    > AppActivate MyAppID, 0
    >
    > End Sub
    >
    >
    > Both of these work for me but the delay coming back to excel is way
    > too long.
    >
    >
    >
    > On Jan 15, 1:38 am, "RB Smissaert" <>
    > wrote:
    >> Maybe you could post your code.
    >>
    >> RBS
    >>
    >> "Waxaholic" <> wrote in message
    >>
    >> news:...
    >>
    >>
    >>
    >> > Thanks to all. I have tried both recommendations above and they do
    >> > work, however, they are real slow (about 30sec or more) in returning
    >> > focus. This is way too long and would allow the end user to screw
    >> > things up. I am trying to get an instantaneous return of focus as soon
    >> > as the external app launches.

    >>
    >> > What i have is some vba that checks for an update to the excel
    >> > workbook. It then allows the user to download the update and the
    >> > additional option of installing it. This is where i launch the
    >> > installer and want to return to excel and close the workbook before
    >> > the user can continue with the installer.- Hide quoted text -

    >>
    >> - Show quoted text -

    >
     
    RB Smissaert, Jan 15, 2008
    #8
  9. In launcher2, use this:
    AppActivate Application.Caption
    (don't change it)



    Waxaholic wrote:
    >
    > Version 1:
    > Sub Launcher()
    > Dim lExcelHwnd As Long
    >
    > lExcelHwnd = FindWindow("UMR Creator v0_32wu", Application.Caption)
    >
    > Shell ThisWorkbook.Path & "\" & Sheets("MenuSheet").Range("K2") &
    > ".exe", vbNormalNoFocus
    >
    > SetForegroundWindow lExcelHwnd
    >
    > End Sub
    >
    > Version 2:
    > ----------------------------------------------------
    > Sub Launcher2()
    >
    > Dim MyAppID, ReturnValue
    > Dim UMRCID As Long
    >
    > MyAppID = ("UMR Creator v0_32wu")
    >
    > UMRCID = Shell(ThisWorkbook.Path & "\" &
    > Sheets("MenuSheet").Range("K2") & ".exe", 6)
    > AppActivate MyAppID, 0
    >
    > End Sub
    >
    > Both of these work for me but the delay coming back to excel is way
    > too long.
    >
    > On Jan 15, 1:38 am, "RB Smissaert" <>
    > wrote:
    > > Maybe you could post your code.
    > >
    > > RBS
    > >
    > > "Waxaholic" <> wrote in message
    > >
    > > news:...
    > >
    > >
    > >
    > > > Thanks to all. I have tried both recommendations above and they do
    > > > work, however, they are real slow (about 30sec or more) in returning
    > > > focus. This is way too long and would allow the end user to screw
    > > > things up. I am trying to get an instantaneous return of focus as soon
    > > > as the external app launches.

    > >
    > > > What i have is some vba that checks for an update to the excel
    > > > workbook. It then allows the user to download the update and the
    > > > additional option of installing it. This is where i launch the
    > > > installer and want to return to excel and close the workbook before
    > > > the user can continue with the installer.- Hide quoted text -

    > >
    > > - Show quoted text -


    --

    Dave Peterson
     
    Dave Peterson, Jan 15, 2008
    #9
  10. Waxaholic

    Waxaholic Guest

    Thanks to both of you but i still get the same result in slowness.

    The "UMR Creator v0_32wu" is the window handle for the excel workbook.
    I played with this and tried using XLMAIN, as well as a few different
    variations in code and no speed increase. I am thinking of another
    approach like launching a helper xls file with the Launcher code in
    it. This would allow me to close the existing workbook without
    problem. I'll report back if it works.
     
    Waxaholic, Jan 15, 2008
    #10
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Les
    Replies:
    0
    Views:
    527
  2. Stephen Bullen

    Re: How to set focus back to sheet after showing a user form

    Stephen Bullen, Jul 22, 2003, in forum: Microsoft Excel Programming
    Replies:
    0
    Views:
    280
    Stephen Bullen
    Jul 22, 2003
  3. Timse

    Exel VBA - Clearing clipboard, area names

    Timse, Jun 11, 2004, in forum: Microsoft Excel Programming
    Replies:
    2
    Views:
    300
    papou
    Jun 11, 2004
  4. Timse

    Exel VBA - Sheets.Name

    Timse, Jun 18, 2004, in forum: Microsoft Excel Programming
    Replies:
    2
    Views:
    10,203
    Timse
    Jun 18, 2004
  5. Tetsuya Oguma

    How can I set focus back or remained on Textbox?

    Tetsuya Oguma, Mar 5, 2008, in forum: Microsoft Excel Programming
    Replies:
    2
    Views:
    288
    Susan
    Mar 5, 2008
Loading...

Share This Page