473,508 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find worksheetname

Hi NG

I got a folder in which I got 1000 excelfiles. In each file there is one or
more worksheets. I need to import theese sheets in access. I want to use
docmd.transferspreadsheet, but my problem is that I don't know the name of
the worksheet, and I don't know the number of worksheets in each file.

Is there anyone who can help?

Thx in advance
Henry

(Emailaddress don't work)

Nov 12 '05 #1
2 2523
"Anne" <av*@telmore.ru> wrote in message
news:3f***********************@dread16.news.tele.d k...
Hi NG

I got a folder in which I got 1000 excelfiles. In each file there is one or more worksheets. I need to import theese sheets in access. I want to use
docmd.transferspreadsheet, but my problem is that I don't know the name of
the worksheet, and I don't know the number of worksheets in each file.

Is there anyone who can help?

Thx in advance
Henry


You don't say whether you've started to write the import routine and have
gotten stuck or whether you don't know where to start. One possible route -
create a new form with a button "cmdImport" and copy the following code in -
changing the two constants at the top.

Fletcher
Notes:
The code uses late binding to avoid reference issues - although you could
switch to early
I would be very cautious of DoCmd.TransferSpreadsheet - although it will be
relatively fast, it does not allow for custom error handling. If you really
wanted to go this way, then make sure you first import to a dummy table with
very loose requirements - perhaps all text 255 characters long with no
indexes and no required fields. Once you have this temp table, you can then
make sure all the required fields are there and are in the right format.

Option Compare Database
Option Explicit

Const XLFOLDER = "C:\Test\Workbooks\"
'
Const XLTARGET = "Table1"
'
'
'

Private Function ImportSheets(xlApp As Object, strPath As String) As Boolean

On Error GoTo Err_Handler

Dim xlBook As Object ' Excel.Workbook
Dim xlSheet As Object ' Excel.Worksheet

Set xlBook = xlApp.Workbooks.Open(strPath, False, True)

For Each xlSheet In xlBook.Worksheets

DoCmd.TransferSpreadsheet acImport, _
acSpreadsheetTypeExcel5, _
XLTARGET, _
strPath, _
False, _
xlSheet.Name & "$"
Next

ImportSheets = True

Exit_Handler:

On Error Resume Next

If Not xlBook Is Nothing Then
xlBook.Close
Set xlBook = Nothing
End If

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function

Private Sub cmdImport_Click()

On Error GoTo Err_Handler

Dim xlApp As Object ' Excel.Application
Dim fso As Object ' FileSystemObject
Dim fld As Object ' Folder
Dim fil As Object ' File

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(XLFOLDER) Then

Set xlApp = CreateObject("Excel.Application")

Set fld = fso.GetFolder(XLFOLDER)

For Each fil In fld.Files

If UCase(fso.GetExtensionName(fil.Path)) = "XLS" Then

If Not ImportSheets(xlApp, fil.Path) Then

MsgBox "Error importing '" & fil.Path & "'", _
vbExclamation, "Import Error"

End If

End If

Next fil

End If

MsgBox "Done", vbInformation, "Import Routine"

Exit_Handler:

If Not fld Is Nothing Then
Set fld = Nothing
End If

If Not fld Is Nothing Then
Set fld = Nothing
End If

If Not xlApp Is Nothing Then
xlApp.Quit
Set xlApp = Nothing
End If

If Not fso Is Nothing Then
Set fso = Nothing
End If

Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


Nov 12 '05 #2
Hi Fletcher,
I got the same problem and tried your code.
Access reads out the name of the first worksheet but gives back error
3125 when it comes to import it. then it cancels the entire workbook.
I cant find the mistake.
i actually dont want to import whole worksheets but named areas in
them.
do you have an idea how to achieve this.
thanx heaps
Jens

"Fletcher Arnold" <fl****@home.com> wrote in message news:<bm**********@hercules.btinternet.com>...
"Anne" <av*@telmore.ru> wrote in message
news:3f***********************@dread16.news.tele.d k...
Hi NG

I got a folder in which I got 1000 excelfiles. In each file there is one

or
more worksheets. I need to import theese sheets in access. I want to use
docmd.transferspreadsheet, but my problem is that I don't know the name of
the worksheet, and I don't know the number of worksheets in each file.

Is there anyone who can help?

Thx in advance
Henry


You don't say whether you've started to write the import routine and have
gotten stuck or whether you don't know where to start. One possible route -
create a new form with a button "cmdImport" and copy the following code in -
changing the two constants at the top.

Fletcher
Notes:
The code uses late binding to avoid reference issues - although you could
switch to early
I would be very cautious of DoCmd.TransferSpreadsheet - although it will be
relatively fast, it does not allow for custom error handling. If you really
wanted to go this way, then make sure you first import to a dummy table with
very loose requirements - perhaps all text 255 characters long with no
indexes and no required fields. Once you have this temp table, you can then
make sure all the required fields are there and are in the right format.

Option Compare Database
Option Explicit

Const XLFOLDER = "C:\Test\Workbooks\"
'
Const XLTARGET = "Table1"
'
'
'

Private Function ImportSheets(xlApp As Object, strPath As String) As Boolean

On Error GoTo Err_Handler

Dim xlBook As Object ' Excel.Workbook
Dim xlSheet As Object ' Excel.Worksheet

Set xlBook = xlApp.Workbooks.Open(strPath, False, True)

For Each xlSheet In xlBook.Worksheets

DoCmd.TransferSpreadsheet acImport, _
acSpreadsheetTypeExcel5, _
XLTARGET, _
strPath, _
False, _
xlSheet.Name & "$"
Next

ImportSheets = True

Exit_Handler:

On Error Resume Next

If Not xlBook Is Nothing Then
xlBook.Close
Set xlBook = Nothing
End If

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function

Private Sub cmdImport_Click()

On Error GoTo Err_Handler

Dim xlApp As Object ' Excel.Application
Dim fso As Object ' FileSystemObject
Dim fld As Object ' Folder
Dim fil As Object ' File

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(XLFOLDER) Then

Set xlApp = CreateObject("Excel.Application")

Set fld = fso.GetFolder(XLFOLDER)

For Each fil In fld.Files

If UCase(fso.GetExtensionName(fil.Path)) = "XLS" Then

If Not ImportSheets(xlApp, fil.Path) Then

MsgBox "Error importing '" & fil.Path & "'", _
vbExclamation, "Import Error"

End If

End If

Next fil

End If

MsgBox "Done", vbInformation, "Import Routine"

Exit_Handler:

If Not fld Is Nothing Then
Set fld = Nothing
End If

If Not fld Is Nothing Then
Set fld = Nothing
End If

If Not xlApp Is Nothing Then
xlApp.Quit
Set xlApp = Nothing
End If

If Not fso Is Nothing Then
Set fso = Nothing
End If

Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7579
by: Dan Jones | last post by:
I'm writing a script to process a directory tree of images.  In each directory, I need to process each image and generate an HTML file listing all of the images and links to the subdirectories....
0
2750
by: amit | last post by:
I want to find out that if there is a mechanism to find a text inside a C# file and replace it with another string. I am using DTE to do it, the find proerty does it, the results are getting...
0
2566
by: AMIT PUROHIT | last post by:
hi, this is a qry which I m stuck up with I want to find out that if there is a mechanism to find a text inside a C# file and replace it with another string. I am using DTE(EnvDTE) to do it,...
0
2103
by: amit | last post by:
hi I have created a tool which does a find and replace thru DTE, now after it is done, it opens up a window, "FIND REACHED THE STARTING POINT OF SEARCH" I want to disbale this window...
5
3001
by: Mike Labosh | last post by:
In VB 6, the Form_QueryUnload event had an UnloadMode parameter that let me find out *why* a form is unloading, and then conditionally cancel the event. In VB.NET, the Closing event passes a...
3
7181
by: DJTN | last post by:
I'm getting the following error when I try to compile my setup project in VS 2002. I have re-installed the .net framework 1.1 and it didnt solve the problem. WARNING: Unable to find dependency...
3
16474
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or...
0
11251
by: Derek | last post by:
I am creating an intranet using Visual Web Developer Express Edition. Everything has been working OK until yesterday when I started getting 62 messages all beginning "Could not find schema...
0
7225
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
7123
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...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
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
5627
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,...
0
3193
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
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.