473,387 Members | 1,440 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,387 software developers and data experts.

Runtime-error 91 when openning Excel Pivot Table for the 2nd time

Hi Guys,

I have code for click event on an Access form that opens an Excel file, refreshes data of a pivot table on the active worksheet and then filters records based on a criteria. Everything works fine if I open Access for the first time and click the button to run my code. However, if I close the Excel file and then reclick the button that runs my code I receive a debug message saying "Run-time error 91, Object variable or with block not set." Here is my code. The code halts at: Set pt = ActiveSheet.PivotTables("PivotTable3"). Any help would be greatly appreciated. Thanks!
Expand|Select|Wrap|Line Numbers
  1. Dim appExcel As Excel.Application
  2. Dim wkb As Excel.Workbook
  3. Dim strFile As String
  4. Dim pt As PivotTable
  5.  
  6. Set appExcel = New Excel.Application
  7. strFile = "\\pwrutc\wrkgrp\Operations\Facilities Plant Operations\CapitalPlanningDB\TomTest.xls"
  8. Set wkb = appExcel.Workbooks.Open(strFile)
  9. appExcel.Visible = True 'Do something with worksheet
  10.  
  11.    Set pt = ActiveSheet.PivotTables("PivotTable3")
  12.        pt.RefreshTable
  13. ActiveSheet.PivotTables("PivotTable3").PivotFields("CBR/130Y").CurrentPage = "2007045"
  14. Set wkb = Nothing
  15. Set appExcel = Nothing
  16. Set pt = Nothing
Sep 24 '08 #1
15 5811
NeoPa
32,556 Expert Mod 16PB
Although you tidy up your object variables, you don't seem to close either Excel or the Workbook.

Next attempt is likely to struggle I would think.
Sep 25 '08 #2
Thank you for your reply. Although if Excel and the workbook closes, my user would not have a chance to look at the the pivot table.
Sep 25 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Mmm, I think you will need to debug your code by setting a breakpoint at line 5 and stepping through one line at a time. Pay particular attention to the value of AppExcel, as your error message is telling you that AppExcel is not set when you are referencing it to open the workbook that second time, or alternatively that it cannot access the pivot table of the sheet.

You should also check that you can see Excel (as you have set it visible) and that it is showing the correct workbook - otherwise there is no ActiveSheet to refer to.

AppExcel is set to nothing in line 13. Trying to reference AppExcel thereafter would cause an error, but you are not doing this (at least not in this code extract). Closing Excel of itself should not cause such an error unless you try to reference the currently-set instance of object AppExcel again after the closure, as the automation server is no longer connected to the object concerned, but again you do not appear to be doing this.

-Stewart

ps If you find that the workbook is open and is showing OK then check the syntax of your pivottable selection line. Is the active sheet the one on which your pivot table is stored? Did the user change the active sheet before closing the workbook (by saving the workbook with a different sheet open, for instance)?
Sep 25 '08 #4
NeoPa
32,556 Expert Mod 16PB
Thank you for your reply. Although if Excel and the workbook closes, my user would not have a chance to look at the the pivot table.
That makes sense. You do, however, want to make sure the workbook is not still open when the code runs again. I suspect you are already.

I see no reason why you would get the error you do, but then we can only see a snippet of your code. Nothing indicates what type of procedure it's found in, which may be relevant.

If you follow Stewart's advice and trace through the code (Debugging in VBA), I expect you'll find something that will make the situation clearer :)

Welcome to Bytes!
Sep 25 '08 #5
Stewart and NeoPa,

I tried stepping through the code as advised by Stewart. The variable "pt" is being set to "nothing" and appExcel.visble is "True". Also, the correct workbook and worksheet is being saved when I close the first instance of Excel. Let me clarify what's going on. When I run the code to open the pivot table everything works as designed. However, if I close the pivot table and Excel application and rerun my code, Excel will reopen the correct workbook and display all the pivot table data but without filtering on my criteria "2007045". I then get my Err 91. I made a stripped down mdb file and pivot table for testing purposes. I can get it to fail in this stripped down version. Can you guys take a look so you can see what I mean?

Thank very much,
Tom
Sep 25 '08 #6
Stewart Ross
2,545 Expert Mod 2GB
Hi Tom. If it would help then by all means post a copy of your database. To do this, you'd need to zip the DB and the Excel file and attach them to your next post. It is done by editing your next post within an hour after creating it - if you choose Manage Attachments you can upload the zip file and attach it to your post.

-Stewart
Sep 25 '08 #7
Thanks Stewart! Here you go. BTW: you'll figure it out but The files should go under a folder named TomTest under root of C drive. Thanks again
Attached Files
File Type: zip TomTest.zip (55.2 KB, 149 views)
Sep 25 '08 #8
Stewart Ross
2,545 Expert Mod 2GB
Hi Tom. I have so far found that on all but one occasion when I opened the Excel sheet from Access that the PivotTable object caused a failure. For me, this occurred regardless of whether I had opened the sheet already or not.

I used debug.print to try to print the pivottables.count value from the active sheet - this caused an immediate failure, just as it did for you on trying to set the pivottable object. However, in the immediate window I can access the pivottable count without a problem. I also tried referring to it from ActiveSheet, WorkSheets(1) and WorkSheets("Test") (all equivalent ways to do so) - no go. I tried explicitly activating the worksheet first, using ActiveWorkbook.Worksheets(1).Activate. No go. I tried using DoEvents to make sure there were no queued event processes causing issues. No go.

All this needs further investigation. It is a large pivottable, connected via a SQL Server DB (I have commented out the table refresh method in your code as I don't have a connection for the DB).

Very interesting problem. No solution so far, but timing is on the top of my list of suspects. I think what is happening is that the sheet is opening correctly, but the pivottable collection is not available in code until Excel has finished doing whatever it does under the hood in initialising the sheet. This I reckon is too late for the automation code, which has already tried to access the pivottable collection and failed.

-Stewart
Sep 25 '08 #9
Stewart Ross
2,545 Expert Mod 2GB
Well, it's not a timing issue; the pivot table count is working correctly, and I can access the name of the pivot table - but I can't set object variable pt to the value of pivottables(1) or pivottables("PivotTable3"). A real conundrum at present.

-Stewart
Sep 25 '08 #10
Stewart Ross
2,545 Expert Mod 2GB
Resolved - and I should have seen it straight away too. Amazing what can be overlooked.

Problem was that you are referring to ActiveSheet without the automation object appExcel in two places - which works within Excel itself (where ActiveSheet is an implied property of Excel) but not consistently when running as an automation server in Access. It's a very subtle error.

Lines 11 and 13 in post #1 should be replaced with

Expand|Select|Wrap|Line Numbers
  1. Set pt = appExcel.ActiveSheet.PivotTables("PivotTable3") 
  2. appExcel.ActiveSheet.PivotTables("PivotTable3").PivotFields("CBR/130Y").CurrentPage = "2007045"
-Stewart
Sep 25 '08 #11
Hey Stewart, I appreciate all your efforts. I've been racking my brain over this problem for days now. I guess if it's not possible I will resort to using the Pivot table form that comes with Access. However, it just doesn't look as nice as the Excel pivot table. Thanks again
Sep 25 '08 #12
OMG! You are awesome Stewart! I am so elated and relieved and so will my boss. Thank you, Thank you so very very much. You are the man.
Tom
Sep 25 '08 #13
NeoPa
32,556 Expert Mod 16PB
Nice one Stewart. I've just got in so I'm pleased to see you've already done all the hard work here :D

I wanted to post as I noticed the timing on the previous posts and suspect you've both missed the other's posts (Thought I'd trigger another look for you both).

While I'm here, I'll mention that what I do to avoid this problem when running Excel as an Automation Server (Excel work from within Access code) is to surround my code with a With of the workbook as in :
Expand|Select|Wrap|Line Numbers
  1. With wbk
  2.   ...
  3.   .ActiveSheet.Range(...)
  4.   ...
  5. End With
Hope this makes sense.

** Edit **
I just posted this from a page (I can still see) that I loaded less than ten minutes ago. Post #13 is not shown there at all.
Sep 26 '08 #14
Thank you again NeoPa and Stewart for your help and for being so quick to find and answer! I have found Bytes very useful in the past. This was the first time I had ever asked for help through a forum and it sure was worth it.

Thanks again,
Take care,
Tom
Sep 26 '08 #15
Stewart Ross
2,545 Expert Mod 2GB
Very glad we were of assistance to you - and thanks also to NeoPa for showing how using a simple With statement could help avoid this kind of omission.

Cheers

Stewart
Sep 26 '08 #16

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

Similar topics

2
by: Ritesh Kapoor | last post by:
Hi, I have been reading some text on C and C++ (i.e advanced books). One of the books mentioned that C++ requires a runtime support whereas C does not - what the author was trying to say was...
14
by: wolftor | last post by:
1) Is there a free runtime version of Access available that is more recent than the one for Access 2000? 2) If I create an application (MDE) in A2K, will it run on all later versions of Access?...
17
by: Owen Jenkins | last post by:
I have an Access application that is being used by 150+ clients. I develop in 97, convert to 2000 and distribute as a 97 or 2000 mde, or 97 runtime. This limits me to 97 functions. My clients may...
1
by: Samuel R. Neff | last post by:
We just started getting NullReferenceException in one of our applications on our demo server. This is occuring in a .NET Windows Service that is using binary remoting over TCP to talk to another...
1
by: DJ Dev | last post by:
Hi, I am stuck at a problem for 3 days now. I create runtime datagrids depending on the user selections and they may vary from 2-10 depending on user selection at runtime. The datagrids are...
7
by: MarkoH | last post by:
Wsdl.exe /server creates abstract class derived from WebService. Is there a way to create this class at runtime based on some WSDL file given at runtime ? What would be even better - creating...
16
by: MLH | last post by:
If I give someone a runtime app, they can open the database window by pressing the F-11 key. How to prevent???
8
by: ARC | last post by:
Hmmm...This is interesting. While reading info on packaging an Access 2007 runtime app, it's mentioned that a file such as, .accde (mde for 2007) will be changed to .accdr, where the final r means...
12
by: Reg (Lincolnshire) | last post by:
Converted an A2003 system to A2007, on testing founf out that right- click to bring up a shortcut menu on a combo box didn't work if the combo box was in a subform. Found out that this was a...
2
by: mohi | last post by:
hello every one , this may be very basic question and may be a bit out of topic ,, can anyone please tell me what are the functions of runtime library and are they the one which create the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.