VBA: How to close Excel.Application after a crash - excel

My Word macro uses this code to open and access an Excel workbook:
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Set exWb = objExcel.Workbooks.Open("C:\Folder\Filename.xls")
' Main procedure
exWb.Close
Set exWb = Nothing
objExcel.Quit
Set objExcel = Nothing
Since I'm still debugging, the macro often stops before reaching the Close and Quit commands, leaving an instance of Excel and the workbook open in the background.
I can sometimes close them by opening Task Manager and ending the tasks, but I doubt that's the best solution. Is there a macro I can write in Word that closes all open Workbooks and quits all (background) instances of Excel?

Following is the traditional way to deal with such anomalies:
Sub MySub()
Dim objExcel As Excel.Application, exWb As Excel.Workbook
On Error GoTo Cleanup
Set objExcel = New Excel.Application
Set exWb = objExcel.Workbooks.Open("C:\Folder\Filename.xls")
' Main procedure
Cleanup:
If Err.number <> 0 Then MsgBox "Error " & Err.number & ": " & Err.Description
If Not exWb Is Nothing Then exWb.Close False
If Not objExcel Is Nothing Then objExcel.Quit
End Sub

Related

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)

Excel don't close in background

I am opening Excel from MsAccess and when I finished the task I close it, but Excel remain opened in backgroud until I close MsAccess
this is the code
Public Sub closeExcel()
Dim xlApp
Dim wkb As Object
Dim wks As Object
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If err <> 0 Then
Set xlApp = CreateObject("Excel.Application")
End If
With xlApp
Set wkb = .Workbooks.Add
Set wks = wkb.Worksheets(1)
wkb.Close True
Set wks = Nothing
Set wkb = Nothing
.Quit
End With
End Sub
I need to remain Access runing, but I want that Excel be closed.
How can I do this?
You are cutting off the worksheet. Try this:
With xlApp
Set wkb = .Workbooks.Add
Set wks = wkb.Worksheets(1)
' Do stuff.
Set wks = Nothing
wkb.Close True
Set wkb = Nothing
.Quit
End With
Set xlApp = Nothing

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

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

Why does workbook turns hidden after vba executes

Here is the sub
Sub test()
With ThisWorkbook
.Worksheets("Sheet1").Range("A1").Value = 5
.Save
End With
End Sub
Here is the vbscript that starts the sub
dim eApp
set eApp = GetObject("C:\Users\Owner\Desktop\Book1.xlsm")
eApp.Application.Run "Book1.xlsm!test"
set eApp = nothing
It executes fine but the workbook gets hidden for some reason . I have to unhide it View - -> Unhide
Why does this happen and how to fix it
A clearer way of running your vbs would be as follows
Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("excel.application")
Set ObjWB = ObjExcel.Workbooks.Open("C:\Users\Owner\Desktop\Book1.xlsm")
'make it visible
ObjExcel.Visible = True
ObjExcel.Run "Book1.xlsm!test"
ObjWB.Close False
ObjExcel.Quit
Set ObjExcel = Nothing

Resources