Excel will not fully close after running Word VBA macro - excel

I'm currently trying to make a macro that opens a user defined excel spreadsheet, extracts some data for use in the word document and then closes it. My problem is that when I run the macro, the spreadsheet that I opened is still technically open as a background process in my task manager. I read on another stack overflow question that the reason is because visual basic will not release the reference object from excel until I close out of Microsoft Word. However, even after closing out of Word, the excel background process is still going and I can only stop it by ending the task in the task manager. To clarify, if I run the macro, close Word and then try to open the excel file, I can get in without telling me it's a read only file. However, if I don't close out of Word and I try to go into the spreadsheet after running the macro, then it tells me that it's a read only file. Below is the code I'm using that is causing this problem for me. Thanks to anyone who can help.
Sub UpdateProposal()
'Declares variables
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim SpreadsheetPath As String
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
Dim ExcelWasNotRunning As Boolean
Dim ProposalInfoArr(1 To 30) As String
'Skips to ErrorHandler if user cancels out of file dialog
On Error GoTo ErrorHandler
'Display a Dialog Box that allows to select a single file.
'The path for the file picked will be stored in SpreadsheetPath variable
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Stores in SpreadsheetPath variable
SpreadsheetPath = .SelectedItems.Item(1)
End With
'If Excel is running, get a handle on it; otherwise start a new instance of Excel
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err Then
ExcelWasNotRunning = True
Set xlApp = New Excel.Application
End If
'If you want Excel to be visible, you could add the line: xlApp.Visible = True here; but your code will run faster if you don't make it visible
'Open the workbook
Set xlBook = xlApp.Workbooks.Open(FileName:=SpreadsheetPath)
'''Extracts Data
'Quits out of Excel if it was not running previous to running the macro.
If ExcelWasNotRunning Then
xlApp.DisplayAlerts = False
xlApp.Quit
End If
'Make sure you release object references.
Set xlRange = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
'Ends the macro before the error handler
Exit Sub
'Ends Macro
ErrorHandler:
MsgBox "The following error occurred: " & Err.Description
End Sub

You are defining the objects correctly:
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
but you forgot about the implicitly used Workbooks object... as most of the answers you will find do... which means it doesn't get released. So do it like this:
Dim SpreadsheetPath As String
Dim xlApp As Excel.Application
Dim xlBooks As Excel.Workbooks
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
SpreadsheetPath = "C:\MyPath\MyFile.xlsx"
Set xlApp = New Excel.Application
' Set xlApp = GetObject(, "Excel.Application") ' or attach to an existing one
Set xlBooks = xlApp.Workbooks
Set xlBook = xlBooks.Open(FileName:=SpreadsheetPath) ' you can use .Add instead if someone else may have it open already
Set xlSheet = xlBook.Worksheets(1)
Set xlRange = xlSheet.Range("A1")
' do stuff with the worksheet/range
xlRange.Value = "foo"
' the order matters
' just like it does
' when you create the objects
Set xlRange = Nothing
Set xlSheet = Nothing
xlBook.Close False
Set xlBook = Nothing
Set xlBooks = Nothing
xlApp.Quit
Set xlApp = Nothing
However, you may find that it still isn't getting released when you want, but it will get released when you close the program you are using to create it (in your case, MS-Word) as that is (presumably) when Windows does its built-in garbage collection.
Note: I removed the error handling just to keep it a clean example, but you can leave that in

Related

Display Alerts in Excel from Access

I am opening an excel workbook from excel and delete a sheet inside. I want to do this without the message from Excel:
"Excel will permanently delete this sheet, do you want to continue"
However I cannot make the "DisplayAlerts = False" work correctly.
Public Sub xportQuery()
Dim appExcel As Excel.Application
Dim myWorkbook As Excel.Workbook
Dim PathDaily, FileName As String
PathDaily = Forms!Menu!Text69
FileName = Forms!Menu!Text84
Set appExcel = CreateObject("Excel.Application")
Set myWorkbook = appExcel.Workbooks.Open(PathDaily & FileName)
appExcel.Visible = True
'Set appExcel = Nothing
Set myWorkbook = Nothing
appExcel.DisplayAlerts = False
Workbooks(FileName).Sheets("Sheety").Delete
appExcel.DisplayAlerts = True
End Sub
Writing it this way I get a "Subscript out of range" on the Sheets("Sheety").delete
If I take of the two DisplayAlerts lines, the code works but with the alert.
How do I write my code correctly to work without any alert message?
Note: DoCmd.SetWarnings didn t work either as the message is displayed in Excel
Thanks To Sam's Comment:
it works either by changing
Workbooks(FileName).Sheets("Sheety").Delete
into
myWorkbook.Sheets("Sheety").Delete
or
AppExcel.Workbooks(FileName).Sheets("Sheety").Delete
However the rest of the macro can still use Workbooks(Filename) without the "AppExcel." Reference

How to open Excel if not already open

I'm trying to find a way to open Excel using Outlook VBA, but only if it's not already open. I managed to find some code on the internet that opens Excel, does changes and then closes it, but it doesn't behave nicely if the Excel workbook is already open(it does apply the changes, but it no longer closes the Excel workbook, and it simply leaves it with a grey interior; also, sometimes it doesn't even show in the explorer anymore and I have to close it from the task manager).
I would also greatly appreciate if someone could explain what most of the code does.
Public xlApp As Object
Public xlWB As Object
Public xlSheet As Object
Sub ExportToExcel()
Dim enviro As String
Dim strPath As String
'Get Excel set up
enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
strPath = enviro & "\Documents\test2.xlsx"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Sheet1")
' Process the message record
On Error Resume Next
xlWB.Close 1
If bXStarted Then
xlApp.Quit
End If
End Sub
I know what the xlWb and xlSheet objects do, and how they're declared, and I also understand what the environ function and strPath string do, but I don't undestand why we need the bXStarted boolean, what Set xlApp = GetObject does, why the Application.StatusBar message doesn't get displayed, the difference between GetObject and CreateObjectand why so many error tests are needed.
Thanks in advance.
The difference between get object and create object is in the title, one will get open excel.application, if there is an error err<>0 then it creates an excel.application. I think you'll be getting a saveas message, as the file may not be saving, but open, and you're instructing it to save, the error resume next is skipping it . Try saving before just a .close If you remove the on error resume next the error wont be skipped and will be shown.
Sub explaination()
Dim blnDidICreateExcel As Boolean ' Please read MSDN on boolean
Dim objToHoldExcelCreatedOrNot As Object ' Please read MSDN on objects create/get
' Does the user have Excel open, if i try to get it, then there will be an error logically if not
Set objToHoldExcelCreatedOrNot = GetObject(, "Excel.Application")
' Was there an error
If Err <> 0 Then
' There was, so i need to create one
Set objToHoldExcelCreatedOrNot = CreateObject("Excel.Application")
blnDidICreateExcel = True ' Yes, i created it
End If
' Do the neccessary
' CLose the workbook
' Did i create this Excel, if so tidy up
If blnDidICreateExcel Then objToHoldExcelCreatedOrNot.Quit
End Sub

How to find other Excel instances and assign them to an Excel.Application variable

I'm trying to accomplish being able to find other Excel instances and control them using VBA.
Studying other threads this is what I've come up with so far, but for some reason it doesn't work.
What I'm trying to do is the following:
Dim xl as Excel.Application
Dim objList As Object
Dim objProcess As Object
Set objList = GetObject("winmgmts:")._
ExecQuery("select * from win32_process where name='Excel.exe'")
If objList.Count > 1 Then
For Each objProcess In objList '
If objProcess <> Application Then '
Set xl = objProcess ' this is what doesn't work
Exit For '
End If '
Next '
Else
Set xl = New Excel.Application
End If
'Do stuff with xl
Can anyone please tell me where I'm going wrong or if this is this even possible?
Have a look at this code I quickly put together, this worked for me when I had 5 instances of excel open, each with it's own workbook open. The code pops open a message box for all of the open workbooks in all of the instances of excel and displays the workbook name. You should be able to amend this to assign that workbook or instance of Excel to a variable instead.
Sub Testing()
Dim xlApp As Excel.Application
Set xlApp = GetObject(, "Excel.Application")
Dim xlWB As Excel.Workbook
For Each xlWB In xlApp.Workbooks
MsgBox xlWB.Name
Next xlWB
Set xlApp = Nothing
Set xlWB = Nothing
End Sub

Reference and access Excel from Outlook

I'm trying to update an Excel file from Outlook (Office 2010). How do I reference and access Excel?
As a simple test I'm trying to count the number of open workbooks. When I run this I get 0, even though there are 2 open.
Sub Test()
Dim xlApp As Excel.Application
Dim xlWBook As Excel.Workbook
Set xlApp = New Excel.Application
Debug.Print "xlApp.Workbooks.Count = " & xlApp.Workbooks.Count
On Error Resume Next
Set xlWBook = xlApp.Workbooks("Data.xlsx")
Err.Clear 'Clear error and open File Index
If xlWBook Is Nothing Then
Set xlWBook = xlApp.Workbooks.Open("C:\Users\Chris\Desktop\Data.xlsx")
End If
End Sub
This is what I use to detect Excel:
Dim xlApp As excel.Application
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number = 429 Then 'Excel not running
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo 0
once xlApp is set, you can use xlApp.Workbooks.Count to count the worksheets
Note: This code will get the first one opened, if there are more than one instance of Excel to find
If you have to find a specific workbook, from this page, Set xlApp = GetObject("Book2").Application will find the workbook, even if it's not in the first instance. If the workbook is from a file, or already saved, replace book2 with the full path and filename - Side effect - this will also open the file if it is not already open
More usage info: http://msdn.microsoft.com/en-us/library/aa164798%28v=office.10%29.aspx

how to print excel file via vb6?

I have an excel file that was created by vb6 application and after I save it, I want it to be printed into the default printer..,
Tnx, any help would be appreciated.
Private Sub Command1_Click()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSH As Excel.Worksheet
'open excel application
Set xlApp = New Excel.Application
'Open excel workbook
Set xlWB = xlApp.Workbooks.Open(FileName:="C:\YourFile.xls")
'There are two ways to access specific worksheets
'By index number (the first worksheet in this case)
Set xlSH = xlWB.Worksheets(1)
'or by the Sheet's Name
Set xlSH = xlWB.Worksheets("TestSheet")
PrintSheet xlSH, "MyFoot", "MyHead"
'Close workbook (optional)
xlWB.Close
'Quit excel (automatically closes all workbooks)
xlApp.Quit
'Clean up memory (you must do this)
Set xlWB = Nothing
Set xlApp = Nothing
End Sub
Sub PrintSheet(sh As Worksheet, strFooter As String, strHeader As String)
sh.PageSetup.CenterFooter = strFooter
sh.PageSetup.CenterHeader = strHeader
sh.PrintOut
End Sub
Yet, to answer your question, you can use :
ActiveWorkbook.PrintOut Copies:=1, Collate:=True
and you can find much information here : http://www.exceltip.com/excel_tips/Printing_in_VBA/210.html
Anyway, i insist, you should accept answers from your previous questions or people won't care answering your new ones.
Max

Resources