473,509 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why am I getting excel object variable or with block variable when running code?

anoble1
245 New Member
Hi,

In Access, I am getting error: excel object variable or with block variable when running code. The code runs fine the first time when I start it. When I close excel and hit the button again it gets hung below where it assigns the name. It never makes it to where it assigns the name. The only time it works is when I hit the Stop button in Access. or restart Access. Then it renames the sheet.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command11_Click()
  2. Dim fso
  3. Dim processfpath As String
  4. Dim filename As String
  5. Dim TransFile As String
  6. Dim xlApp As Excel.Application
  7. Dim xlWkbk As Excel.workBook
  8. Dim xlSht As Excel.workSheet
  9. Dim fileSave As String
  10. Dim sheetname As String
  11.  
  12. ' get the business unit of the requesting LCS
  13. Requestor = Me.Requestor.Value
  14. strSQL = "SELECT tblUsers.UserName, tblUsers.BusinessUnit FROM tblUsers  WHERE [UserName]='" & Forms!frmProcess!Requestor & "';"
  15. Set rs = CurrentDb.OpenRecordset(strSQL)
  16. business = rs.Fields("BusinessUnit")
  17.  
  18. 'Set the application
  19. Set xlApp = New Excel.Application
  20.  
  21. 'Make the Application Visible
  22. xlApp.Visible = True
  23.  
  24. 'Set the workbook and the filepath
  25. Set xlWkbk = xlApp.Workbooks.Open("H:\TESTING\Golden Springs DS 21342.xlsx")
  26.  
  27. Set xlSht = ActiveSheet
  28.  
  29. xlSht.Name = ("TEST2")
  30. 'Here is where the error happens above.
  31.  
  32.  
  33. processfpath = Me.FilePath.Value
  34.  
  35. Set xlApp = Nothing
  36. Set xlWkbk = Nothing
  37. Set xlSht = Nothing
  38.  
May 20 '13 #1
1 2465
anoble1
245 New Member
Fixed it.

Expand|Select|Wrap|Line Numbers
  1. ' get the business unit of the requesting LCS
  2. Requestor = Me.Requestor.Value
  3. strSQL = "SELECT tblUsers.UserName, tblUsers.BusinessUnit FROM tblUsers  WHERE [UserName]='" & Forms!frmProcess!Requestor & "';"
  4. Set rs = CurrentDb.OpenRecordset(strSQL)
  5. business = rs.Fields("BusinessUnit")
  6.  
  7. 'Set the application
  8. '
  9.  
  10.  
  11.  
  12.  
  13. 'Make the Application Visible
  14. xlApp.Visible = True
  15.  
  16. Set xlApp = New Excel.Application
  17.  
  18. xlApp.Visible = True
  19.  
  20.  
  21. 'Set the workbook and the filepath 'Change "C:\Book1.xls " to your own filepath and Workbook name
  22. 'Set xlWkbk = xlApp.Workbooks.Open("S:\Workgroups\APC Power Delivery-Contract Services\Trim Notices\Distribution Trim Notices\Golden Springs DS 21342.xlsx")
  23. Set xlWkbk = xlApp.Workbooks.Open("H:\TESTING\Golden Springs DS 21342.xlsx")
May 20 '13 #2

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

Similar topics

3
15294
by: Joe Schwartz | last post by:
All, Could somebody please help me out here. I am getting an "Object variable or With block variable not set" error with the following code. This happens when I enter a value into the "ponum"...
4
9517
by: nelsonchang | last post by:
Dear all, I wrote the VB code in a "button press event" as below in which is retrieve data from SQL server and insert the data into Excel. After that, I would call Excel "subtotal" API to build a...
3
35490
by: Richard Hollenbeck | last post by:
I've marked the line in this subroutine where I've been getting this error. It may be something stupid but I've been staring at this error trying to fix it for over an hour. I'm pretty sure the...
8
18419
by: sandy21380 | last post by:
I keep getting the error 'Object variable or With block variable not set' with the following code. 'i' is integer, ViewOptions is OptionBox. Any idea what I'm doing wrong? For i = 0 To 4 ...
3
3243
by: Newbie19 | last post by:
I'm trying to get a list of all subfolders in a folder on a share drive, but I keep on getting this error message: Object variable or With block variable not set. Description: An unhandled...
4
3615
by: sahitya sidda | last post by:
"Error 91: object variable or with block variable not set" I am getting the above error while retrieving the index of selected row in listview. Please help me out Private Sub...
8
8699
by: cloh | last post by:
Thanks to all the people who reply so promptly to my questions! I have another one related to the form I am working on. When I try to call this function from another, I get the "Object variable or...
5
3242
by: Al G | last post by:
Hi, I'm converting a bit of POP3 VB6 code to VB2005, and have run into this error with the following code. Can someone help me find out what I'm missing/doing wrong? 'holds the attachments...
2
4652
by: Whasigga | last post by:
Hello I am working on a project to export data from a query into an Excel file. I have a form with a button on it and the button when clicked runs a function that is supposed to run the query and...
0
1407
by: Paul Morriss | last post by:
I get this error from an application: ************** Exception Text ************** System.NullReferenceException: Object variable or With block variable not set. at...
0
7234
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
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.