-
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
-
Re: open excel worksheet in vb.net
-
Re: open excel worksheet in vb.net
-
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.
-
Re: open excel worksheet in vb.net
Did you add a reference to Excel?
-
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
-
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.
-
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
-
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)
-
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.
-
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)
-
Re: open excel worksheet in vb.net
I am running 2003 version
-
Re: open excel worksheet in vb.net
-
Re: open excel worksheet in vb.net
Quote:
Originally Posted by RobDog888
See post #9.
still not working...still comes up with the member not found message
-
Re: open excel worksheet in vb.net
Which line is the error message pointing to?
-
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
-
Re: open excel worksheet in vb.net
VB Code:
oWorkSheet = oWorkBook.Worksheets.Items(3)
-
Re: open excel worksheet in vb.net
try:
oWorkSheet = oWorkBook.Worksheets("myWorkSheetName")
oWorkSheet.Activate()
-
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)
-
Re: open excel worksheet in vb.net
Quote:
Originally Posted by bgard68
try:
oWorkSheet = oWorkBook.Worksheets("myWorkSheetName")
oWorkSheet.Activate()
no, still the same error message
-
Re: open excel worksheet in vb.net
What version of Excel are you running ?
-
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.
-
Re: open excel worksheet in vb.net
Quote:
Originally Posted by bgard68
What version of Excel are you running ?
also 2003 for excel
-
Re: open excel worksheet in vb.net
Quote:
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
-
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)
-
Re: open excel worksheet in vb.net
Quote:
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?
-
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
-
Re: open excel worksheet in vb.net
Quote:
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?
-
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()
-
Re: open excel worksheet in vb.net
Quote:
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)
-
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
-
Re: open excel worksheet in vb.net
still the same error message
-
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?
-
Re: open excel worksheet in vb.net
Quote:
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
-
Re: open excel worksheet in vb.net
repost your this function again. The whole function.
-
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
-
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)
-
Re: open excel worksheet in vb.net
done that, still says the same error message.."member not found"
-
Re: open excel worksheet in vb.net
You never confirmed how many sheets you have in your workbook? The Items collection is zero based.
-
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