Results 1 to 13 of 13

Thread: Using VB and Excel

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Using VB and Excel

    When I click text1, readwritetest.xls then open readwritetest.xls (read only) cell A1 has not changed. After exiting VB readwritetest.xls remains as read only.

    VB Code:
    1. Private Sub cmdOpenWorkSheet_Click()
    2. Dim xls As Excel.Application
    3. Dim xlBook As Excel.Workbook
    4. Set xls = New Excel.Application
    5. Set xlBook = xls.Workbooks.Open(App.Path & "\readwritetest.xls")
    6. xlBook.Sheets("sheets").Cells(1, 1) = Me.Text1
    7. End Sub
    8.  
    9. Private Sub Text1_Click()
    10. Call cmdOpenWorkSheet_Click
    11. End Sub

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

    Re: Using VB and Excel

    Why are you opening the file that is read only if you want to make a change to it?

    If the workbook is marked as readonly you can save a copy of it as a different workbook name using the .SaveAs method.
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: Using VB and Excel

    These are the steps I took:
    The file was not open.
    I ran the code.
    I tried to open the file to verify an update had occured.
    File was locked for editing.
    Opened the file as read only.
    No update had occured.
    Exited VBE.
    I tried to open the file.
    File was locked for editing.


    I just commented that when I tried to open the file after I had run the code - to verify that an update had occured, it would only open as read only.
    I also noted that after I had exited VBE, the file was still "lock for editing".

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

    Re: Using VB and Excel

    You need to close VB6 and the Excel file and check in the task manager for any other instances of Excel. If you find any and dont have any other workbooks open, then terminate them. Then doubleclick on your excel file and see if it opens as read/write. Also, make sure the file is not marked as read only in the file properties when you right click on the file in Explorer.
    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

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Using VB and Excel

    Quote Originally Posted by lesthan60
    VB Code:
    1. '.....
    2. Set xls = New Excel.Application
    3. Set xlBook = xls.Workbooks.Open(App.Path & "\readwritetest.xls")
    4. xlBook.Sheets("sheets").Cells(1, 1) = Me.Text1
    5. End Sub
    You are creating a 2nd instance of excel, in the background and opening the file in that instance. You never close the file and you never remove that instance of excel. This is why the file is read-only, you still have it open in the other instance.

    You need to add code to
    1/ Close xlBook, svaing your changes.
    2/ Set xls = nothing.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: Using VB and Excel

    Thanks for your replies.
    RobDog888:There were several instances of Excel running. I closed all of them and the file opened normally.

    DKenny: What's wrong with this, after clicking text1, runtime error '9' appears and the underlined code is highlighted.

    VB Code:
    1. Private Sub cmdOpenWorkSheet_Click()
    2. Dim xls As Excel.Application
    3.          Dim xlBook As Excel.Workbook
    4.           Set xls = New Excel.Application
    5.          Set xlBook = xls.Workbooks.Open(App.Path & "\readwritetest.xls")
    6.       [U] xlBook.Sheets("sheets").Cells(1, 1) = Me.Text1[/U]
    7.       xlBook.Save
    8.         Set xls = Nothing
    9. End Sub
    10. Private Sub Text1_Click()
    11.     Call cmdOpenWorkSheet_Click
    12. End Sub

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Using VB and Excel

    What is the value of Me.text1?

    Also you need to clear the xlBook object variable before you clear the xls object variable.

    Quote Originally Posted by lesthan60
    VB Code:
    1. xlBook.Save
    2. Set xlBook = nothing  'add this line
    3. Set xls = Nothing
    4. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: Using VB and Excel

    Thanks DKenny but why does runtime error '9' appear? Have I coded that line correctly? text1.value = "test"

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Using VB and Excel

    This might seems like an obvoious question, but... Does "readwritetest.xls" have a worksheet called "sheets"?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: Using VB and Excel

    lol, thanks DK - I can read and write to that file from VB.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: Using VB and Excel

    DKenny, I still can't close the file. Can you see why?

    VB Code:
    1. Private Sub cmdOpenWorkSheet_Click()
    2. Dim xls As excel.Application
    3.          Dim xlBook As excel.Workbook
    4.           Set xls = New excel.Application
    5.          Set xlBook = xls.Workbooks.Open(App.Path & "\readwritetest.xls")
    6.        xlBook.Sheets("sheet1").Cells(1, 1) = Me.Text1
    7.        Me.Text2 = xlBook.Sheets("sheet1").Cells(2, 1)
    8.  
    9.        xlBook.Save
    10.         Set xlBook = Nothing
    11.         Set xls = Nothing
    12.  
    13. End Sub
    14.  
    15. Private Sub Text1_DblClick()
    16.     Call cmdOpenWorkSheet_Click
    17. End Sub

  12. #12
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Using VB and Excel

    Try replacing
    Quote Originally Posted by lesthan60
    VB Code:
    1. xlBook.Save
    2.         Set xlBook = Nothing
    3.         Set xls = Nothing
    with

    VB Code:
    1. Application.DisplayAlerts = False
    2. xlBook.Close SaveChanges:=True
    3. Application.DisplayAlerts = True
    4. Set xlBook = Nothing
    5. Set xls = Nothing
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: Using VB and Excel

    That seems to have taken care of it, thanks.

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