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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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