|
-
May 22nd, 2006, 09:22 AM
#1
Thread Starter
Junior Member
[RESOLVED] First thread in this site, problem with reading from excel,help~
Firstly I have to tell u that I am a Chinese boy, so maybe I will not quite understand what u experts have responded and ask again for solution, sorry for that.
Well, to the problem. I am trying to read from an Excel file including 2 cols and unsure rows, and I wanna get all these rows to be written into my access file. Firstly, I have to ensure that none of these rows can be null. I wrote these codes, yet the problem shows now:
1. Only when I open the file manually can the file be accessed by my application. (The excel file is in MyDocument folder, can it be the problem of permission that refused me???)
2. I found quite a lot of copies of application "Excel.exe" running in my processes explorer.(This happens when I tested for quite a time). I cannot quit this application????
VB Code:
Public Function ValidateExcelFile(strFilePath As String) As Boolean
Set appExcel = New Excel.Application
Set wbExcel = appExcel.Workbooks.Open(strFilePath)
Set wsExcel = wbExcel.Sheets(1)
Dim lRowNum As Long
Dim lColNum As Long
lColNum = wsExcel.UsedRange.Columns.Count
lRowNum = wsExcel.UsedRange.Rows.Count
If lColNum <> 2 Then
ValidateExcelFile = False
appExcel.Quit
Set appExcel = Nothing
Set wbExcel = Nothing
Set wsExcel = Nothing
'Exit Function
Else
Dim i
For i = 2 To lRowNum
If wsExcel.Cells(i, 1).Value = "" Then
ValidateExcelFile = False
Err.Raise "100", , modWizard.GetResString("3004")
appExcel.Quit
Set appExcel = Nothing
Set wbExcel = Nothing
Set wsExcel = Nothing
Exit Function
End If
Next
ValidateExcelFile = True
appExcel.Quit
Set appExcel = Nothing
Set wbExcel = Nothing
Set wsExcel = Nothing
End If
End Function
Hope you guys can help me~~~A help from China )
Thanks.
Last edited by si_the_geek; May 22nd, 2006 at 09:39 AM.
-
May 22nd, 2006, 09:47 AM
#2
Re: First thread in this site, problem with reading from excel,help~
welcome!
hmm.. im not sure why it wont work.. it looks good to me.
are u getting any errors?
(if so.. on what line)
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
May 22nd, 2006, 09:49 AM
#3
Re: First thread in this site, problem with reading from excel,help~
Welcome to VBForums! 
First of all, your English is very good - so hopefully you'll understand us (we can certainly understand you!).
1. I'm not sure why that would happen, your code seems fine. What does strFilePath contain?
Does the file have a password to open it?
2. This is happening because you aren't closing the objects (wbExcel etc) properly. This is even more so when you raise the error (Err.Raise), as this immediately exits the loop - without running the "quit" code you have.
Here is an amended version of your code, which should remove that issue:
VB Code:
Public Function ValidateExcelFile(strFilePath As String) As Boolean
Set appExcel = New Excel.Application
Set wbExcel = appExcel.Workbooks.Open(strFilePath)
Set wsExcel = wbExcel.Sheets(1)
Dim lRowNum As Long
Dim lColNum As Long
lColNum = wsExcel.UsedRange.Columns.Count
lRowNum = wsExcel.UsedRange.Rows.Count
If lColNum <> 2 Then
ValidateExcelFile = False
Else
Dim i
For i = 2 To lRowNum
If wsExcel.Cells(i, 1).Value = "" Then
ValidateExcelFile = False
Set wsExcel = Nothing
wbExcel.Close SaveChanges:= False
Set wbExcel = Nothing
appExcel.Quit
Set appExcel = Nothing
Err.Raise "100", , modWizard.GetResString("3004")
Exit Function
End If
Next
ValidateExcelFile = True
End If
'as this was being done at the end of both parts of the "If", you can simply have it afterwards
Set wsExcel = Nothing
wbExcel.Close SaveChanges:= False
Set wbExcel = Nothing
appExcel.Quit
Set appExcel = Nothing
End Function
I would recommend stopping all instances of Excel.Exe in task manager before running it again - it may well solve the first problem too.
-
May 22nd, 2006, 09:55 AM
#4
Re: First thread in this site, problem with reading from excel,help~
ahh.. LOL. after a 10 day vacation to the beach.. I completely missed those!
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
May 22nd, 2006, 09:57 AM
#5
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
 Originally Posted by Static
welcome!
hmm.. im not sure why it wont work.. it looks good to me.
are u getting any errors?
(if so.. on what line)
Thank you, feeling happy to hear that welcome~
I do get errors, yet I've used an ErrorHandler, which u know, sometimes will not display the line error happened. And its technical description is a bit diffcult for me to translate, sorry:<
-
May 22nd, 2006, 10:00 AM
#6
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
 Originally Posted by si_the_geek
Welcome to VBForums!
First of all, your English is very good - so hopefully you'll understand us (we can certainly understand you!).
1. I'm not sure why that would happen, your code seems fine. What does strFilePath contain?
Does the file have a password to open it?
2. This is happening because you aren't closing the objects (wbExcel etc) properly. This is even more so when you raise the error (Err.Raise), as this immediately exits the loop - without running the "quit" code you have.
Here is an amended version of your code, which should remove that issue:
VB Code:
Public Function ValidateExcelFile(strFilePath As String) As Boolean
Set appExcel = New Excel.Application
Set wbExcel = appExcel.Workbooks.Open(strFilePath)
Set wsExcel = wbExcel.Sheets(1)
Dim lRowNum As Long
Dim lColNum As Long
lColNum = wsExcel.UsedRange.Columns.Count
lRowNum = wsExcel.UsedRange.Rows.Count
If lColNum <> 2 Then
ValidateExcelFile = False
Else
Dim i
For i = 2 To lRowNum
If wsExcel.Cells(i, 1).Value = "" Then
ValidateExcelFile = False
Set wsExcel = Nothing
wbExcel.Close SaveChanges:= False
Set wbExcel = Nothing
appExcel.Quit
Set appExcel = Nothing
Err.Raise "100", , modWizard.GetResString("3004")
Exit Function
End If
Next
ValidateExcelFile = True
End If
'as this was being done at the end of both parts of the "If", you can simply have it afterwards
Set wsExcel = Nothing
wbExcel.Close SaveChanges:= False
Set wbExcel = Nothing
appExcel.Quit
Set appExcel = Nothing
End Function
I would recommend stopping all instances of Excel.Exe in task manager before running it again - it may well solve the first problem too.
Thank you sir. I will try that immediately!
Thanks, really!
-
May 22nd, 2006, 10:03 AM
#7
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
By the way, I don't have a password to open, and strFilePath, as is defined, contains the real path of the file, gained from a textBox with a button to open a OpenDialog.
-
May 22nd, 2006, 10:12 AM
#8
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
I found the problem,yet havn't resovled it :<
The problem happens here:
VB Code:
Set wbExcel = appExcel.Workbooks.Open(strFilePath)
and VB says :"Cannot access The file book2.xls." and the app stops.
Why that happens??? Confused~
-
May 22nd, 2006, 10:21 AM
#9
Re: First thread in this site, problem with reading from excel,help~
Add the line just before that one:
..and tell us what gets printed to the Immediate window (if you cant see the Immediate window, select it from the "View" menu).
-
May 22nd, 2006, 10:25 AM
#10
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
it says :"C:\Documents and Settings\Admin\My Documents\Book2.xls"
can it be a permission-refused problem, or something?
Thanks~
-
May 22nd, 2006, 10:28 AM
#11
Re: First thread in this site, problem with reading from excel,help~
Do you have access/permission to the Admin profile's Documents?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 22nd, 2006, 10:28 AM
#12
Re: First thread in this site, problem with reading from excel,help~
It could be, but then you wouldn't be able to open it manually either.
Could another program have the file open at the same time? (if so, that would probably cause the problem).
-
May 22nd, 2006, 10:28 AM
#13
Re: First thread in this site, problem with reading from excel,help~
open your task manager and check to make sure NO EXCEL's are running...
if its open elsewhere it may cause a problem...
then if it still doesnt work.. copt the book to C:\
and try it from there... "C:\Book2.xls"
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
May 22nd, 2006, 10:29 AM
#14
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
Well, it can't be the permission problem, I tried to read another file from Drive D,which is not an NTFS file system, but it still not works
-
May 22nd, 2006, 10:30 AM
#15
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
There isn't any Excel.exe instance in my task manager, and I tried in another Disk, but problem remains :<<<<
-
May 22nd, 2006, 10:30 AM
#16
Re: First thread in this site, problem with reading from excel,help~
Actually... as you aren't writing to the file, you could open it as read-only, using code like this:
VB Code:
Set wbExcel = appExcel.Workbooks.Open(strFilePath[u], 0, True[/u])
-
May 22nd, 2006, 10:34 AM
#17
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
By the way, VB says also :"Runtime error:1004", can this help you guys figure that out?
sighhhh~
-
May 22nd, 2006, 10:35 AM
#18
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
-
May 22nd, 2006, 10:39 AM
#19
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
Oh, faint~This time, the problem "updates" as that:
"Runtime error:1004, Cannot get 'Open' attribute in Class Workbooks." --->In English basicly means that.
-
May 22nd, 2006, 10:44 AM
#20
Re: First thread in this site, problem with reading from excel,help~
Just read most of the thread. Are you using a non-english version of Excel or Windows? Which version of Excel are you using?
If you copy the xls file to the root of C will it open? It doesn break on error at the .Open line of code?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 22nd, 2006, 10:53 AM
#21
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
I am using a Chinese-simplified version of both Windows and Excel, and VB as well.
I am using Excel 2000.
I added a Reference to Microsoft Excel Object Library 9 in VB.
I still cannot open the Excel file using my app, even if copied to the root C. Yet can be opened of course in Excel itself
-
May 22nd, 2006, 11:00 AM
#22
Re: First thread in this site, problem with reading from excel,help~
Hmm, its got to be the chinese version of Excel possibly. Just a hunch as I have seen it behave badly before on certain chars causing issues.
Do you have both Windows and Office current Updates?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 22nd, 2006, 11:09 AM
#23
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
Well, Thank you guys, really~
I think I get it now. The murderer is another Function IsExcelFile
As I wanna check whether the file input is Excel before reading it, I wrote this function as follows:
VB Code:
Public Function IsExcelFile(strFilePath As String) As Boolean
Dim connStr As String
connStr = ConnStringHead & strFilePath & ConnStringEnd
dim sql
sql= "select * from [Sheet1$A2:A100]"
On Error Resume Next
adoConn.Open connStr
If Err Then
IsExcelFile = False
Exit Function
Else
IsExcelFile = True
End If
'--------------------------------------------these two lines are missing
adoRS.Close
adoConn.Close
'--------------------------------------------
End Function
And as you see, the connection hasn't been closed~~~
Yes~~~thank you for your help~~~really helpful to me!
By the way, welcome to China if possible~~~
Last edited by boris9050; May 22nd, 2006 at 11:33 AM.
-
May 22nd, 2006, 11:12 AM
#24
Thread Starter
Junior Member
Re: First thread in this site, problem with reading from excel,help~
 Originally Posted by RobDog888
Hmm, its got to be the chinese version of Excel possibly. Just a hunch as I have seen it behave badly before on certain chars causing issues.
Do you have both Windows and Office current Updates?
Thank you sir, it's done~hoho
Love VB, love life~~~
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|