Excel workbook doesn't get focus upon opening

  • Thread starter Bill R via AccessMonster.com
  • Start date
B

Bill R via AccessMonster.com

I have the following code at the end of a procedure that pastes new data in
an Excel workbook:

Set objXL = New excel.Application
With objXL
.Visible = True
Set objWkb = .Workbooks.Open(strPath)
objWkb.Save
End With

Set objWkb = Nothing
Set objXL = Nothing

It works fine, except, now that the company upgraded me to Excel 2003, the
focus remains in Access after the code runs. In Excel 2000, after the code
ran, the Excel workbook would open and get the focus. This is problematic
because sometimes you'll get a dialog (refresh links, etc.) when the workbook
opens, but that's not apparent unless the workbook has the focus.

Bill
 
D

David Lloyd

Bill:

The AppActivate method can be used to give the Excel application the focus.
This method seems to be sensitive to how it is used. For example, I tested
the method without an error handler and although it executed properly, I
received an error message back in Access. With error handling in place, no
error is generated.

Function OpenWorkbook()

Dim objXL As Object
Dim objwkb As Object
Dim strPath As String

On Error GoTo Errorhandler

strPath = "h:\MyWorkbook.xls"

Set objXL = New Excel.Application
With objXL
.Visible = True
Set objwkb = .Workbooks.Open(strPath)
objwkb.Save
AppActivate "MyWorkbook.xls", True
End With

Function_Exit:
Set objwkb = Nothing
Set objXL = Nothing

Exit Function

Errorhandler:
If Err.Number <> 0 Then
MsgBox "Error Number: " & Err.Number & vbCrLf & "Description: " &
Err.Description
Resume Function_Exit
End If
End Function

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I have the following code at the end of a procedure that pastes new data in
an Excel workbook:

Set objXL = New excel.Application
With objXL
.Visible = True
Set objWkb = .Workbooks.Open(strPath)
objWkb.Save
End With

Set objWkb = Nothing
Set objXL = Nothing

It works fine, except, now that the company upgraded me to Excel 2003, the
focus remains in Access after the code runs. In Excel 2000, after the code
ran, the Excel workbook would open and get the focus. This is problematic
because sometimes you'll get a dialog (refresh links, etc.) when the
workbook
opens, but that's not apparent unless the workbook has the focus.

Bill
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top