473,385 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Application-defined error when populating cells in late-bound Excel automation object

topher23
234 Expert 100+
I have an Access subroutine that needs to generate an Excel spreadsheet for accounting. I've used late-bound Excel automation before, but for some reason I keep running into a strange issue when I run this one.

This is the relevant portion of the procedure:
Expand|Select|Wrap|Line Numbers
  1. Private Sub GenerateExplodedBOM()
  2. 'On Error GoTo Err_Handler
  3. Dim appXL As Object
  4. Dim wbBOM As Object
  5. Dim rs As New adodb.Recordset
  6.  
  7.     Set appXL = CreateObject("Excel.Application")
  8.     appXL.Visible = True
  9.     Set wbBOM = appXL.workbooks.Add
  10.     wbBOM.Activate
  11.     wbBOM.sheets.Add Before:=wbBOM.sheets(1)
  12.     wbBOM.sheets(1).name = Forms!PDC_home!ItemNum & " Exploded BOM"
  13.     wbBOM.sheets(1).Select
  14.     rs.Open "SELECT * FROM tblProduct WHERE ItemNum = '" & _
  15.  Forms!PDC_home!ItemNum & "'", CurrentProject.Connection, _
  16.  adOpenForwardOnly
  17.     With wbBOM.sheets(1)
  18.         .cells(1, 1).value = rs!ItemNum
  19.         .cells(1, 2).value = rs!PurchaseDescription
  20.         .cells(1, 3).value = "1"
  21.         .cells(1, 4).value = CCur(rs!BOMtotal)
  22.     End With
  23.  
  24.     rs.Close
  25.     Set rs = Nothing
  26.  
When I run this, I always get "Application-defined error" when I hit ".cells(1, 1).value = rs!ItemNum" - but the crazy part is that, after the code breaks, I can hit "Run" and it continues right through, no problems. Any ideas on why this happens and how to work around it? Google hasn't been very helpful on this.
Feb 21 '14 #1

✓ answered by NeoPa

FWIW the addition of a DoEvents typically indicates that correct working of the code depends on something catching up in the background. Either OS or Application code somewhere. By its very nature this is always difficult to tie down. Knowing it may be a problem is very helpful though. It means you can shove that one line in and get everything to work ;-)

6 1361
topher23
234 Expert 100+
Grrr! I just tacked a
Expand|Select|Wrap|Line Numbers
  1. DoEvents
before the With block and now it works. Of all the ridiculous kludges!
Feb 21 '14 #2
ADezii
8,834 Expert 8TB
  1. Why Late Binding?
  2. Why the Sheets Collection instead of the Worksheets Collection?
  3. Index 1 of the Worksheets/Sheets Collection has a special meaning in Excel - why not use .Activate/ActiveSheet instead?
Feb 22 '14 #3
topher23
234 Expert 100+
1. Late binding because there are several different versions of Excel in use at our company, so this eliminates broken references.
2. I used to use Worksheets more often, but the Sheets collection allows direct reference to worksheets, chart "sheets", and modules (instead of just standard worksheets), so I tend to use that instead when coding.
3. I've had Activesheet give me unpredictable results with automation in the past, particularly when using .Visible=False. I feel more comfortable giving a specific sheet reference.

FWIW, whether I referenced Sheets(1) or Sheets("[ItemNum] Exploded BOM"), the application still threw the same error when I tried to write the cell values. I also tried with .Activate/ActiveSheet - still the same error. I'm not sure what Excel needs to process before it changes those cell values, but DoEvents lets it happen.
Feb 22 '14 #4
ADezii
8,834 Expert 8TB
Thanks Topher23, just wanted to satisfy my own curiosity.
the Sheets collection allows direct reference to worksheets, chart "sheets", and modules
What exactly do you mean by direct reference to modules?
Feb 22 '14 #5
topher23
234 Expert 100+
Hmmm, looks like I may have been wrong about modules. I remembered reading in the past that you could use Sheets([modulename]) just like Modules([modulename]) in Access, but I just tried it and it didn't work. Further reading leads me to the belief that my memory on that may have been faulty. The Sheets collection only consists of the objects that show up as tabs in the GUI, which can also be referenced through the collections relating to their specific object types: Worksheets(), Charts(). So if tab 1 is a chart and tab 2 is a worksheet, tab 2 would be Worksheets(1) but Sheets(2).
Feb 24 '14 #6
NeoPa
32,556 Expert Mod 16PB
FWIW the addition of a DoEvents typically indicates that correct working of the code depends on something catching up in the background. Either OS or Application code somewhere. By its very nature this is always difficult to tie down. Knowing it may be a problem is very helpful though. It means you can shove that one line in and get everything to work ;-)
Feb 24 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Juan | last post by:
When populating a dataset I get an error if the data being retrieved includes a string field with a Null value: "Not valid store type: DBNull". What kinf of data type can i define in the dataset to...
1
by: Matt Dinovo | last post by:
I have a remoting server that is on the same local box as a COM+ managed object with Library Activation. When I try to activate the object from the remoted assembly, I get the following error: "The...
7
by: Franck | last post by:
Hi, I'm using an xls file through my web service to print out a pdf file from which I returned path. In this way, got a macro in my XLS file which do the print out. Testing it from the xls file...
6
by: Steve Richter | last post by:
I am getting error in a vbscript: ActiveX component cant create object: Excel.Application. The vbscript code is: Dim objExcel Set objExcel = CreateObject("Excel.Application") I am pretty...
12
by: elziko | last post by:
I'm using late binding (I must) to automate Excel. My code opens Excel after createing and poulating some sheets. My problem is that when the user finally decides to close Excel its process is...
2
by: Lenonardo | last post by:
Hi. I'm writing a VB.Net application to update multiple Excel Worksheets. I'm using late binding (i.e. all variables are objects + use createobject) I develop the application on an XP...
4
by: goonsquad | last post by:
I'm writing an Excel Automation app and I'm running to a problem early on with what should be a basic step. Dim oExcel As Object Dim oBook As Object Dim oSheet As Object Dim oRng As...
6
by: a.theil | last post by:
Please help! I need a simple excel automation, just 2 write some files into excel. I do: Dim oXL As Excel.Application Dim oWB As Excel.Workbook Dim oSheet As Excel.Worksheet Dim oRng As...
0
by: mward | last post by:
I just upgraded my Office 2003 to SP2 and am having problems with my Excel automation code from C#. The version of Excel.exe that I'm now using is 11.0.8033.0. It appears as though the object...
2
by: MarkBStewart | last post by:
I am trying to create borders in an excel spreadsheet programaticaly using VBA. I am getting an object error on line 2 Selection.Borders(xlDiagonalDown... This is my code: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.