|
-
Mar 20th, 2006, 11:46 AM
#1
Thread Starter
Junior Member
open excel worksheet in vb.net
I can open the workbook but cant open the worksheet i need.
Here is the code i have so far.....
Private Sub btnstaff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstaff.Click
Dim ApExcel As Object 'To open Excel
ApExcel = CreateObject("Excel.application") 'Creates an object
ApExcel.Visible = True ' So you can see Excel
ApExcel.Workbooks.open("C:\documents and settings\mike\desktop\dss.xls")
I want to be able to define the worksheet (in this case, sheet 3)
Please help
-
Mar 20th, 2006, 11:52 AM
#2
Addicted Member
Re: open excel worksheet in vb.net
Using Framework 1.1, VB.Net 2003 unless I
state otherwise 
-
Mar 20th, 2006, 12:03 PM
#3
Re: open excel worksheet in vb.net
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 
-
Mar 20th, 2006, 12:08 PM
#4
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
ok...i have tried the following but keep getting error message saying "member not found" which is found on the line with a * next to it.
Dim oExcel As Object
Dim oWorkBook As Excel.Workbook
Dim oWorkSheet As Excel.Worksheet
oExcel = CreateObject("Excel.application")
oExcel.Visible = True
oWorkBook = oExcel.Workbooks.Open("C:\documents and settings\mike\desktop\dss.xls")
* oWorkSheet = oWorkBook.Worksheets(3)*
oWorkSheet.Activate()
with regard to the (3) after worksheets, i have tried various different ways, all return the same error message.
I dont want to add any data to any cell, i just want to be able to view different sheets.
Last edited by mike7510uk; Mar 20th, 2006 at 12:15 PM.
-
Mar 20th, 2006, 12:14 PM
#5
Re: open excel worksheet in vb.net
Did you add a reference to Excel?
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 
-
Mar 20th, 2006, 12:16 PM
#6
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
yes i did add the reference.
But just in case i did it wrong, please can you explain how i should have done it
thanks
-
Mar 20th, 2006, 12:18 PM
#7
Re: open excel worksheet in vb.net
Which version are you running of VB?
For 2003 its Project > Add Reference... > Select MS Excel xx.0 Object Library > Ok.
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 
-
Mar 20th, 2006, 12:25 PM
#8
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
i am running VB.net
I did do it right before, so yes i have added the refernece but am still getting the same "member not found" message
-
Mar 20th, 2006, 12:31 PM
#9
Addicted Member
Re: open excel worksheet in vb.net
Do you have 4 worksheets or 3 worksheets?
If you have 3 worksheets then try:
'Zero based!
oWorkSheet = oWorkBook.Worksheets(2)
Using Framework 1.1, VB.Net 2003 unless I
state otherwise 
-
Mar 20th, 2006, 12:32 PM
#10
Re: open excel worksheet in vb.net
There are 3 different year versions and multiple types of VB.NET. You can have 2002, 2003, 2005. And also, Express, Standard, Pro, Architect, etc.
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 
-
Mar 20th, 2006, 12:34 PM
#11
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
i have 6 worksheets, i can get Excel to open if i leave out the.....
'oWorkSheet = oWorkBook.Worksheets(3)
'oWorkSheet.Activate()
....code. Just cant go to the sheet i want. ( I have six sheets and six buttons, each button should open up the correct sheet)
-
Mar 20th, 2006, 12:35 PM
#12
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
I am running 2003 version
-
Mar 20th, 2006, 12:37 PM
#13
Re: open excel worksheet in vb.net
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 
-
Mar 20th, 2006, 12:41 PM
#14
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
 Originally Posted by RobDog888
See post #9.
still not working...still comes up with the member not found message
-
Mar 20th, 2006, 12:43 PM
#15
Re: open excel worksheet in vb.net
Which line is the error message pointing to?
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 
-
Mar 20th, 2006, 12:45 PM
#16
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
* oWorkSheet = oWorkBook.Worksheets(3)*
thats the error line. (the (3) i have tried alternatives but still the same outcome
-
Mar 20th, 2006, 12:47 PM
#17
Re: open excel worksheet in vb.net
VB Code:
oWorkSheet = oWorkBook.Worksheets.Items(3)
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 
-
Mar 20th, 2006, 12:51 PM
#18
Addicted Member
Re: open excel worksheet in vb.net
try:
oWorkSheet = oWorkBook.Worksheets("myWorkSheetName")
oWorkSheet.Activate()
Using Framework 1.1, VB.Net 2003 unless I
state otherwise 
-
Mar 20th, 2006, 12:53 PM
#19
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
Dim oExcel As Object
Dim oWorkBook As Excel.Workbook
Dim oWorkSheet As Excel.Worksheet
oExcel = CreateObject("Excel.application")
oExcel.Visible = True
oWorkBook = oExcel.Workbooks.Open("C:\documents and settings\mike\desktop\dss.xls")
oWorkSheet = oWorkBook.Worksheets.Items(3)
oWorkSheet.Activate()
the above code is all the code i have relating to the button...
the same error message appears (even after i did the .items(3) bit)
-
Mar 20th, 2006, 12:55 PM
#20
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
 Originally Posted by bgard68
try:
oWorkSheet = oWorkBook.Worksheets("myWorkSheetName")
oWorkSheet.Activate()
no, still the same error message
-
Mar 20th, 2006, 12:56 PM
#21
Addicted Member
Re: open excel worksheet in vb.net
What version of Excel are you running ?
Using Framework 1.1, VB.Net 2003 unless I
state otherwise 
-
Mar 20th, 2006, 12:59 PM
#22
Re: open excel worksheet in vb.net
VB Code:
oWorkSheet = oWorkBook.Sheets.Items(2)
Forgot to make the adjustment to sheet 3. Try the Sheets collection instead.
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 
-
Mar 20th, 2006, 12:59 PM
#23
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
 Originally Posted by bgard68
What version of Excel are you running ?
also 2003 for excel
-
Mar 20th, 2006, 01:04 PM
#24
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
 Originally Posted by RobDog888
VB Code:
oWorkSheet = oWorkBook.Sheets.Items(2)
Forgot to make the adjustment to sheet 3. Try the Sheets collection instead.
still the same message
-
Mar 20th, 2006, 01:08 PM
#25
Re: open excel worksheet in vb.net
Not sure whats different in your workbook but this is what I usually do from my link I posted earlier.
VB Code:
Dim oWB As Excel.Workbook = moApp.Workbooks.Add
Dim oSht As Excel.Worksheet = DirectCast(oWB.Sheets("Sheet1"), Excel.Worksheet)
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 
-
Mar 20th, 2006, 01:19 PM
#26
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
 Originally Posted by RobDog888
Not sure whats different in your workbook but this is what I usually do from my link I posted earlier.
VB Code:
Dim oWB As Excel.Workbook = moApp.Workbooks.Add
Dim oSht As Excel.Worksheet = DirectCast(oWB.Sheets("Sheet1"), Excel.Worksheet)
where does "moApp" come from?
-
Mar 20th, 2006, 01:26 PM
#27
Hyperactive Member
Re: open excel worksheet in vb.net
its been awhile, but I think you need to cast everthing.
Here is some stuff I pulled out of my code VB.net 2003 & office 2003
VB Code:
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = CType(xlApp.Workbooks.Open(sFileName), Excel.Workbook)
xlSheet = CType(xlApp.ActiveSheet, Excel.Worksheet)
xlSheet = CType(xlBook.Worksheets.Item(1), Excel.Worksheet
Last edited by PENNYSTOCK; Mar 20th, 2006 at 01:32 PM.
-
Mar 20th, 2006, 01:29 PM
#28
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
 Originally Posted by PENNYSTOCK
its been awhile, but I think you need to cast everthing.
Here is some stuff I pulled out of my code VB.net 2003 & office 2003
VB Code:
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = CType(xlApp.Workbooks.Open(sFileName), Excel.Workbook)
xlSheet = CType(xlApp.ActiveSheet, Excel.Worksheet)
where does that code go?
-
Mar 20th, 2006, 01:36 PM
#29
Hyperactive Member
Re: open excel worksheet in vb.net
Like this.
VB Code:
Dim oExcel As Object
Dim oWorkBook As Excel.Workbook
Dim oWorkSheet As Excel.Worksheet
oExcel = ctype(CreateObject("Excel.application"), Excel.Application)
oExcel.Visible = True
oWorkBook = ctype(oExcel.Workbooks.Open("C:\documents and settings\mike\desktop\dss.xls"), Excel.Workbook)
oWorkSheet = ctype(oWorkBook.Worksheets.Items(3),Excel.Worksheet)
oWorkSheet.Activate()
-
Mar 20th, 2006, 01:39 PM
#30
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
 Originally Posted by PENNYSTOCK
Like this.
VB Code:
Dim oExcel As Object
Dim oWorkBook As Excel.Workbook
Dim oWorkSheet As Excel.Worksheet
oExcel = ctype(CreateObject("Excel.application"), Excel.Application)
oExcel.Visible = True
oWorkBook = ctype(oExcel.Workbooks.Open("C:\documents and settings\mike\desktop\dss.xls"), Excel.Workbook)
oWorkSheet = ctype(oWorkBook.Worksheets.Items(3),Excel.Worksheet)
oWorkSheet.Activate()
it still says "member not found" (refering to the 'oWorkSheet = ctype(oWorkBook.Worksheets.Items(3),Excel.Worksheet)' line)
-
Mar 20th, 2006, 01:40 PM
#31
Hyperactive Member
Re: open excel worksheet in vb.net
One last thing to try that I can see.
Change
Dim oExcel As Object
to
Dim oExcel As Excel.Application
-
Mar 20th, 2006, 01:43 PM
#32
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
still the same error message
-
Mar 20th, 2006, 01:47 PM
#33
Hyperactive Member
Re: open excel worksheet in vb.net
Since you have visable set to True, do you see the excel file opening? Do you exit excel before you re-run the program?
-
Mar 20th, 2006, 01:52 PM
#34
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
 Originally Posted by PENNYSTOCK
Since you have visable set to True, do you see the excel file opening? Do you exit excel before you re-run the program?
yes, excel opens, then the error message, when the error message is cancelled, i have to close excel. so it opens, just not on the sheet i want
-
Mar 20th, 2006, 01:59 PM
#35
Hyperactive Member
Re: open excel worksheet in vb.net
repost your this function again. The whole function.
-
Mar 20th, 2006, 02:03 PM
#36
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
Private Sub btnstaff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstaff.Click
Dim oExcel As Object
Dim oWorkBook As Excel.Workbook
Dim oWorkSheet As Excel.Worksheet
oExcel = CType(CreateObject("Excel.application"), Excel.Application)
oExcel.Visible = True
oWorkBook = CType(oExcel.Workbooks.Open("C:\documents and settings\mike\desktop\dss.xls"), Excel.Workbook)
oWorkSheet = CType(oWorkBook.Worksheets.Items(3), Excel.Worksheet)
oWorkSheet.Activate()
End Sub
-
Mar 20th, 2006, 02:13 PM
#37
Hyperactive Member
Re: open excel worksheet in vb.net
take the "s" of item
VB Code:
oWorkSheet = CType(oWorkBook.Worksheets.Items(3), Excel.Worksheet)
should be
oWorkSheet = CType(oWorkBook.Worksheets.Item(3), Excel.Worksheet)
-
Mar 20th, 2006, 02:17 PM
#38
Thread Starter
Junior Member
Re: open excel worksheet in vb.net
done that, still says the same error message.."member not found"
-
Mar 20th, 2006, 02:26 PM
#39
Re: open excel worksheet in vb.net
You never confirmed how many sheets you have in your workbook? The Items collection is zero based.
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 
-
Mar 20th, 2006, 02:33 PM
#40
Hyperactive Member
Re: open excel worksheet in vb.net
What excel do you running based on your references.
I have all three of these in my refs:
Excel 11 Ver 1.5
microsoft.office.core 11 Ver 2.3
VBIDE 5.3 ver 5.3
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
|