Error importing and exporting excel data to vb.net, "Microsoft.Office.Interop.Excel.Application is not defined" - vb.net

I am trying to run a file I downloaded to import and export data from excel.
I downloaded from from the following site:
http://www.sattsoft.com/sourcecodes/details/1/9/import-export-data-to-excel-using-vb-net.html
When I run the file the error comes on the following lines
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
These are the errors that come on those respective lines
Type "Microsoft.Office.Interop.Excel.Application is not defined"
Type "Microsoft.Office.Interop.Excel.Workbook is not defined"
Type "Microsoft.Office.Interop.Excel.Worksheet is not defined"
What I want to do is to run the file without the errors so that I can import and export data from excel to vb.net datagridview.
I am using Excel 2010 and visual studio 2010
How do I get around this error so I can run the file?

Add COM reference Microsoft.Office.Interop.Excel to your project, it's called Microsoft Excel XX.Y Object Library (XX.Y is version number depending of installed Excel).

I had to use this library in references from the "Extensions" section (not not under COM); had to select Microsoft.Office.Interop.Excel, version 15.0.0.0.
That fixed the error.
Selecting the COM reference did not resolve this message.

Related

VBA Excel 2010 FormatConditions failing Application Defined or Object Defined Error

I have a VBA code that I used to use in an Excel 2016 (64-bit), and it works fine.
But when I tried to use in a Excel 2010 (32-bit) version, I've got an error "Runtime Error (1004) Application Defined or Object Defined Error" in the following line:
CndFrmtType = Sh.Cells.FormatConditions(CndFrmtItem).Type
I've already tried to re-build the file in a 2010 Excel (from the ground, to avoid reference's issues), but it still not working.
I really tried to google it, but didn't find anything about an eventual incompatibility in the FormatConditions object.
The returned FormatConditions object is as follows:
Can anyone help me on this?
be sure you have:
set Sh to a valid Worksheet object
initialized CndFrmtItem to a valid name or index number to reference a format condition of that worksheet
declared CndFrmtType as of Long type
and it'll work

Getting UCase, Trim, Left libary errors in VBA

I have a application that opens excel files. When I run a macro function in my excel that was opened with the application. I'm getting Compile error "Can't find project or library" on the "UCase" "Trim" "Left" just to name a few. In my macro functions, I have multiply cases of using the above functions. I also have references to "Visual Basic For Application", "Microsoft Excel 12.0 Object Library", "OLE Automation", "Microsoft Office 12.0 Library", Microsoft Forms 2.0 Object Library."
If I run excel by itself without the application, there is no errors. Is there any explanation to why this is happening? Libraries mix match? Works fine for the developer and a few but as for the rest of users, they will get these errors.
It's because those methods belong to Excel Application so that you must call them by preceding their name with the Excel object name (and possibly with its relevant member, too) you must have instantiated before
for instance
example 1: late binding
Option Explicit
Sub LateBindingExcel()
Dim xlApp As Object 'declaring your application object as of "Object" type doesn't require any reference to Excel library
' open an Excel session
Set xlApp = CreateObject("Excel.Application")
' call Excel application WorksheetFunction.Trim()
MsgBox xlApp.WorksheetFunction.Trim(" see how spaces get trimmed by this function ")
End Sub
example 2: early binding
Option Explicit
Sub EarlyBindingExcel()
Dim xlApp As Excel.Application 'declaring your application object as of "Excel.Application" type requires adding Excel library reference to your project
' open an Excel session
Set xlApp = CreateObject("Excel.Application")
' call Excel application WorksheetFunction.Trim()
MsgBox xlApp.WorksheetFunction.Trim(" see how spaces get trimmed by this function ")
End Sub
one sensible difference between the two binding "styles" is that the latter allows you exploiting IntelliSense features while the former doesn't
It is likely due to the 'Visual Basic for Applications' in the Tool reference refers to non-presence library. This may happen if the developer use the library in their customized directory instead of default library.
There are 2 means to solve the issue.
Make global replacement of all string related functions (Format, Left, Right, Mid, Trim etc) with prefix VBA. e.g. VBA.Left. This will force the use of functions within standard library.
Move all your excel sheets to another new workbook and then, select the 'Visual Basic for Applications' from Tool reference.

vb.net word application interop object error

I am using vb.net to create word documents, i have this code:
Imports word = Microsoft.Office.Interop.Word
Imports xl = Microsoft.Office.Interop.Excel
Dim oWord As New word.Application
Dim oDoc As word.Document
oWord.Visible = global_variables.ShowWordDocs
but when running, i am getting this error on the last line:
Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020970-
0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
The value of the last line equals False
i have tried repairing Microsoft Office - all i have installed is word, excel, office shared features and office tools - version 2010
Error implies that there's an issue with the COM interface. Have you tried re-registering (RegSvr32.exe /i ...) the COM DLLs/TLBs/OLBs for Word/Excel? These will be in your Office installation folder (e.g. C:\Program Files\Microsoft Office\Office14)

Reading and Parsing Excel in .NET

I have googled, and rummaged through stackoverflow with no luck on the matter.
I have found countless ways to import an entire excel file into a datagridview, but this is not what I want.
Basically, I have an excel file with multiple users (in one column) and a unique ID in another column. I want to only parse specific users and the corresponding unique ID from that row.
How Can I do this?
There are several ways to read the contents of an Excel file. One possibility is to use Excel's object model through the Excel Interop API. Using this approach, you'll interact with Excel using objects:
Imports Microsoft.Office.Interop
Public Class ExcelReader
Public Sub Read(filename As String)
Dim excel As New Excel.Application
Dim book As Excel.Workbook = excel.Workbooks.Open(filename)
Dim sheet As Excel.Worksheet = book.Worksheets("Sheet1")
Dim first_cell_text As String = sheet.Cells(1, 1).Value().ToString
Dim a_number As Integer = sheet.Cells(1, 2).value
End Sub
End Class
You need to add a reference to the Excel interop DLL, which on my machine is located at C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll. I am pretty sure you need to have Excel installed on any machine that runs the app, too.
Documentation for the Excel object model is available here.
Other options include reading an Excel file as if it were a database using ADO.NET, and using third party libraries that can read Excel.

vb.net windows form connection excelworkbook

I'm creating a vb.net excellworkbook.xlsx file and worksheet from windowsform want to achieve. I could not. I do not make a connection between vb.net and windowsform. I need to use in my project, but dozens excelworkbook excelworkbook excel in the program I need to do without opening the windows forms and links to all the relevant supposed to do. I would be glad if you can help.
Frankly, I have almost no clue what you're asking. Almost no clue anyway.
Access, read and write an excel workbook in Visual Studio vb.net by doing the following:
Click Project > Add Reference > COM > Microsoft Excel Object Library
In your code you will need:
Imports Microsoft.Office.Interop
Then you can create the excel object:
Dim ExcelAp As New Excel.Application
Open a workbook:
Dim WrkBk As Excel.Workbook = ExcelAp.Workbooks.Open("Path")
And then access cells and their values:
WrkBk.Cells(1, 1).value = "Hello World!"
That might give you a start anyway.