Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Open Excel workbook from Access

I am trying to automate the opening of an excel workbook from by
access database - both 2003. I am using the following but am
running into a problem when excel is not running:

Sub ExcelOpen()

Dim xlApp As Excel.Application
Dim xlWkb As Excel.Workbook
Dim xlWsh As Excel.Worksheet


On Error Resume Next
' reference open session of excel
Set xlApp = GetObject(, "excel.application")
If Err.Number < 0 Then
' excel not already running
Err.Clear
On Error GoTo 0
Set xlApp = New Excel.Application
End If


Set xlWkb = xlApp.Workbooks.Open("C:\My Documents\2008
TRACKING\RPSReporting (Open-Closed-Targets).xls")
Set xlWsh = xlWkb.Worksheets("Main")

With xlApp
If Not .UserControl Then
' opened excel using code
..Quit
End If
End With


Set xlWkb = Nothing
Set xlApp = Nothing

End Sub


If excel is running, it opens the workbook without
a problem, but if excel is not running it opens and closes
the workbook. I know it must be this part of the code:

With xlApp
If Not .UserControl Then
' opened excel using code
..Quit
End If
End With

but how do I get it not to close? I am new to this type of
coding and not sure how to get around this.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Open Excel workbook from Access

Some suggestions...
Add two lines as follows...
Set xlApp = New Excel.Application
xlApp.Visible = True '<<<<
End If
On Error GoTo 0 '<<<<
'--
Comment out the usercontrol portion...
'With xlApp
'If Not .UserControl Then
' opened excel using code
'.Quit
'End If
'End With
'--
Set the worksheet reference to Nothing...
Set xlWsh = Nothing
Set xlWkb = Nothing
Set xlApp = Nothing
--
Jim Cone
Portland, Oregon USA



"Opal"
wrote in message
I am trying to automate the opening of an excel workbook from by
access database - both 2003. I am using the following but am
running into a problem when excel is not running:

Sub ExcelOpen()
Dim xlApp As Excel.Application
Dim xlWkb As Excel.Workbook
Dim xlWsh As Excel.Worksheet

On Error Resume Next
' reference open session of excel
Set xlApp = GetObject(, "excel.application")
If Err.Number < 0 Then
' excel not already running
Err.Clear
On Error GoTo 0
Set xlApp = New Excel.Application
End If

Set xlWkb = xlApp.Workbooks.Open("C:\My Documents\2008
TRACKING\RPSReporting (Open-Closed-Targets).xls")
Set xlWsh = xlWkb.Worksheets("Main")

With xlApp
If Not .UserControl Then
' opened excel using code
..Quit
End If
End With

Set xlWkb = Nothing
Set xlApp = Nothing
End Sub

If excel is running, it opens the workbook without
a problem, but if excel is not running it opens and closes
the workbook. I know it must be this part of the code:
With xlApp
If Not .UserControl Then
' opened excel using code
..Quit
End If
End With

but how do I get it not to close? I am new to this type of
coding and not sure how to get around this.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Open Excel workbook from Access

Thank you Jim, that is very helpful. One more question, I thought the
line:

Set xlWsh = xlWkb.Worksheets("Main")

would open the workbook on the tab labeled Main, but I have been
testing it and its not. Any suggestions?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Open Excel workbook from Access

The Set statement establishes a reference to the sheet.
You can then use that reference in your code.
If you want to display the sheet then try...

xlWsh.Activate
--
Jim Cone
Portland, Oregon USA



"Opal"
wrote in message
....
Thank you Jim, that is very helpful. One more question, I thought the
line:
Set xlWsh = xlWkb.Worksheets("Main")
would open the workbook on the tab labeled Main, but I have been
testing it and its not. Any suggestions?
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Open Excel workbook from Access

On Nov 5, 12:26*pm, "Jim Cone" wrote:
The Set statement establishes a reference to the sheet.
You can then use that reference in your code.
If you want to display the sheet then try...

xlWsh.Activate
--
Jim Cone
Portland, Oregon *USA

"Opal"
wrote in message
...
Thank you Jim, that is very helpful. *One more question, I thought the
line:
Set xlWsh = xlWkb.Worksheets("Main")
would open the workbook on the tab labeled Main, but I have been
testing it and its not. *Any suggestions?


Thank you, that's very helpful.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening Excel, Book1 opens, remains open with other workbook open DanieB Excel Discussion (Misc queries) 0 September 3rd 09 08:23 AM
Access Data from Workbook without seeing the workbook open. RyanH Excel Programming 1 August 29th 08 04:23 PM
Open a specific workbook...find value from other open workbook and then insert cells values in cell next to it. [email protected] Excel Programming 1 May 13th 07 01:46 PM
excel 2003 saved file will not open without a blank workbook open Bob Excel Discussion (Misc queries) 4 November 11th 06 04:24 PM
Open excel workbook in access John[_46_] Excel Programming 1 August 25th 03 03:26 PM


All times are GMT +1. The time now is 09:20 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"