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: - Set Sheet = MyXL.ActiveWorkbook.Worksheets("Sheet1")
-
-
Sheet.Activate
-
MyXL.ActiveSheet.PrintOut
I can get the variable: - 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.
6 3869
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: - Set Sheet = MyXL.ActiveWorkbook.Worksheets("Sheet1")
-
-
Sheet.Activate
-
MyXL.ActiveSheet.PrintOut
I can get the variable: - 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 ? - Dim sht As Worksheet
-
For Each sht In MyXL.ActiveWorkbook.Sheets
-
MsgBox sht.Name
-
Next sht
This perhaps could be used to load a list to select from ??
MTB
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: - Set Sheet = MyXL.ActiveWorkbook.Worksheets("Sheet1")
-
-
Sheet.Activate
-
MyXL.ActiveSheet.PrintOut
I can get the variable: - 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: - SheetName = Me![WorksheetList]
-
Set Sheet = MyXL.ActiveWorkbook.Worksheets(SheetName)
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: -
Sub GetExcel()
-
Dim MyXL As Object
-
Dim ExcelWasNotRunning As Boolean
-
Dim Sheet As Object
-
-
On Error Resume Next
-
-
Set MyXL = GetObject(, "Excel.Application")
-
If Err.Number <> 0 Then ExcelWasNotRunning = True
-
Err.Clear
-
-
Set MyXL = GetObject("c:\Test.XLS")
-
-
MyXL.Application.Visible = True
-
MyXL.Parent.Windows(1).Visible = True
-
-
-
Set Sheet = MyXL.ActiveWorkbook.Worksheets("Test2")
-
Sheet.Activate
-
Sheet.PrintOut
-
-
MyXL.Close False
-
MyXL.Quit
-
-
Set MyXL = Nothing
-
Set Sheet = Nothing
-
End Sub
-
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!
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!
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). - Private Sub Form_Load()
-
On Error GoTo ErrorHandler
-
Dim XLOpen As Boolean
-
Dim MyXL As Excel.Application
-
Dim sht As Excel.Worksheet
-
Dim ValueList As String
-
-
XLOpen = False
-
Set MyXL = CreateObject("Excel.application")
-
XLOpen = True
-
-
MyXL.Workbooks.Open CurrentProject.Path & "\SheetOffset.xls"
-
-
For Each sht In MyXL.Worksheets
-
ValueList = ValueList & ";" & sht.Name
-
Next sht
-
-
ValueList = Mid(ValueList, 2)
-
-
WorksheetList.RowSource = ValueList
-
-
MyXL.Quit
-
-
Set MyXL = Nothing
-
Exit Sub
-
-
ErrorHandler:
-
If XLOpen Then MyXL.Visible = True
-
MsgBox Err.Description
-
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
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: - Dim ExcelApp As New Excel.Application
-
Dim ExcelFile As New Excel.Workbook
-
Dim ExcelSheet As New Excel.Worksheet
-
Dim SheetName As String
-
-
If IsNull(Me![WorksheetList]) Then Exit Sub
-
SheetName = Me![WorksheetList]
-
-
-
-
ExcelApp.Visible = True
-
Set ExcelFile = ExcelApp.Workbooks.Open("C:\Test\Test.xls")
-
Set ExcelSheet = ExcelFile.Worksheets(SheetName)
-
-
ExcelSheet.Activate
-
'not sure what you have in mind here
-
-
ExcelFile.Close True
-
ExcelApp.Quit
-
-
Set ExcelSheet = Nothing
-
Set ExcelFile = Nothing
-
Set ExcelApp = Nothing
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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. ...
|
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
|
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...
|
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...
|
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...
|
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
...
|
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
...
|
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
...
|
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
...
|
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,...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |