Opening an excel file using VB.Net - excel

I am getting an error Object reference not set to an instance of an object when I write the following code in the line owb = oxl.Workbooks.Open("G:\NTPC.xlsx"). There is no problem with the file and it indeed exists in the G drive of my pc. Pls tell where I am going wrong?
Imports System
Imports Microsoft.Office.Interop
Module Program
Dim oxl As Excel.Application
Dim owbs As Excel.Workbooks
Dim owb As Excel.Workbook
Dim osheets As Excel.Worksheets
Dim osheet As Excel.Worksheet
Sub Main(args As String())
'oxl = CreateObject("Excel.Application")
'oxl.DisplayAlerts = True
'oxl.Visible = True
owb = oxl.Workbooks.Open("G:\NTPC.xlsx")
End Sub
End Module

You need to create an Excel Application instance in the code first.
Imports System
Imports Microsoft.Office.Interop
Module Program
Dim oxl As Excel.Application
Dim owbs As Excel.Workbooks
Dim owb As Excel.Workbook
Dim osheets As Excel.Worksheets
Dim osheet As Excel.Worksheet
Sub Main(args As String())
oxl = CreateObject("Excel.Application")
'oxl.DisplayAlerts = True
oxl.Visible = True
owb = oxl.Workbooks.Open("G:\NTPC.xlsx")
End Sub
End Module

Related

Invalid Cast Exception was unhandled Excel Application

Dim oExcel As Excel.ApplicationClass = New Excel.ApplicationClass
Dim objWS As New Microsoft.Office.Interop.Excel.Worksheet
Dim oBook As Excel.WorkbookClass
Dim oBooks As Excel.WorkbookClass
'Start Excel and open the workbook.
oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oBooks = oExcel.Workbooks
oBook = oBooks.Open("H:\Copy of Book1.xlsm")
Hi there! I have an error of Invalid Cast Exception at oExcel = CreateObject("Excel.Application")
I'm using Visual Basic and I'm trying to open my excel file which is named Copy of Book1. I'm also using Microsoft Excel 2010. Any idea how to fix that error?
Thank you in advance!
As said in the comment if you just want to start Excel and open a file change the code as follows
Dim oExcel As Excel.Application = New Excel.Application
Dim oBook As Excel.Workbook
'Start Excel and open the workbook.
oExcel.Visible = True
oBook = oExcel.Workbooks.Open("H:\Copy of Book1.xlsm")
Try this:
Sub openExcel()
Dim objExcel As Excel.Application
Set objExcel = CreateObject("Excel.Application")
Dim ws As Worksheet
Dim wb As Workbook
objExcel.Visible = True
Set wb = objExcel.Workbooks.Open("H:\Copy of Book1.xlsm")
End Sub

Cannot access read-only document .xlsx

I have a datagrtidview.
I created a button "Export datagridview to Excel"
Below is the code for the export:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
xlWorkSheet.SaveAs("C:\Masterlist.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("You can find the file in C:\Masterlist.xlsx")
End Sub
I have also included reference: Microsoft.Office.Interop.Excel
However when I finished my project as an application, I install my application on other computer and this happens when I click on "Export to Excel" button:
https://i.stack.imgur.com/F45Q5.png
What code that can disable read-only?
Or any code here that makes this error appears?
Thanks!

Can you return a excel Object to use in a Sub

I've have this question, can you return a instance/object of excel to use in a sub
What I mean is instead of writing the same code, over and over every time I need to open an excel document. I would like to call a function, that open excel something like this
Public Function TestExcel(ByVal filePath As String) As Object
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(filePath)
Set oSheet = oBook.Sheets(1)
Set TestExcel = oSheet
End Function
Sub test()
Dim oSheet As Object
Set oSheet = TestExcel("C:\Users\MYfile")
test = oSheet.Range("B" & excelRow).Value
'Or something like this
number = 10
osheet.Range("B" & excelRow).Value = number
End Sub
function functionname(args) as excel.workbook and in this function, generally toward the end, you say set functionname = oBook using your post
For example
Function OpenWorkBook(strPath as string) as Excel.workbook
set OpenWorkbook=workbooks.open (strPath)
end function
and its use
dim x as excel.workbook
set x=openworkbook("c:\test.xlsx")

VBA - returning a reference to a WorkBook

I want to call a function that returns a Workbook or reference to that workbook. However I'm unable to set the value 'wb' with the workbook returned from the function.
Public Sub TestScript()
Dim wb As Workbook
wb = GetWorkBook()
End Sub
Function GetWorkBook() As Workbook
Dim db2 As Workbook
Dim xlApp As Excel.Application
Set xlApp = GetObject(, "Excel.Application")
Dim xlWB As Excel.Workbook
For Each xlWB In xlApp.Workbooks
If xlWB.Name = "Test.XLSX" Then
Set db2 = xlWB
End If
Next xlWB
Set xlApp = Nothing
Set xlWB = Nothing
GetWorkBook = db2
End Function
Gives:
Runtime Error 91:
Object Variable or With Block Variable not set
You forgot to use the set statement two times:
Public Sub TestScript()
Dim wb As Workbook
Set wb = GetWorkBook()
End Sub
Function GetWorkBook() As Workbook
Dim db2 As Workbook
Dim xlApp As Excel.Application
Set xlApp = GetObject(, "Excel.Application")
Dim xlWB As Excel.Workbook
For Each xlWB In xlApp.Workbooks
If xlWB.Name = "Test.XLSX" Then
Set db2 = xlWB
End If
Next xlWB
Set xlApp = Nothing
Set xlWB = Nothing
Set GetWorkBook = db2
End Function
Whenever you are assigning an object to a variable (reference it thereby) then you have to use the set statement (https://stackoverflow.com/a/349636/1153513).
Examples:
Set rngYourRangeVariable = Thisworkbook.Worksheets("Sheet1").Range("A1:C4")
Set shtSomeSheet = Thisworkbook.Worksheet("Sheet1")
Set conSomeADOconnection = New ADODB.Connection

Access VBA How to add new sheets to excel?

I am running a few modules of code in access and am writing data into
Excel. When I write the first time, data gets written properly. But again
when I try, the new data is written on top of the old data. What should I do to
insert a new sheet?
My existing code is
Dim objexcel As Excel.Application
Dim wbexcel As Excel.Workbook
Dim wbExists As Boolean
Dim objSht As Excel.Worksheet
Dim objRange As Excel.Range
Set objexcel = CreateObject("excel.Application")
On Error GoTo Openwb
wbExists = False
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT1.xls")
Set objSht = wbexcel.Worksheets("Sheet1")
objSht.Activate
wbExists = True
Openwb:
On Error GoTo 0
If Not wbExists Then
objexcel.Workbooks.Add
Set wbexcel = objexcel.ActiveWorkbook
Set objSht = wbexcel.Worksheets("Sheet1")
End If
I think that the following code should do what you want. It's very similar to yours, except it uses the return values from the .Add methods to get the objects you want.
Public Sub YourSub()
Dim objexcel As Excel.Application
Dim wbexcel As Excel.Workbook
Dim wbExists As Boolean
Set objexcel = CreateObject("excel.Application")
'This is a bad way of handling errors. We should'
'instead check for the file existing, having correct'
'permissions, and so on, and actually stop the process'
'if an unexpected error occurs.'
On Error GoTo Openwb
wbExists = False
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT1.xls")
wbExists = True
Openwb:
On Error GoTo 0
If Not wbExists Then
Set wbexcel = objexcel.Workbooks.Add()
End If
CopyToWorkbook wbexcel
EndSub
Private Sub CopyToWorkbook(objWorkbook As Excel.Workbook)
Dim newWorksheet As Excel.Worksheet
set newWorksheet = objWorkbook.Worksheets.Add()
'Copy stuff to the worksheet here'
End Sub

Resources