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

nmk34

New Member
Joined
Apr 12, 2022
Messages
45
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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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