Results 1 to 24 of 24

Thread: [RESOLVED] First thread in this site, problem with reading from excel,help~

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    Resolved [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:
    1. Public Function ValidateExcelFile(strFilePath As String) As Boolean
    2.     Set appExcel = New Excel.Application
    3.     Set wbExcel = appExcel.Workbooks.Open(strFilePath)
    4.     Set wsExcel = wbExcel.Sheets(1)
    5.     Dim lRowNum As Long
    6.     Dim lColNum As Long
    7.     lColNum = wsExcel.UsedRange.Columns.Count
    8.     lRowNum = wsExcel.UsedRange.Rows.Count
    9.     If lColNum <> 2 Then
    10.         ValidateExcelFile = False
    11.         appExcel.Quit
    12.         Set appExcel = Nothing
    13.         Set wbExcel = Nothing
    14.         Set wsExcel = Nothing
    15.         'Exit Function
    16.     Else
    17.         Dim i
    18.  
    19.         For i = 2 To lRowNum
    20.             If wsExcel.Cells(i, 1).Value = "" Then
    21.                 ValidateExcelFile = False
    22.                 Err.Raise "100", , modWizard.GetResString("3004")
    23.                 appExcel.Quit
    24.                 Set appExcel = Nothing
    25.                 Set wbExcel = Nothing
    26.                 Set wsExcel = Nothing
    27.                 Exit Function
    28.             End If
    29.         Next
    30.         ValidateExcelFile = True
    31.         appExcel.Quit
    32.         Set appExcel = Nothing
    33.         Set wbExcel = Nothing
    34.         Set wsExcel = Nothing
    35.     End If
    36.    
    37. 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.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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"

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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:
    1. Public Function ValidateExcelFile(strFilePath As String) As Boolean
    2.         Set appExcel = New Excel.Application
    3.         Set wbExcel = appExcel.Workbooks.Open(strFilePath)
    4.         Set wsExcel = wbExcel.Sheets(1)
    5.         Dim lRowNum As Long
    6.         Dim lColNum As Long
    7.         lColNum = wsExcel.UsedRange.Columns.Count
    8.         lRowNum = wsExcel.UsedRange.Rows.Count
    9.         If lColNum <> 2 Then
    10.             ValidateExcelFile = False
    11.         Else
    12.             Dim i
    13.  
    14.             For i = 2 To lRowNum
    15.                 If wsExcel.Cells(i, 1).Value = "" Then
    16.                     ValidateExcelFile = False
    17.                     Set wsExcel = Nothing
    18.                     wbExcel.Close  SaveChanges:= False
    19.                     Set wbExcel = Nothing
    20.                     appExcel.Quit
    21.                     Set appExcel = Nothing
    22.                     Err.Raise "100", , modWizard.GetResString("3004")
    23.                     Exit Function
    24.                 End If
    25.             Next
    26.             ValidateExcelFile = True
    27.         End If
    28. 'as this was being done at the end of both parts of the "If", you can simply have it afterwards
    29.         Set wsExcel = Nothing
    30.         wbExcel.Close  SaveChanges:= False
    31.         Set wbExcel = Nothing
    32.         appExcel.Quit
    33.         Set appExcel = Nothing
    34.        
    35.     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.

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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"

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    Smile Re: First thread in this site, problem with reading from excel,help~

    Quote 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:<

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    Cool Re: First thread in this site, problem with reading from excel,help~

    Quote 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:
    1. Public Function ValidateExcelFile(strFilePath As String) As Boolean
    2.         Set appExcel = New Excel.Application
    3.         Set wbExcel = appExcel.Workbooks.Open(strFilePath)
    4.         Set wsExcel = wbExcel.Sheets(1)
    5.         Dim lRowNum As Long
    6.         Dim lColNum As Long
    7.         lColNum = wsExcel.UsedRange.Columns.Count
    8.         lRowNum = wsExcel.UsedRange.Rows.Count
    9.         If lColNum <> 2 Then
    10.             ValidateExcelFile = False
    11.         Else
    12.             Dim i
    13.  
    14.             For i = 2 To lRowNum
    15.                 If wsExcel.Cells(i, 1).Value = "" Then
    16.                     ValidateExcelFile = False
    17.                     Set wsExcel = Nothing
    18.                     wbExcel.Close  SaveChanges:= False
    19.                     Set wbExcel = Nothing
    20.                     appExcel.Quit
    21.                     Set appExcel = Nothing
    22.                     Err.Raise "100", , modWizard.GetResString("3004")
    23.                     Exit Function
    24.                 End If
    25.             Next
    26.             ValidateExcelFile = True
    27.         End If
    28. 'as this was being done at the end of both parts of the "If", you can simply have it afterwards
    29.         Set wsExcel = Nothing
    30.         wbExcel.Close  SaveChanges:= False
    31.         Set wbExcel = Nothing
    32.         appExcel.Quit
    33.         Set appExcel = Nothing
    34.        
    35.     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!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    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.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    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:
    1. Set wbExcel = appExcel.Workbooks.Open(strFilePath)

    and VB says :"Cannot access The file book2.xls." and the app stops.

    Why that happens??? Confused~

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: First thread in this site, problem with reading from excel,help~

    Add the line just before that one:
    VB Code:
    1. Debug.Print strFilePath
    ..and tell us what gets printed to the Immediate window (if you cant see the Immediate window, select it from the "View" menu).

  10. #10

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    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~

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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).

  13. #13
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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"

  14. #14

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    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

  15. #15

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    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 :<<<<

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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:
    1. Set wbExcel = appExcel.Workbooks.Open(strFilePath[u], 0, True[/u])

  17. #17

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    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~

  18. #18

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    Re: First thread in this site, problem with reading from excel,help~

    OK, try it immediately~

  19. #19

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    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.

  20. #20
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  21. #21

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    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

  22. #22
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  23. #23

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    Resolved 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:
    1. Public Function IsExcelFile(strFilePath As String) As Boolean
    2.     Dim connStr As String
    3.     connStr = ConnStringHead & strFilePath & ConnStringEnd
    4.     dim sql
    5.     sql= "select * from [Sheet1$A2:A100]"
    6.  
    7.     On Error Resume Next
    8.    
    9.     adoConn.Open connStr
    10.    
    11.     If Err Then
    12.         IsExcelFile = False
    13.         Exit Function
    14.     Else
    15.         IsExcelFile = True
    16.     End If
    17.        
    18. '--------------------------------------------these two lines are missing
    19.         adoRS.Close
    20.         adoConn.Close
    21. '--------------------------------------------
    22. 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.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    Re: First thread in this site, problem with reading from excel,help~

    Quote 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
  •  



Click Here to Expand Forum to Full Width