473,387 Members | 1,517 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.

Excel Automation and variables

Using Microsoft Office 2003.

From Access, triggered by a button click on a form, I have a piece of code that opens Excel, opens a specific file, activates a specific sheet in that workbook, prints it, then closes both the workbook and Excel.

When I use absolute names, it works wonderfully. However, I want the user to be able to select a sheet name from a listbox, and pass that as a variable for the name. Therein lies the problem.

Using the absolute name:

Expand|Select|Wrap|Line Numbers
  1. Set Sheet = MyXL.ActiveWorkbook.Worksheets("Sheet1")
  2.  
  3. Sheet.Activate
  4. MyXL.ActiveSheet.PrintOut
I can get the variable:

Expand|Select|Wrap|Line Numbers
  1. SheetName = [WorksheetList].[value]
However, I can't seem to refer to the variable properly in place of ("Sheet1"). It has something to do with the quotes, I am sure. They are required. I have had no luck "building" quotes onto the SheetName using & and such.

Any ideas? I am desperate and ready to go out and lie on the lawn in hopes seagulls will peck me to death and I won't have to look at this any more.
Oct 31 '07 #1
6 3869
MikeTheBike
639 Expert 512MB
Using Microsoft Office 2003.

From Access, triggered by a button click on a form, I have a piece of code that opens Excel, opens a specific file, activates a specific sheet in that workbook, prints it, then closes both the workbook and Excel.

When I use absolute names, it works wonderfully. However, I want the user to be able to select a sheet name from a listbox, and pass that as a variable for the name. Therein lies the problem.

Using the absolute name:

Expand|Select|Wrap|Line Numbers
  1. Set Sheet = MyXL.ActiveWorkbook.Worksheets("Sheet1")
  2.  
  3. Sheet.Activate
  4. MyXL.ActiveSheet.PrintOut
I can get the variable:

Expand|Select|Wrap|Line Numbers
  1. SheetName = [WorksheetList].[value]
However, I can't seem to refer to the variable properly in place of ("Sheet1"). It has something to do with the quotes, I am sure. They are required. I have had no luck "building" quotes onto the SheetName using & and such.

Any ideas? I am desperate and ready to go out and lie on the lawn in hopes seagulls will peck me to death and I won't have to look at this any more.
Hi

I'm just a little confused, alittle more info on how you envusage this running.

What here is ths snippet of code placed, and what do tme element reresent.

SheetName = [WorksheetList].[value]

??
I feel sure this is possible but doon't know your precise requirements, ie where and what is "list"

this may help ?

Expand|Select|Wrap|Line Numbers
  1. Dim sht As Worksheet
  2. For Each sht In MyXL.ActiveWorkbook.Sheets
  3.     MsgBox sht.Name
  4. Next sht
This perhaps could be used to load a list to select from ??

MTB
Oct 31 '07 #2
ADezii
8,834 Expert 8TB
Using Microsoft Office 2003.

From Access, triggered by a button click on a form, I have a piece of code that opens Excel, opens a specific file, activates a specific sheet in that workbook, prints it, then closes both the workbook and Excel.

When I use absolute names, it works wonderfully. However, I want the user to be able to select a sheet name from a listbox, and pass that as a variable for the name. Therein lies the problem.

Using the absolute name:

Expand|Select|Wrap|Line Numbers
  1. Set Sheet = MyXL.ActiveWorkbook.Worksheets("Sheet1")
  2.  
  3. Sheet.Activate
  4. MyXL.ActiveSheet.PrintOut
I can get the variable:

Expand|Select|Wrap|Line Numbers
  1. SheetName = [WorksheetList].[value]
However, I can't seem to refer to the variable properly in place of ("Sheet1"). It has something to do with the quotes, I am sure. They are required. I have had no luck "building" quotes onto the SheetName using & and such.

Any ideas? I am desperate and ready to go out and lie on the lawn in hopes seagulls will peck me to death and I won't have to look at this any more.
Try:
Expand|Select|Wrap|Line Numbers
  1. SheetName = Me![WorksheetList]
  2. Set Sheet = MyXL.ActiveWorkbook.Worksheets(SheetName)
Oct 31 '07 #3
SheetName = Me![WorksheetList]
Set Sheet = MyXL.ActiveWorkbook.Worksheets(SheetName)

--------------------------------------------------------------------------------
Okay, I had tried that and it did not work for me. Then I went back and checked, and it seemed even with the absolute names it was not activating the proper worksheet, although I swear at some time it was. This is what I have:

Expand|Select|Wrap|Line Numbers
  1. Sub GetExcel()
  2. Dim MyXL As Object
  3. Dim ExcelWasNotRunning As Boolean
  4. Dim Sheet As Object
  5.  
  6. On Error Resume Next
  7.  
  8. Set MyXL = GetObject(, "Excel.Application")
  9. If Err.Number <> 0 Then ExcelWasNotRunning = True
  10. Err.Clear
  11.  
  12. Set MyXL = GetObject("c:\Test.XLS")
  13.  
  14. MyXL.Application.Visible = True
  15. MyXL.Parent.Windows(1).Visible = True
  16.  
  17.  
  18. Set Sheet = MyXL.ActiveWorkbook.Worksheets("Test2")
  19. Sheet.Activate
  20. Sheet.PrintOut
  21.  
  22. MyXL.Close False
  23. MyXL.Quit
  24.  
  25. Set MyXL = Nothing
  26. Set Sheet = Nothing
  27. End Sub
  28.  
I copied the above from various sources around the internet to play with and come up with something that works.

Basically there is an Excel workbook that contains several worksheets. Each named sheet is a blank template. On an access form is a combo box that lists the name of each worksheet. I want the user to be able to choose a worksheet name, then click a button to trigger Excel to start, open the file to that specific worksheet, print it out, then close the file and Excel.

I need to pass the worksheet name as a variable instead of using, say, cases because from time to time worksheets will be added to this workbook (and of course added to the value list of the combobox). I don't want anybody to have to adjust the code when that happens.

I hope that is clearer!
Nov 1 '07 #4
MikeTheBike - that would be great if I can populate the list that way. Only I have no idea where to put that code! It would have to be separate from the click event, because the list needs to be populated before that is even triggered.

Would it go into an event for the form itself, or for the drop down/list it is to populate?

As for the original post, I might be able to get around it by saving each worksheet as it's own named workbook, and listing that file path and name in a table, using that to populate the list. That seems to work and I can pass the variable fine without the quotes. So it seems my REAL issue is not being able to activate the proper worksheet. If I can lick that, I think I might be in the clear and NOT have to create a million new files.

I appreciate any help at all!
Nov 1 '07 #5
MikeTheBike
639 Expert 512MB
MikeTheBike - that would be great if I can populate the list that way. Only I have no idea where to put that code! It would have to be separate from the click event, because the list needs to be populated before that is even triggered.

Would it go into an event for the form itself, or for the drop down/list it is to populate?

As for the original post, I might be able to get around it by saving each worksheet as it's own named workbook, and listing that file path and name in a table, using that to populate the list. That seems to work and I can pass the variable fine without the quotes. So it seems my REAL issue is not being able to activate the proper worksheet. If I can lick that, I think I might be in the clear and NOT have to create a million new files.

I appreciate any help at all!
Hi

You could try something like this in the forms Load event (assuming the combo control is named WorksheetList).

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. On Error GoTo ErrorHandler
  3.     Dim XLOpen As Boolean
  4.     Dim MyXL As Excel.Application
  5.     Dim sht As Excel.Worksheet
  6.     Dim ValueList As String
  7.  
  8.     XLOpen = False
  9.     Set MyXL = CreateObject("Excel.application")
  10.     XLOpen = True
  11.  
  12.     MyXL.Workbooks.Open CurrentProject.Path & "\SheetOffset.xls"
  13.  
  14.     For Each sht In MyXL.Worksheets
  15.         ValueList = ValueList & ";" & sht.Name
  16.     Next sht
  17.  
  18.     ValueList = Mid(ValueList, 2)
  19.  
  20.     WorksheetList.RowSource = ValueList
  21.  
  22.     MyXL.Quit
  23.  
  24.     Set MyXL = Nothing
  25.     Exit Sub
  26.  
  27. ErrorHandler:
  28.     If XLOpen Then MyXL.Visible = True
  29.     MsgBox Err.Description
  30. End Sub
Note, the error handling is very rudermentary, just enough to let me prove the principle (without multiple instances of excel I can't see!).

After this you can select the required sheet and open Excel and the workbook again as necessary !!??

MTB

ps. the combo's Row Source Type should be set to Value List
Nov 1 '07 #6
ADezii
8,834 Expert 8TB
MikeTheBike - that would be great if I can populate the list that way. Only I have no idea where to put that code! It would have to be separate from the click event, because the list needs to be populated before that is even triggered.

Would it go into an event for the form itself, or for the drop down/list it is to populate?

As for the original post, I might be able to get around it by saving each worksheet as it's own named workbook, and listing that file path and name in a table, using that to populate the list. That seems to work and I can pass the variable fine without the quotes. So it seems my REAL issue is not being able to activate the proper worksheet. If I can lick that, I think I might be in the clear and NOT have to create a million new files.

I appreciate any help at all!
This different approach has been tested and does work. It reads the Sheet Name from a List Box, opens the appropriate Excel Workbook, then activates the proper Worksheet. The rest should be a piece of cake:
Expand|Select|Wrap|Line Numbers
  1. Dim ExcelApp As New Excel.Application
  2. Dim ExcelFile As New Excel.Workbook
  3. Dim ExcelSheet As New Excel.Worksheet
  4. Dim SheetName As String
  5.  
  6. If IsNull(Me![WorksheetList]) Then Exit Sub
  7. SheetName = Me![WorksheetList]
  8.  
  9.  
  10.  
  11. ExcelApp.Visible = True
  12. Set ExcelFile = ExcelApp.Workbooks.Open("C:\Test\Test.xls")
  13. Set ExcelSheet = ExcelFile.Worksheets(SheetName)
  14.  
  15. ExcelSheet.Activate
  16.   'not sure what you have in mind here
  17.  
  18. ExcelFile.Close True
  19. ExcelApp.Quit
  20.  
  21. Set ExcelSheet = Nothing
  22. Set ExcelFile = Nothing
  23. Set ExcelApp = Nothing
Nov 2 '07 #7

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

Similar topics

7
by: taylor.bryant | last post by:
I am running: Win XP SP2 Excel 2002, Access 2002 (Office XP SP3) Using Visual Basic (not VB.NET) At one point (prior to XP SP2?!? - I can't pin it down), this did not happen and I was easily...
9
by: jillandgordon | last post by:
I am trying to import an excel file into Access 97. It looks perfectly all right but, every time I try to import it, I get to the lst step and am told that it was not imported due to an error. ...
17
by: Mansi | last post by:
I need to do some research on how to use excel automation from c#. Does anyone know of any good books related to this subject? Thanks. Mansi
12
by: D. Shane Fowlkes | last post by:
This most likely belongs in another forum but I thought I'd start here. I have a COM Object written in VB6. The DLL will access MS Excel and use it's Object Library to write a customized report...
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...
7
by: rdemyan via AccessMonster.com | last post by:
I want to make sure that I'm closing an opened spreadsheet correctly. I've been having some locking up problems. The following code adds a dummy row to the spreadsheet to ensure that that the data...
0
by: Sharath | last post by:
Quality Globe is Glad to Offer you the Fast Track course on Automation, QTP Basics and Advanced, and Quality Center Starting Date: June 4th, 2007 Timings: 10 AM to 3:30 PM Duration: 50 Hours ...
0
by: Sharath | last post by:
"Inspired" by the huge success of our first two automation fast track batches We are forced to start third fast track automation batch ...
0
by: Sharath | last post by:
We are glad to inform you that "Inspired" by the huge success of our first three automation fast track batches We are forced to start fourth fast track automation batch ...
0
by: Sharath | last post by:
We are glad to inform you that "Inspired" by the huge success of our first four automation fast track batches We are forced to start fifth fast track automation batch ...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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.