"Object Required" Error After Declaring Excel.Application object - excel

I am receiving an "object required" error while writing a VBScript to run a macro outside of Excel. The code is below this message. After declaring xlApp = CreateObject("Excel.Application"), any attempted action on the xlApp object will throw the "object required" error.
Am I missing something in my declaration statement, or is there some sort of import/include statement I need to make?
Sub temp()
Dim xlApp, xlBook
Dim oShell: Set oShell = CreateObject("WScript.Shell")
oShell.CurrentDirectory = "H:"
MsgBox (oShell.CurrentDirectory)
xlApp = CreateObject("Excel.Application")
xlApp.Visible = True 'error is thrown here
xlBook = xlApp.Workbooks.Open("H:\SW Tool Resources\test\tester.xlsm")
MsgBox ("Success")
End Sub

You need to Set your object variable to the new instance of Excel:
Set xlApp = CreateObject("Excel.Application")

Related

Creating a macro-enabled Excel file from within Word VBA

The following code, when used within Word VBA, successfully creates a standard Excel workbook in the chosen folder:
Const ExcelSourcePath = "C:\Users\Holge\_Universet i billeder\_ExcelDocs\"
Dim xlAppl As New Excel.Application
Dim xlBook As New Excel.Workbook
Private Sub TestCreateExcelFile()
Set xlAppl = CreateObject("Excel.Application")
Set xlBook = xlAppl.Workbooks.Add
Application.DisplayAlerts = False
xlBook.SaveAs FileName:=ExcelSourcePath & "TestFile.xlsx"
Application.DisplayAlerts = True
xlBook.Close False
Set xlBook = Nothing
xlAppl.Quit
Set xlAppl = Nothing
End Sub
However, what I really need is to create a macro-enabled file. But if I change ".xlsx" into ".xlsm" I get a RTE 1004, "Wrong file type name" (translated from Danish).
Perhaps CreateObject should be called with another argument, but which one? I have not been alble to find possible values for this argument.
In the following code:
Dim xlAppl As New Excel.Application
Dim xlBook As New Excel.Workbook
Private Sub TestCreateExcelFile()
Set xlAppl = CreateObject("Excel.Application")
Set xlBook = xlAppl.Workbooks.Add
A new Excel Application instance is created twice. You need to keep the one line of code for create a new instance, not doing that twice.
Moreover, an instance of the Workbook class can't be created by using the New operator like shown in your code:
Dim xlBook As New Excel.Workbook
Most probably the intention was to declare objects out of the sub and then instantiate them:
Const ExcelSourcePath = "C:\Users\Holge\_Universet i billeder\_ExcelDocs\"
Dim xlAppl As Excel.Application
Dim xlBook As Excel.Workbook
Private Sub TestCreateExcelFile()
Set xlAppl = CreateObject("Excel.Application")
Set xlBook = xlAppl.Workbooks.Add
Application.DisplayAlerts = False
xlBook.SaveAs FileName:=ExcelSourcePath & "TestFile.xlsx"
Application.DisplayAlerts = True
xlBook.Close False
Set xlBook = Nothing
xlAppl.Quit
Set xlAppl = Nothing
End Sub
Just need to remove New operators from the code with declarations.

Can't create Excel spreadsheet from Word

I'm trying to write a VBA macro from within Word to create an Excel spreadsheet and export data to it. I'm having trouble just creating the new spreadsheet. For the following macro, I get "Object doesn't support this property or method" on the line
Set myWb = myExcel.Workbooks.Add
in the code
Private Sub CreateExcel2()
Dim myExcel As Object
Dim myWb As Object
Set myExcel = CreateObject("Excel.Application")
Set myWb = myExcel.Workbooks.Add
Application.DisplayAlerts = False
myWb.SaveAs FileName:="D:\test\dump.xls"
Application.DisplayAlerts = True
myWb.Close False
Set myWb = Nothing
myExcel.Quit
Set myExcel = Nothing
End Sub
You can try:
On Error Resume Next
Set oExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Set oExcel = CreateObject("Excel.Application")
End If
On Error GoTo 0
oExcel.visible = true 'to check of excel opens
'try Set myWb = myExcel.Workbooks.Add()
'try Set myWb = oExcel.Workbooks.Open(sPathFile)

How to run an Excel macro after a new mail is received in Outlook?

I want to run an Excel macro when email with "subject" is sent to my inbox. I set up run a script in Manage Rules & Alerts in Outlook. When I receive a mail with "subject" nothing happens to the macro.
Sub Test(mail As MailItem)
Dim ExApp As Excel.Application
On Error Resume Next
Set ExApp = GetObject(, "Excel.Application")
If Not ExApp Is Nothing Then
ExApp.Run "'C:\Users\Desktop\Production v2.7.1.xlsm'!Main_function_Auto"
End If
End Sub
When your calling Excel sub procedure from Outlook, make sure to include the module name -
Example
Option Explicit
Public Sub Example(Item As Outlook.MailItem)
Dim xlApp As Excel.Application
Dim xlBook As Workbook
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open(Environ( _
"USERPROFILE") & "\Desktop\Production.xlsm")
xlApp.Visible = True
' // Run Macro in file
xlBook.Application.Run "Module1.Main_function_Auto"
Set xlApp = Nothing
Set xlBook = Nothing
End Sub

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

Run time error 462 Access VBA using Excel

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

Resources