VBA - Excel sometimes fails to close properly - vba

I have a macro in outlook, which helps runs statistics in an excel workbook.
However sometimes it fails to close it properly, and ends up ruining the process, since the workbook is open still, when i run it next time.
This is my method for closing it.
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open(strpath)
...
xlWB.Save
xlWB.Close savechanges:=True
xlApp.Quit
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
From my understanding it should do it.

Did you turn the displayalerts off? Use:
xlApp.DisplayAlerts = False
after you instanciate the Excel application. That prevents Excel from asking for user input ("are you really sure you want to ...?). Such popup could prevent Excel from closing.
Happened to me more than once on an invisable application.

Related

Window not visible despite running in background

I have a macro that I'm trying to run through Access that will open up an excel sheet, do some actions on it, and then leave the sheet open.
I have most of it working, with the exception of not being able to get my excel document to open visibly. If I check the task manager, an excel process is running in the background so something does happen, just nothing that I can physically see.
I've attempted to sample some code found through stackoverflow and other resources, which I'm sure you'll see some of that in my current code. But I tried for about an hour with no avail.
Private Sub Command1_Click()
Dim fd As FileDialog
Dim MySheetPath As String
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
On Error GoTo ErrorHandler
'allowing selection of the time
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
If fd.Show = True Then
If fd.SelectedItems(1) <> vbNullString Then
MySheetPath = fd.SelectedItems(1)
End If
Else
End
End If
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
ShowaWindow (MySheetPath)
Set XlSheet = XlBook.Worksheets(1)
XlSheet.Rows(2).EntireRow.Insert
XlSheet.Range("D2") = "ABC"
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing
Exit Sub
ErrorHandler:
Set fd = Nothing
MsgBox "Error " & Err & ": " & Error(Err)
End Sub
Sub ShowaWindow(sFileName As String)
Dim oWb As Workbook
Set oWb = GetObject(sFileName)
For Each oWb In Workbooks
If LCase(oWb.Name) <> LCase(sFileName) Then
oWb.Windows(1).Visible = True
Exit For
End If
Next
End Sub
Ideally I would like to be able to see the worksheet appear.
Set Xl = CreateObject("Excel.Application")
Xl.Visible=True
You don't need to put it immediately after creating the object, just before you set the object to Nothing.
Set XlBook = GetObject(MySheetPath)
This is wrong. Don't use GetObject to open the workbook, use the Excel.Application instance you just created:
Set XlBook = Xl.Workbooks.Open(MySheetPath)
Later you iterate all opened workbooks:
For Each oWb In Workbooks
But that's not the Workbooks collection from the Xl application instance, it's the Workbooks collection from the instance that's currently running your code - you need to qualify it with the Xl object:
Private Sub ShowaWindow(ByVal app As Excel.Application, ByVal sFileName As String)
'...
For Each oWb In app.Workbooks
Also, make the app instance visible after you created it, and don't forget to invoke XlBook.Close and Xl.Quit to properly tear down that EXCEL.EXE process when you're done.

Need to get the last worksheet name in msgbox

I need to get the last (rightmost worksheet) name in msgbox.
I used Sheets(Sheets.Count) to get last sheet. But its only giving first sheets name. Kindly help me on this.
Here is my code.
Sub ShowMRNumber()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open("location")
Set xlSheet = xlApp.Sheets(Sheets.Count)
MsgBox "MR No. is" & vbNewLine xlSheet.Name
xlApp.Workbooks.Close
End Sub
Your line saying
Set xlSheet = xlApp.Sheets(Sheets.Count)
should actually be
Set xlSheet = xlBook.Sheets(xlBook.Sheets.Count)
Using xlApp.Sheets is probably not an issue, as that would probably have defaulted to the active workbook within the xlApp instance of Excel, but Sheets.Count (without a xlApp or xlBook qualifier) would not have been referring to a workbook open in a different instance of Excel - it would have been referring to the active workbook in the instance of Excel where the code was running.

Open an Excel file saved with password for modify

I would like to open an excel file that is saved with a password for modify with VBScript. My current code VBS code is below, which works, but it keeps popping up with boax asking for a password. How can i open the excel spreadsheet with excel prompting me for a password?
Option Explicit
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Users\jasons\Documents\TestFile.xlsm",,,,"yep123")
xlApp.Visible = True
xlApp.Run "Refresh_data_ss"
xlApp.Save
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Now I see, there is a password for modify on your file but not for open. Open password is the fifth parameter to Workbooks.Open, and modify is the sixth. SO you need to have the following instead (one more comma):
Set xlBook = xlApp.Workbooks.Open("C:\Users\jasons\Documents\TestFile.xlsm",,,,,"yep123")
You can get the password from an InputBox
myPass = InputBox("Write the password: ")
Set xlBook = xlApp.Workbooks.Open("C:\Users\jasons\Documents\TestFile.xlsm",,,, myPass)

Opening Excel and Running Macro From Outlook Leaves Excel Stuck in Task Manager

I have a macro in Outlook that calls an Excel file and runs a macro in that Excel file then closes the file. The problem is After closing Excel it stays in the Task Manager. I have tested this a million times and I even have removed all code in my Excel macro to see if that was the problem but Excel is still is still getting stuck in the task manager. My Outlook code is:
Dim xlApp As Object
Dim xlWB As Workbook
Dim strFile As String
Set xlApp = CreateObject("excel.application")
xlApp.Visible = True
xlApp.DisplayAlerts = False
strFile = "c:\desktop\a.xlsm"
Set xlWB = Workbooks.Open(strFile)
xlApp.Run ("Cleanup")
xlWB.Close False
If Not xlWB Is Nothing Then
Set xlWB = Nothing
End If
xlApp.Quit
If Not xlApp Is Nothing Then
Set xlApp = Nothing
End If
The problem with the code was that I was not opening the workbook with the Excel application that I created. I fixed the problem by adding xlApp here:
Set xlWB = xlApp.Workbooks.Open(strFile)
That could have not been a more simple fix to such a time consuming problem :(

Difficult closing the .exe process in excel

I'm building a MS-Excel extract from a VB6 application. I'm having a very difficult time getting rid of the .exe process for excel. I generate an extract and save it to C: drive. I don't open it or anything, yet it's still visible in the Task Manager under Processes. Here is how I declare and close all excel components.
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
This I do all the way at the end of the procedure
Xlbook.close
Set xlSheetWeek = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Why is the process still working even though I do all the xlbook.close and Don't have excel open. It shouldn't be showing.
You need to call the Quit() method on your Excel.Application object (xlApp), https://msdn.microsoft.com/en-us/library/office/ff839269.aspx:
xlApp.Quit()
You can get crazy and
sKill = "TASKKILL /F /IM excel.exe"
Shell sKill, vbHide