If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Find instances of Excel in VBA?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-16-06, 05:29
disruptivehair disruptivehair is offline
Registered User
 
Join Date: Dec 2005
Location: Texas
Posts: 100
Find instances of Excel in VBA?

I've got some code that exports recordsets from Access into Excel spreadsheets.

Every time the user exports a recordset, the program creates a new instance of Excel because this is what I've told it to do; I don't know how to ask it to check for existing instances of Excel and to use an existing instance if it's present or to create a new one if there's none present.

Nobody around me knows how to do this and I haven't found anything on the net that works; can anyone help?

Thanks!

Reply With Quote
  #2 (permalink)  
Old 06-22-06, 03:42
disruptivehair disruptivehair is offline
Registered User
 
Join Date: Dec 2005
Location: Texas
Posts: 100
I've still been searching on the net, haven't found anything yet...any takers?
Reply With Quote
  #3 (permalink)  
Old 06-22-06, 14:28
norie norie is offline
Registered User
 
Join Date: Mar 2006
Posts: 163
Can you post your current code?
Reply With Quote
  #4 (permalink)  
Old 06-28-06, 09:40
disruptivehair disruptivehair is offline
Registered User
 
Join Date: Dec 2005
Location: Texas
Posts: 100
Quote:
Originally Posted by norie
Can you post your current code?

This is a complete and utter hack; it seems to work but it's very unstable. I was hoping there was a considerably more elegant way to do this. The sub assumes an instance of Excel exists and if it doesn't it errors and creates one but the error is not seen by the user.

Public Sub testexcel()

On Error GoTo TestExcel_Err

Dim db As Database
Dim rst As ADODB.Recordset

Dim strSource As String
Dim i As Integer

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set db = CurrentDb()

strSource = DLookup("AccessObject", "tblReports", "ReportName='" & _
gVarReportSelected & "'")

Set rst = New ADODB.Recordset
rst.Open Source:=strSource, ActiveConnection:="Provider=Microsoft.Jet.OLEDB.4. 0; " & _
"Data Source=" & CurrentDb.Name

Set xlApp = GetObject(, "Excel.Application")

testexcel:
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.ActiveSheet

For i = 0 To rst.Fields.Count - 1
With xlSheet
.Cells(1, i + 1).Value = rst.Fields(i).Name
.Range("A2").CopyFromRecordset rst
.Columns(i + 1).AutoFit
End With
Next i

xlApp.Visible = True

TestExcel_Err:
Select Case Err.Number
Case 429
Set xlApp = CreateObject("Excel.Application")
Resume testexcel
Case Else
Exit Sub
End Select


End Sub
Reply With Quote
  #5 (permalink)  
Old 06-30-06, 09:34
disruptivehair disruptivehair is offline
Registered User
 
Join Date: Dec 2005
Location: Texas
Posts: 100
Nevermind...2 weeks later, I've found the answer in O'Reilly's Access Cookbook, chapter 11.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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