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
Related
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
I am copying a table from Outlook to Excel. The code I have found online copies the table in a new Excel file.
I want to copy the table into an existing Excel file.
Here is the code I am running in Outlook.
Sub dd()
Dim item As MailItem, x%
Dim r As Object 'As Word.Range
Dim doc As Object 'As Word.Document
Dim xlApp As Object, wkb As Object
Set xlApp = CreateObject("Excel.Application")
Set wkb = xlApp.Workbooks.Add
xlApp.Visible = True
Dim wks As Object
Set wks = wkb.Sheets(1)
For Each item In Application.ActiveExplorer.Selection
Set doc = item.GetInspector.WordEditor
For x = 1 To doc.Tables.Count
Set r = doc.Tables(x)
r.Range.Copy
wks.Paste
wks.Cells(wks.Rows.Count, 1).End(3).Offset(1).Select
Next
Next
End Sub
The code here
Set wkb = xlApp.Workbooks.Add
is what opens the new workbook. Try replacing this line with something like
Set wkb = xlApp.Workbooks.Open("C:\PathToExcel\File.xlsx")
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
I occasionally get a run time error when trying to open/manipulate Excel files using Access VBA. The error is
"Run-Time error '462': The remote server machine does not exist or is
unavailable
What is frustrating is that the error occurs only for certain files and not others and in different instances. Here is my code, the error occurs at the workbooks.open(sPath) line:
DoCmd.SetWarnings False
Dim oExcel As New Excel.Application
Dim oWB As Workbook
Dim oWS As Worksheet
Set oExcel = Excel.Application
Set oWB = oExcel.Workbooks.Open(sPath)
Set oWS = oWB.Sheets(1)
oExcel.Visible = False
If fGetFileName(sPath) = "FILE_NAME1.xlsx" Then
'oExcel.Visible = False
oWS.Range("AW1").Value = "TEXT1"
oWS.Range("AX1").Value = "TEXT2"
oWS.Range("AY1").Value = "TEXT3"
End If
oWB.Save
Debug.Print "Amended " & sPath
oWB.Close False
Set oWB = Nothing
oExcel.Quit
Set oExcel = Nothing
DoCmd.SetWarnings True
After a bit of research online, I've found this document gives a good overview of the error: https://anictteacher.files.wordpress.com/2011/11/vba-error-462-explained-and-resolved.pdf
Using the logic from that document, the error is that:
object has not been fully qualified by reference to the Office object
in every case
However, I amended the row where the error occurs to specifically reference the Excel object (Set oWB = oExcel.Workbooks.Open(sPath)). Have tried declaring the dimensions as Objects and put reference to oExcel in every mention of a workbook/sheet. Any ideas? Does sPath need to better qualified?
You declare oExcel as a new Excel.Application:
Dim oExcel As New Excel.Application
That means later in your code, Set oExcel = Excel.Application is not really useful ... because oExcel is already a reference to Excel.Application. Add in a MsgBox to demonstrate what oExcel is at that point ...
MsgBox "TypeName(oExcel): " & TypeName(oExcel)
'Set oExcel = Excel.Application
Consider a different approach for your Excel object variable. Test this stripped down procedure (the purpose is only to see whether it eliminates your current error):
Dim oExcel As Excel.Application
Dim oWB As Excel.Workbook
DoCmd.SetWarnings True
MsgBox "TypeName(oExcel): " & TypeName(oExcel)
Set oExcel = New Excel.Application
MsgBox "TypeName(oExcel): " & TypeName(oExcel)
Set oWB = oExcel.Workbooks.Open(sPath)
oWB.Close False
Set oWB = Nothing
oExcel.Quit
Set oExcel = Nothing
Also, do use WorkSheet and close this:
Set oWS = oWB.WorkSheets(1)
' snip.
oWB.Save
Set oWs = Nothing
oWB.Close False
Set oWB = Nothing
I have used the below code to open an Excel file in MS Access 2003.
I would like to delete row 2 or A2:K2.
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open "QuotebyItem.xls", True, False
Dim wb as Excel.Workbook
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set wb = xlApp.Workbooks.Open("QuotebyItem.xls", True, False)
wb.Sheets(1).Rows(2).Delete
...assuming your file has only one sheet: if there might be multiple and you need to address only one of them:
wb.Sheets("Sheet2").Rows(2).Delete