I have a form with 1 button to create an Excel file on my desktop.
I get error message:
NullReferenceException Was Unhandled
Object reference not set to an instance of an object
and it highlights the code:
WB = excelapp.workbooks.add
I did add the reference "Microsoft excel 14.0" and my full code is below:
imports excel = microsoft.office.interop.excel
dim excelapp as excel.application
dim WB as excel.workbook
sub button1()
WB = excelapp.workbooks.add
excelapp.visible=true
end sub
Missing New on your Excel instance for starters:
Dim xlApp As New Excel.Application
Dim xlWorkbook As Excel.Workbook = xlApp.Workbooks.Add()
Dim xlWorksheet As Excel.Worksheet = CType(xlWorkbook.Sheets("sheet1"), Excel.Worksheet)
xlWorksheet.Cells(1, 1) = "data in first cell"
xlWorksheet.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" & "Test.xlsx")
xlWorkbook.Close()
xlApp.Quit()
xlApp = Nothing
xlWorkBook = Nothing
xlWorkSheet = Nothing
You should probably put this in Try/Catch/Finally block to catch errors in case you run into issues, but mostly because if the program doesn't continue properly finish that code block, an EXCEL.EXE will remain open in your task manager, as well as whatever Excel file it was accessing will be "in use by another program" when you try to access/modify/delete it.
just add one line, then it'll work
imports excel = microsoft.office.interop.excel
dim excelapp as excel.application
dim WB as excel.workbook
sub button1()
excelapp = new excel.application
WB = excelapp.workbooks.add
excelapp.visible=true
end sub
Related
I am trying to write in an already manually opened workbook using VBA from my PowerPoint presentation.
Using CreateObject and then Workbook.Open it opens a new instance of the file.
I'm trying to use GetObject as found in several examples over the web.
Here's my code :
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = Trueme
Set xlWorkbook = xlApp.Workbooks.Open(ActivePresentation.Path & "\Suivi individuel.xlsx", True, False)
xlWorkbook.Sheets(1).Range("C14").Value = "Hello"
SlideShowWindows(1).View.GotoSlide nextSlide
I've tried:
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlWorkbook = GetObject(ActivePresentation.Path & "\Suivi individuel.xlsx", "Excel.Application")
xlWorkbook.Sheets(1).Range("C14").Value = "Hello"
SlideShowWindows(1).View.GotoSlide nextSlide
It says runtime error 432: File Name or Class Name not found during automation operation.
Try:
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlWorkbook = GetObject(ActivePresentation.Path & "\Suivi individuel.xlsx")
If you just want to get an open instance of Excel then
Set xlApp = GetObject(,"Excel.Application")
Set xlWorkbook = xlApp.ActiveWorkbook
Wrap that in an error handler if there's a chance Excel might not be open
See: https://support.microsoft.com/en-us/kb/288902
Ok, I must be missing something subtle here.
In Excel 2010 this VBA works:
Private Sub ExcelTest()
Dim wb As Workbook
For Each wb In Workbooks
Debug.Print wb.Name
Next wb
End Sub
I'm trying to replicate this in Access VBA and I've tried the following approaches in Access 2010 with no success.
Private Sub AccessTest1()
'Derived from http://stackoverflow.com/questions/15958061/controlling-excel-
'workbook-from-access-2010-vba
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Set xlApp = New Excel.Application
For Each xlWB In Excel.Workbooks
Debug.Print xlWB.Name
Next xlWB
'Nothing happens.
'Stepping through and hovering:
'xlApp = "MicroSoft Excel"
'xlWB = Nothing
'xlWB.Name = Object variable or With block variable not set.
'But doesn't throw an error.
End Sub
Private Sub AccessTest2()
'Derived from http://stackoverflow.com/questions/5729195/how-to-refer-to-excel-
'objects-in-access-vba
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Set xlApp = New Excel.Application
For Each wb In Excel.Workbooks '<--- Like this nothing happens
Debug.Print wb.Name
Next wb
'Stepping through and hovering:
'xlApp = "MicroSoft Excel"
'wb = Nothing
'wb.Name = Object variable or With block variable not set. But doesn't throw an error.
For Each wb In xlApp.Workbooks '<--- This throws a "Object variable or
'With block variable not set" error
Debug.Print wb.Name
Next wb
End Sub
Private Sub AccessTest3()
'Derived from http://stackoverflow.com/questions/5729195/how-to-refer-to-excel-
'objects-in-access-vba
Dim objExcelApp As Object
Dim wb As Object
Set objExcelApp = CreateObject("Excel.Application")
Set wb = objExcelApp.Workbook '<--- Throws "Object doesn't support this property
'or method error"
'Hovering after error:
'objExcelApp = "MicroSoft Excel"
'wb = Nothing
For Each wb In objExcelApp.Workbooks
Debug.Print wb.Name
Next wb
End Sub
I definitely have the "Microsoft Excel 14.0 Object Library" reference checked in Access.
I'm just trying to list any open Excel Workbooks so that I can then act against that information.
Thanks for your help in advance.
Set xlApp = New Excel.Application creates a new instance of Excel - and of course you do not have any workbooks in there.
What you want to achive is to go thru an already opened Excel instance. Therefore you have to use getObject().
Another Error is in your For statement ... you have to use xlApp.Workbooks instead of Excel.Workbooks.
The following code should provide the exprected result:
Dim xlApp As Excel.Application
Set xlApp = GetObject(, "Excel.Application")
Dim xlWB As Excel.Workbook
For Each xlWB In xlApp.Workbooks
Debug.Print xlWB.Name
Next xlWB
Set xlApp = Nothing
Set xlWB = Nothing
Please keep also in mind to destry object variables on the end by setting them to Nothing.
I have this code that allows me to export the data contained in the two TextBoxs in an excel spreadsheet, the problem is that the excel file is not created. I put the two statements are not working for rescuing comment below, maybe someone can test it and tell me why it is wrong.
Dim objExcel As New Excel.Application ' Represents an instance of Excel
Dim objWorkbook As Excel.Workbook 'Represents a workbook object
Dim objWorksheet As Excel.Worksheet 'Represents a worksheet object
objWorkbook = objExcel.Workbooks.Add
objWorksheet = CType(objWorkbook.Worksheets.Item(1), Excel.Worksheet)
'This form contains two text boxes, write values to cells A1 and A2
objWorksheet.Cells(1, 1) = TextBox1.Text
objWorksheet.Cells(2, 1) = TextBox2.Text
objWorkbook.Close(False)
'objWorkbook.Save()
'or
'objWorkbook.SaveAs("C:\Temp\Book1.xls")
objExcel.Quit()
Code is tried and tested
Your issue can be from a few different things that I can see. I provided a few issues that I could see from your code you posted.
Your calling .Close before actually saving the workbook.
You didn't specify the "FileFormat" in the save function.
Your not quitting the instance of Excel.
It's important to release ALL objects of Excel you use. If not, it's being held up in memory and will not be released.
Code Below
Dim xlApp As New excel.Application
Dim xlWorkBook As excel.Workbook
Dim xlWorkSheet As excel.Worksheet
Try
xlApp.DisplayAlerts = False
xlWorkBook = xlApp.Workbooks.Add
xlWorkSheet = DirectCast(xlWorkBook.Sheets("Sheet1"), excel.Worksheet)
xlApp.Visible = False 'Don't show Excel; we can and we don't have too
xlWorkSheet.Cells(1, 1) = TextBox1.Text
xlWorkSheet.Cells(2, 1) = TextBox2.Text
xlWorkBook.SaveAs("C:\Temp\Book1.xls", FileFormat:=56) 'Save the workbook
xlWorkBook.Close() 'Close workbook
xlApp.Quit() 'Quit the application
'Release all of our excel objects we used...
ReleaseObject(xlWorkSheet)
ReleaseObject(xlWorkBook)
ReleaseObject(xlApp)
Catch ex As Exception
End Try
Method to Release Excel Objects
Public Shared Sub ReleaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
I wrote the following procedure to check for Microsoft Excel application, if opened. The procedure works fine, except that once it opens the workbook and the activates the sheet, a second instance of Excel tries to open.
Here is my code:
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlWBName As String = "2011.1004.Compensation Template"
For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
If p.ProcessName <> "EXCEL" Then
xlApp.Visible = True
xlBook = xlApp.Workbooks.Open("F:\Test Environment\Compensation Workbook\Compensation Workbook\bin\Debug\" & xlWBName & ".xlsx")
Dim xlSheet As Excel.Worksheet
xlSheet = CType(xlBook.Sheets("SummaryWorksheet"), Worksheet)
xlSheet.Activate()
End If
Next
Use the GetObject method to find an already open Application object:
http://msdn.microsoft.com/en-us/library/e9waz863(v=vs.90).aspx
Dim xlApp As Excel.Application
Try
'get an existing excel.application object
xlApp = GetObject(, "Excel.Application")
Catch ex As Exception
'no existing excel.application object - create a new one
xlApp = New Excel.Application
End Try
Dim xlBook As Excel.Workbook
Dim xlWBName As String = "2011.1004.Compensation Template"
xlApp.Visible = True
xlBook = xlApp.Workbooks.Open("F:\Test Environment\Compensation Workbook\Compensation Workbook\bin\Debug\" & xlWBName & ".xlsx")
Dim xlSheet As Excel.Worksheet
xlSheet = CType(xlBook.Sheets("SummaryWorksheet"), Worksheet)
xlSheet.Activate()
I am tyring to run code like this
xlWorkSheet.Cells(rownumber, 1) = "Some Value"
the problem is when I open the excel document the cell value is not updated.
any help will be appreciated
I use the following code to open the file
Dim pathtofile As String = Server.MapPath("~/UserControls/Upload/MyUpload.xlsx")
Dim xlApp As Excel.Application =New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlworkbooks As Excel.Workbooks
Dim xlWorksheets As Excel.Worksheets
Dim misValue As Object = System.Reflection.Missing.Value
Dim xlWorkSheet As Excel.Worksheet = Nothing
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlworkbooks = xlApp.Workbooks
xlWorkBook = xlworkbooks.Open(pathtofile )
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
You need to save, close, and release the com objects to make sure excel doesn't remain running as a process.
Sub save(filename As String) ' Saves the sheet under a specfic name
xlWorkBook.SaveAs(filename)
End Sub
Sub closexl()
xlWorkBooks.Close()
xlApp.Quit()
End Sub
Sub cleanup() ' Releases all of the com objects to object not have excel running as a process after this method finishes
GC.Collect()
GC.WaitForPendingFinalizers()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing
End Sub