Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help read and write to Excel

Status
Not open for further replies.

mcbeth352001

Technical User
Jul 13, 2006
15
Hi,

I'm trying to write a program to:
1)open an excel file
2)read and write some data to it
3)save and close the file

I am stuck on part 1.

I keep getting the error "Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))" on this line of code aWorkbook = myExcel.Workbooks.Open("C:\IMDATA\MC_NonFinancial.xls")

Background info:
-XP Pro SP2
-VSTO 2005
-MS office 2007 (I have the most current Interop Assemblies)
Program References
-MS Excel 12.0 Object Library
-MS Excel 5.0 Object Libarry


'Current Code
'*******************************************


Dim myExcel As Excel.Application
Dim myExcelPID As Integer
Dim myDataSet As DataSet
'
' Verify the Excel file to populate exists.
'
Dim strExcelFile As String = "C:\IMDATA\MC_NonFinancial.xls"
If Not IO.File.Exists(strExcelFile) Then Return
'
' Start Microsoft Excel and make it visible.
'
myExcel = New Microsoft.Office.Interop.Excel.Application


If myExcel Is Nothing Then
Throw (New Exception("Unable to Start Microsoft Excel."))
Else
myExcel.Visible = True
myExcel.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized
'
' Get the process ID of the Excel instance. This is used
' in the Closing Event to prevent orphaned Excel processes.
'
Dim aProcesses() As Process = Process.GetProcesses
Dim i As Integer

For i = 0 To aProcesses.GetUpperBound(0)
Console.WriteLine(aProcesses(i).MainWindowHandle.ToString + " " + myExcel.Hwnd.ToString)
If aProcesses(i).MainWindowHandle.ToString = myExcel.Hwnd.ToString Then
myExcelPID = aProcesses(i).Id
Exit For
End If
Next
End If

Try
'Open the XLS file and activate the worksheet to populate
Dim strWorkSheetName As String = "Sheet1"
Dim sbExcelData As System.Text.StringBuilder
Dim aWorkbook As Excel.Workbook
Dim aWorkSheet As Excel.Worksheet


aWorkbook = myExcel.Workbooks.Open("C:\IMDATA\MC_NonFinancial.xls")
aWorkSheet = CType(aWorkbook.Sheets(strWorkSheetName), Excel.Worksheet)
aWorkSheet.Activate()
Catch ex As Exception
MsgBox(ex.Message, , "Error Opening Excel File")
CloseExcel(myExcel, myExcelPID)
End Try


Thanks for the help
 
Never mind I found my problem.

I had the references added but I didn't check the box to import the Microsoft.Office.Interop name space which allows you not to have to type "Microsoft.Office.Interop." before your excel objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top