Excel VBA close a blank instance of an excel while maintain another with workbook opened

nmk34

New Member
Joined
Apr 12, 2022
Messages
40
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I got snips of code from googling to email a worksheet only from a workbook and i made it work by saving the worksheet into a new temp workbook. After i email the worksheet workbook, i close this newly made workbook and end up with a blank excel workbook that is active. then i activate the original workbook with macros. how to get rid of the blank instance of Excel only and keep the other one active?

Sub EmailOneSheet()
Dim oApp As Object
Dim oMail As Object
Dim wb As Workbook
Const fname = "C:\Temp2.xls"

'Turn off screen updating
Application.ScreenUpdating = False
'Make a copy of the active sheet and save it to a temporary file
ActiveSheet.Copy
Cells.Copy
Cells.PasteSpecial xlPasteValues

Set wb = ActiveWorkbook
Application.DisplayAlerts = False
wb.SaveAs "C:Temp2.xls"
Application.DisplayAlerts = True
'Create and show the outlook mail item
Set oMail = CreateObject("Outlook.Application").CreateItem(0)
With oMail 'Add a recipient
.To = "EMAIL ADDRESS"
.Subject = "Emailing active sheet of active workbook"
.Attachments.Add wb.FullName
.Send
End With

wb.Close True ' close the new workbook file that I need to email
'Excel.Parent.Quit ' THese two lines closes all instances of excel
'Application.Quit

Application.ScreenUpdating = True
Set oMail = Nothing
Workbooks("emailfromoutlook.xlsm").Activate ' activate the original opened workbook
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I have tested this code and I don't end up with an extra blank instance of Excel. Once it calls wb.close the instance of excel that was created with ActiveSheet.Copy is closed and I am not left with an empty instance of Excel.
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top