Solved

CreateObject("Excel.Application") Failure

Posted on 2013-06-14
7
474 Views
Last Modified: 2013-06-19
Hello,
Using VBA in Access 2007, I'm trying to create an Excel workbook with 2 sheets. The first sheet is created from a query, the second sheet "Result" requires a lot of formatting.
The problem is that the second sheet does not get created, the following code executes from the Event Procedure of a button:
Private Sub Command0_Click()
On Error GoTo errHdl
Dim xlApp As Object, path As String, fileName As String
Dim xlWB As Object, xlWS As Object, xlRes As Object
Dim lst As Integer, ctr As Integer, arr() As String, arrCtr As Integer, arrObj() As String
arrCtr = 0

start:
Set xlApp = CreateObject("Excel.Application")

path = Mid(CurrentDb.Name, 1, InStrRev(CurrentDb.Name, "\"))

If radAll.Value = -1 Then
fileName = "All Clients"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryAcct_Pos_All_Client", path & fileName & ".xls", True
Else
fileName = cmbClient.Value
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryAcct_Pos_Client", path & fileName & ".xls", True
End If

xlApp.Visible = True
xlApp.workbooks.Open path & fileName & ".xls"

If radAll.Value = -1 Then

Set xlWB = xlApp.workbooks(fileName)
Set xlWS = xlWB.Worksheets("qryAcct_Pos_All_Client")

If radAll.Value = -1 Then
With xlApp
.workbooks(fileName).Worksheets.Add After:=xlApp.Worksheets(1)
.activesheet.Name = "Result"
.workbooks(fileName).Worksheets(1).Select
End With

lst = xlApp.worksheetfunction.counta(xlWS.Range("A:A")) - 1

Open in new window


Then there is a lot of formatting that I won't bore you with, ending with:
xlWB.Save
xlApp.screenupdating = True

Set xlApp = Nothing

Exit Sub
errHdl:
If Err.Number = 1004 Then
k = MsgBox("All Clients file already exist in the folder. Do you want to replace it", vbYesNo)
If k = 6 Then
xlWB.Close False
Kill path & fileName & ".xls"
GoTo start
ElseIf k = 7 Then
xlWB.Close False
Exit Sub
End If
End If
End Sub

Open in new window

The code creates the 'qry' sheet correctly and then the Excel window pops-up but without the second 'Result' sheet.
 No error is thrown.
I appreciate any help,
Thanks
0
Comment
Question by:stoneycurtis
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 39247871
What is radAll and what's it's value?

Have you tried stepping through the code with F8 and checking what's happening?

Is the code to add the worksheet actually being executed?
0
 

Author Comment

by:stoneycurtis
ID: 39248048
@imnorie
radAll is a radio checkbox and its value is -1.
The workbook 'All Clients.xls' is created with one sheet, 'qryAcct_Pos_All_Client'.

xlApp.Visible = True
xlApp.workbooks.Open path & fileName & ".xls"

If radAll.Value = -1 Then  

Open in new window

     
(The code stops working here)
0
 
LVL 33

Expert Comment

by:Norie
ID: 39248069
When it stops working what's happening with Excel?

Also, are there any error messages?

PS Why are you checking radAll multiple times?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:stoneycurtis
ID: 39248159
@imnorie
When the code stops, an Excel window pops-up displaying the 'All Clients' workbook and showing the only sheet.

There are no error messages.

I tried to eliminate the number of times the code checked for radAll values but it didn't make any difference. The workbook still only printed with one sheet.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39248253
Here's the order to open and close Excel:

    Dim xls     As Excel.Application
    Dim wkb     As Excel.Workbook
    Dim wks     As Excel.Worksheet
   
    Set xls = New Excel.Application
    Set wkb = xls.Workbooks.Open("c:\test\workbook1.xlsx")
    Set wks = wkb.Worksheets(1)
   
    wks.Name = "My New Name"
    wkb.Close True
   
    Set wks = Nothing
    Set wkb = Nothing
   
    xls.Quit
   
    Set xls = Nothing

You _MUST_ be very specific to this. If you set your xlApp to Nothing, its feet are kicked away.

/gustav
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 39248298
Also, this won't Work:

With xlApp
.workbooks(fileName).Worksheets.Add After:=xlApp.Worksheets(1)
.activesheet.Name = "Result"
.workbooks(fileName).Worksheets(1).Select
End With

It should look like:

Set xlWB = xlApp.WorkBooks(fileName)
Set xlWS = xlWB.WorkSheets("qryAcct_Pos_All_Client")

Set xlWSnew = xlWB.WorkSheets.Add After:=xlWB.Worksheets(1)
xlWSnew.Name = "Result"
xlWB.WorkSheets(1).Select

etc.
Always state the explicit object, and always open and close carefully.

Further, except if you wish to view the formatting etc. taking place, there's no reason to Select anything, neither to make Excel visible - it only slows things down.

/gustav
0
 
LVL 26

Expert Comment

by:Nick67
ID: 39249351
I am with @cactus_data on this one, I think the organization of your sub plays into a bit of your problems.

I'd get the xlApp,  xlWB  and xlWS  declared and set FIRST
I'd then walk through adding the additional sheets you want
Set xlWS = obook.Worksheets.Add
osheet.Move After:=obook.Worksheets(3) 'or whatever number

Open in new window

Once you have your sheets, add data to them
Being an Access guy, I like
Dim db as database
Dim rs as recordset
Set db = Currentdb
Set rs = db.OpenRecordset("Some SQL string, or the name of a saved query",dbOpenDynaset, dbSeeChanges)
xlWS.Range("A2").CopyFromRecordset rs

Open in new window

much better than TransferSpreadsheet, but your mileage may vary

Add data to each sheet in turn
Then format each sheet in turn.

You may find that when you think about re-factoring your code, you could break it into chunks: a sub to create the workbook and add the required sheets, a sub to add data to the sheets, and a sub to format the sheets.

It might be more maintainable and easier to read and de-bug
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Access 2016 Debugging 7 42
bind Combobox 4 29
Syntax error [Missing Operator] in query expression 7 24
Is it possible to reset DSum? 12 41
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now