|
-
Sep 19th, 2005, 11:39 AM
#1
Thread Starter
Junior Member
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:
Private Sub cmdOpenWorkSheet_Click()
Dim xls As Excel.Application
Dim xlBook As Excel.Workbook
Set xls = New Excel.Application
Set xlBook = xls.Workbooks.Open(App.Path & "\readwritetest.xls")
xlBook.Sheets("sheets").Cells(1, 1) = Me.Text1
End Sub
Private Sub Text1_Click()
Call cmdOpenWorkSheet_Click
End Sub
-
Sep 19th, 2005, 12:09 PM
#2
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 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 
-
Sep 19th, 2005, 12:40 PM
#3
Thread Starter
Junior Member
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".
-
Sep 19th, 2005, 12:58 PM
#4
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 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 
-
Sep 19th, 2005, 01:00 PM
#5
Re: Using VB and Excel
 Originally Posted by lesthan60
VB Code:
'.....
Set xls = New Excel.Application
Set xlBook = xls.Workbooks.Open(App.Path & "\readwritetest.xls")
xlBook.Sheets("sheets").Cells(1, 1) = Me.Text1
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 
-
Sep 19th, 2005, 01:37 PM
#6
Thread Starter
Junior Member
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:
Private Sub cmdOpenWorkSheet_Click()
Dim xls As Excel.Application
Dim xlBook As Excel.Workbook
Set xls = New Excel.Application
Set xlBook = xls.Workbooks.Open(App.Path & "\readwritetest.xls")
[U] xlBook.Sheets("sheets").Cells(1, 1) = Me.Text1[/U]
xlBook.Save
Set xls = Nothing
End Sub
Private Sub Text1_Click()
Call cmdOpenWorkSheet_Click
End Sub
-
Sep 19th, 2005, 01:43 PM
#7
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.
 Originally Posted by lesthan60
VB Code:
xlBook.Save
Set xlBook = nothing 'add this line
Set xls = Nothing
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Sep 19th, 2005, 02:37 PM
#8
Thread Starter
Junior Member
Re: Using VB and Excel
Thanks DKenny but why does runtime error '9' appear? Have I coded that line correctly? text1.value = "test"
-
Sep 19th, 2005, 02:39 PM
#9
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 
-
Sep 19th, 2005, 03:05 PM
#10
Thread Starter
Junior Member
Re: Using VB and Excel
lol, thanks DK - I can read and write to that file from VB.
-
Sep 19th, 2005, 04:07 PM
#11
Thread Starter
Junior Member
Re: Using VB and Excel
DKenny, I still can't close the file. Can you see why?
VB Code:
Private Sub cmdOpenWorkSheet_Click()
Dim xls As excel.Application
Dim xlBook As excel.Workbook
Set xls = New excel.Application
Set xlBook = xls.Workbooks.Open(App.Path & "\readwritetest.xls")
xlBook.Sheets("sheet1").Cells(1, 1) = Me.Text1
Me.Text2 = xlBook.Sheets("sheet1").Cells(2, 1)
xlBook.Save
Set xlBook = Nothing
Set xls = Nothing
End Sub
Private Sub Text1_DblClick()
Call cmdOpenWorkSheet_Click
End Sub
-
Sep 19th, 2005, 04:15 PM
#12
Re: Using VB and Excel
Try replacing
 Originally Posted by lesthan60
VB Code:
xlBook.Save
Set xlBook = Nothing
Set xls = Nothing
with
VB Code:
Application.DisplayAlerts = False
xlBook.Close SaveChanges:=True
Application.DisplayAlerts = True
Set xlBook = Nothing
Set xls = Nothing
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Sep 19th, 2005, 06:00 PM
#13
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|