Discussion:
Programing Excel with winForm project, not VSTO
(too old to reply)
Øyvind
2006-11-09 20:15:02 UTC
Permalink
I am learning VB2005 and interoptibility with Excel. I have some experience
from VB6, but VB2005 is new to me.

the project hav reference to MS excel 11
Imports Microsoft.Office.Interop
Imports Excel = Microsoft.Office.Interop.Excel

'' Program starts and loads up an Excel workbook and
the problem starts when i tries to access a cell i Excel.

Dim MyFileName As String = ""
Dim MyResFrom_Xls_WB As String = ""

Dim ActualRow As Integer = 10
Dim ActualCol As Integer = 10
Dim xls_App As New Excel.Application
Dim xls_wb As Excel.Workbook
Dim xls_Sheet As Excel.Worksheet = Nothing
' Dim xls_Range As Microsoft.Office.Interop.Excel.Range


MyFileName = Me.fbxSalgstall.Path + "\" + Me.txtFileNavn.Text

xls_App = New Excel.Application()
xls_wb = xls.Workbooks.Open(MyFileName)
xls_App.Visible = True

MyResFrom_Xls_WB = xls_Sheet.Cells(ActualRow, ActualCol) ' = Error;
Object reference not set to an instance of an object.

MessageBox.Show(MyResFrom_Xls_WB)


End Sub

If somebody have a proposal. This code with rfrence to a cell was running
inside a VSTO project.

If somebody have any proposal ? I am happy :-)
--
Øyvind
Øyvind
2006-11-10 08:31:02 UTC
Permalink
The code I posted yesterday was rewritten and contains several errors.

I change the question to make it easier to understand.

Line of code:
MyResFrom_Xls_WB = xls_sh.Cells(CInt(ActualRow), CInt(ActualCol))

Error received from VS -VB2006
Unable to cast COM object of type
'Microsoft.Office.Interop.Excel.WorksheetClass' to interface type
'Microsoft.Office.Interop.Excel._Worksheet'. This operation failed because
the QueryInterface call on the COM component for the interface with IID
'{000208D8-0000-0000-C000-000000000046}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE)).



The is a late binding topic. And this line reads the contents of a singe
cell in excel2003 sheet with VB2005 code.

I have serced the web, purcahsed books and worked a lot to solve this. If
you try this line in a VSTO project this line are able to reed from a cell.
New VSTO is great, but it’s not the rigt tool when you just have to open,
read and close a spredsheet in a computers memory without any interaction
with a user in simple a batch operation.

I hope that someone are able to help ? 
--
Øyvind
Post by Øyvind
I am learning VB2005 and interoptibility with Excel. I have some experience
from VB6, but VB2005 is new to me.
the project hav reference to MS excel 11
Imports Microsoft.Office.Interop
Imports Excel = Microsoft.Office.Interop.Excel
'' Program starts and loads up an Excel workbook and
the problem starts when i tries to access a cell i Excel.
Dim MyFileName As String = ""
Dim MyResFrom_Xls_WB As String = ""
Dim ActualRow As Integer = 10
Dim ActualCol As Integer = 10
Dim xls_App As New Excel.Application
Dim xls_wb As Excel.Workbook
Dim xls_Sheet As Excel.Worksheet = Nothing
' Dim xls_Range As Microsoft.Office.Interop.Excel.Range
MyFileName = Me.fbxSalgstall.Path + "\" + Me.txtFileNavn.Text
xls_App = New Excel.Application()
xls_wb = xls.Workbooks.Open(MyFileName)
xls_App.Visible = True
MyResFrom_Xls_WB = xls_Sheet.Cells(ActualRow, ActualCol) ' = Error;
Object reference not set to an instance of an object.
MessageBox.Show(MyResFrom_Xls_WB)
End Sub
If somebody have a proposal. This code with rfrence to a cell was running
inside a VSTO project.
If somebody have any proposal ? I am happy :-)
--
Øyvind
Øyvind
2006-11-11 09:21:01 UTC
Permalink
Now I have a solution to my problem.
1. First the error from VS.
2. Then the line who creted the problem =Solution
3. Code

Error reporting
{"Conversion from type 'Range' to type 'String' is not valid."}
at Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Object Value)
at ConsoleApplicationExcelLesFil.Module1.Main() in C:\Documents and
Settings\Utvikling\VB2005\ELITE\ReadXLSDataConsol\ConsoleApplicationExcelLesFil\ConsoleApplicationExcelLesFil\Module1.vb:line 24
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()"

solution : StrFromXLS = Ark.Cells(10, 5).value
code in Viual studio
‘This is a console project in visual studio.

Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop.Excel
Imports excel = Microsoft.Office.Interop.Excel
Imports System.Windows.Forms

Module Module1
Private exitXLS As Boolean = False
Dim WithEvents myExcelApp As excel.Application



Sub Main()
Dim XLS_App As Microsoft.Office.Interop.Excel.ApplicationClass
Dim MySheet As Microsoft.Office.Interop.Excel.Worksheet
Dim StrFromXLS As String

XLS_App = CreateObject("Excel.application")
XLS_App.Visible = True
XLS_App.StatusBar = "Open a new file"
XLS.Workbooks.Open("C:\MyPath\MyWorkBook.xls")
Ark = XLS_app.ActiveSheet

StrFromXLS = Ark.Cells(10, 5).value

MessageBox.Show(StrFromXLS)

While exitXLS = False
System.Windows.Forms.Application.DoEvents()
End While
End Sub

Private Sub myExcelApp_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal
Target As Microsoft.Office.Interop.Excel.Range, ByRef Cancel As Boolean)
Handles myExcelApp.SheetBeforeDoubleClick
exitXLS = True
End Sub
End Module
--
Øyvind
Post by Øyvind
The code I posted yesterday was rewritten and contains several errors.
I change the question to make it easier to understand.
MyResFrom_Xls_WB = xls_sh.Cells(CInt(ActualRow), CInt(ActualCol))
Error received from VS -VB2006
Unable to cast COM object of type
'Microsoft.Office.Interop.Excel.WorksheetClass' to interface type
'Microsoft.Office.Interop.Excel._Worksheet'. This operation failed because
the QueryInterface call on the COM component for the interface with IID
No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE)).
The is a late binding topic. And this line reads the contents of a singe
cell in excel2003 sheet with VB2005 code.
I have serced the web, purcahsed books and worked a lot to solve this. If
you try this line in a VSTO project this line are able to reed from a cell.
New VSTO is great, but it’s not the rigt tool when you just have to open,
read and close a spredsheet in a computers memory without any interaction
with a user in simple a batch operation.
I hope that someone are able to help ? 
--
Øyvind
Post by Øyvind
I am learning VB2005 and interoptibility with Excel. I have some experience
from VB6, but VB2005 is new to me.
the project hav reference to MS excel 11
Imports Microsoft.Office.Interop
Imports Excel = Microsoft.Office.Interop.Excel
'' Program starts and loads up an Excel workbook and
the problem starts when i tries to access a cell i Excel.
Dim MyFileName As String = ""
Dim MyResFrom_Xls_WB As String = ""
Dim ActualRow As Integer = 10
Dim ActualCol As Integer = 10
Dim xls_App As New Excel.Application
Dim xls_wb As Excel.Workbook
Dim xls_Sheet As Excel.Worksheet = Nothing
' Dim xls_Range As Microsoft.Office.Interop.Excel.Range
MyFileName = Me.fbxSalgstall.Path + "\" + Me.txtFileNavn.Text
xls_App = New Excel.Application()
xls_wb = xls.Workbooks.Open(MyFileName)
xls_App.Visible = True
MyResFrom_Xls_WB = xls_Sheet.Cells(ActualRow, ActualCol) ' = Error;
Object reference not set to an instance of an object.
MessageBox.Show(MyResFrom_Xls_WB)
End Sub
If somebody have a proposal. This code with rfrence to a cell was running
inside a VSTO project.
If somebody have any proposal ? I am happy :-)
--
Øyvind
Loading...