+ Reply to Thread
Results 1 to 9 of 9

Bring Excel to foreground from VBA

  1. #1
    Registered User
    Join Date
    06-08-2010
    Location
    INDIA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Bring Excel to foreground from VBA

    Hi,
    I am trying to force Excel window as foregrond from VBA window using some win32 API.But this is not helping out.I followed the steps below.

    1.Open Excel
    2.Press ALT+F11 to open VBA window
    3.Double click the ThisWorkbook and paste the following code in the coding area

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

    Public Sub Test()
    Dim exlHandle As Long
    Dim exlTitle As String
    exlTitle = Application.Caption
    exlHandle = FindWindow(vbNullString, exlTitle)
    SetForegroundWindow (exlHandle)
    End Sub

    Call the method Test from VBA immediate window like below and press enter key there.

    call ThisWorkbook.Test

    It suppose to set Excel as forground but not.Any suggestion?

    Regards,
    Rajanbabu

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Bring Excel to foreground from VBA

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    From a post by Jim Rech.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    06-08-2010
    Location
    INDIA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Bring Excel to foreground from VBA

    Thanks for the reply.But this is also not helping out.
    My need is either one of the below
    1.Set excel as foreground window or
    2.Set VBA as background window

    -Rajanbabu

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Bring Excel to foreground from VBA

    No need to quote my post. It just waste space.

    The code works for me.
    What happens for you. Code runs but does not bring window to foreground.
    Or does it error?
    What if you put a breakpoint in the code does it even run?

  5. #5
    Registered User
    Join Date
    06-08-2010
    Location
    INDIA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Bring Excel to foreground from VBA

    I kept breakpoint in method Sub Test2() and pressed F5 button.But it is throwing error like below
    "The macro 'Book1!BringTotop' can not be found.

    but i kept breakpoint in the method BringTotop() and run over.But didnt set Excel as foreground

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Bring Excel to foreground from VBA

    this workbook works for me.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    06-08-2010
    Location
    INDIA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Bring Excel to foreground from VBA

    Excel is comming foreground window for you?
    I tried your workbook.But still VBA is in foreground.

    I just kept breakpoint in Sub Test2() then pressed F5.This will hit the breakpoint.Now I removed the breakpoint then again pressed F5.Run completed.But VBA is not in foreground.If it is minimised,It is just restoring the Excel window in background.

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Bring Excel to foreground from VBA

    Maybe somebody else can test the workbook because it works for me.

  9. #9
    Registered User
    Join Date
    06-08-2010
    Location
    INDIA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Bring Excel to foreground from VBA

    I tried the same steps in my friend machine which is having Excel 2003,SP2.But still seeing the same behavior.

+ 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