Switch Focus to Excel with Access VBA Automation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BikeToWork
    New Member
    • Jan 2012
    • 124

    Switch Focus to Excel with Access VBA Automation

    Hi, I am trying to figure out some way to open an Excel workbook from Access vba and have the focus automatically shift to Excel. What happens is is that the Excel workbook shows up as a blinking Excel icon in the taskbar. I call the following code from Access to open the Excel workbook.

    Code:
    Function OpenExcel(strFileName As String)
    
    Dim XL As Excel.Application
    Dim WKB As New Excel.Workbook
    
    Set XL = New Excel.Application
    XL.Visible = True
    
    Set WKB = XL.Workbooks.Open(strFileName)
    WKB.Activate
    
    'WKB.Close SaveChanges:=False
    
    
    Set WKB = Nothing
    Set XL = Nothing
    
    
    End Function
    Thanks in advance for any advice.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Try inserting etween lines 6 and 7:
    xl.UserControl = True

    I have a similar code that dumps stuff out to excel using basically Line 6, myline, 7 and the focus is left on the worksheet... (I also have code to select range a1 too)

    Let us know if this works.

    (you might take a look a this too: 163. Application Automation starts with Outlook; however, has an excel example too.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      If you are really stuck, you can use the API to retrieve the Window's Handle in the Task Bar, then Maximize it.

      Comment

      • cjswimmer
        New Member
        • Mar 2019
        • 1

        #4
        I know this is a really old thread but I found a solution and thought I should share. You can use
        Code:
        AppActivate XL.Windows(1).Caption

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Nice. I'll go ahead and set that to Best Answer.

          I'm sure the other suggestions also work but this is exactly what was requested.

          Comment

          Working...