Description:
|
On opening the workbook two temporary control buttons are placed on the RHS of the worksheet menu bar. On clicking the 'Email Active Sheet' button a temporary toolbar with 5 options appears. On selecting one of the 'Send' options the active sheet is emailed in the body of the email to the specified recipient (or recipients) using the users default mailer (Outlook, Outlook Express, ..., etc). NOTE: Requires Office 2000 or greater
|
Discussion:
|
A common question in Help forums is "how to automate emailing the active sheet in the body of an email". The usual solution given (See http://www.rondebruin.nl/mail/folder3/mail2.htm) utilizes Outlook, and the solution given there involves a process of deleting all shapes and converting values to text, needless to say, charts can't be sent in this manner. So if the sheet to send relies heavily on formatting, charts, images, and shapes for presentation purposes, all of that is lost... In addition, if you don't use Outlook as your default emailer and you're using that code you may think that you have emailed it - but you haven't - it's actually just sitting in the Outlook outbox waiting for you to open Outlook so it can be sent.... Excel provides an inbuilt tool that circumvents all these difficulties, but this requires you to click a few buttons on your toolbar to activate the process, which is often not considered adequate when the process is to be fully automated and activated by code. This entry uses code that "clicks the buttons" for the user and sends the email. The only variation from 'normal' usage here is that the recipients(s) can't be written into the code itself, however, we can make use of the fact that once it's been saved, the workbook remembers the last recipients that the workbook was sent to and automatically sends it to the same recipients every time the code is actuated. What this means is that there is actually no need to write the recipients name into the code, instead, the recipients are written in the "To:" field of the mailer the very first time it is sent. When the code is actuated after that, it gives the options to i) use the same recipients, ii) ALWAYS use the same recipients without asking again, or, iii) to change them in the To: field whenever the code is actuated. It's suggested that this code be placed in Personal.xls and used whenever you wish to send the active sheet in any active workbook. SPECIAL NOTE: Any hyperlinks used for navigating about the active sheet will still work on the emailed sheet
|
Option Explicit
Private Sub Workbook_Open()
Dim NewButton As CommandBarButton
On Error Resume Next
Run ("DeleteToolbar")
Call DeleteAddedButtons
Set NewButton = Application.CommandBars("Worksheet Menu Bar") _
.Controls.Add(Type:=msoControlButton)
With NewButton
.Caption = "&Email Active Sheet"
.OnAction = "SendActiveSheet"
.TooltipText = "Email Active Sheet in Body"
.Style = msoButtonIconAndCaption
.FaceId = 24
End With
Set NewButton = Nothing
Set NewButton = Application.CommandBars("Worksheet Menu Bar") _
.Controls.Add(Type:=msoControlButton)
With NewButton
.Caption = "Cancel &Email"
.OnAction = "EscapeProcedure"
.TooltipText = "Return to normal window"
.Style = msoButtonIconAndCaption
.FaceId = 51
.Enabled = False
End With
Set NewButton = Nothing
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Run ("DeleteToolbar")
Call DeleteAddedButtons
End Sub
Private Sub DeleteAddedButtons()
On Error Resume Next
With Application.CommandBars("Worksheet Menu Bar")
.Controls("Email Active Sheet").Delete
.Controls("Cancel &Email").Delete
End With
End Sub
Option Explicit
Sub SendActiveSheet()
With ActiveWorkbook
Select Case .BuiltinDocumentProperties("Comments")
Case "NeverShow"
Call AutoSend
Exit Sub
Case "Initialized"
If Not .EnvelopeVisible Then GoSub BuildToolbar
Case Else
If Not .EnvelopeVisible Then
.BuiltinDocumentProperties("Comments") = "FirstTime"
GoSub BuildToolbar
End If
End Select
Exit Sub
End With
BuildToolbar:
Dim FloatingBar As CommandBar
Dim NewButton As CommandBarButton
On Error Resume Next
Call DeleteToolbar
On Error GoTo 0
Set FloatingBar = CommandBars.Add
With FloatingBar
.Name = "Email Activesheet in Body of Email"
.Position = msoBarFloating
.Visible = True
.Top = 200
.Left = 200
End With
Set NewButton = FloatingBar.Controls.Add(Type:=msoControlButton)
With NewButton
.Caption = "CANCEL"
.OnAction = "ExitSub"
.TooltipText = "Click to cancel operation"
.Style = msoButtonIconAndWrapCaptionBelow
.FaceId = 463
End With
With ActiveWorkbook
If .BuiltinDocumentProperties("Comments") = "Initialized" Then
Set NewButton = FloatingBar.Controls.Add(Type:=msoControlButton)
With NewButton
.Caption = "Send the active sheet to the same recipients as last time..."
.TooltipText = "Use the same email addresses"
.OnAction = "AutoSend"
.Style = msoButtonIconAndWrapCaptionBelow
.FaceId = 45
End With
End If
Set NewButton = FloatingBar.Controls.Add(Type:=msoControlButton)
With NewButton
.Caption = "Send the active sheet to some new recipients"
.TooltipText = "Don't use the same email addresses"
.OnAction = "ManualSend"
.Style = msoButtonIconAndWrapCaptionBelow
.FaceId = 24
End With
If .BuiltinDocumentProperties("Comments") = "Initialized" Then
Set NewButton = FloatingBar.Controls.Add(Type:=msoControlButton)
With NewButton
.Caption = "I'm not sure who the active sheet was sent to last time. Show me.."
.TooltipText = "Show me the last recipients"
.OnAction = "ManualSend"
.Style = msoButtonIconAndWrapCaptionBelow
.FaceId = 487
End With
End If
Set NewButton = FloatingBar.Controls.Add(Type:=msoControlButton)
With NewButton
.Caption = "ALWAYS send the active sheet to these recipients and NEVER show me this toolbar again!"
.TooltipText = "Automate this operation"
.OnAction = "NeverShowAgain"
.Style = msoButtonIconAndWrapCaptionBelow
.FaceId = 536
End With
End With
Set FloatingBar = Nothing
Set NewButton = Nothing
End Sub
Private Sub AutoSend()
Call DeleteToolbar
DoEvents
With Application
.ScreenUpdating = False
On Error Resume Next
With .CommandBars("Send To")
.Controls.Add Type:=msoControlButton, ID:=3708
.Controls("Mail Recipient").Execute
DoEvents
With .Controls("Send Now")
.Execute
.Delete
End With
End With
.ScreenUpdating = True
End With
End Sub
Private Sub ManualSend()
Call DeleteToolbar
DoEvents
With ActiveWorkbook
If .BuiltinDocumentProperties("Comments") = "FirstTime" Then
.BuiltinDocumentProperties("Comments") = "Initialized"
Application.DisplayAlerts = False
.Save
Application.DisplayAlerts = True
.EnvelopeVisible = True
Do Until .EnvelopeVisible
Loop
MsgBox "Enter recipients in the 'To:' field then click" & vbNewLine & _
"the ''Send this Sheet'' button on the left..." & vbNewLine & _
"" & vbNewLine & _
"(Note: For this time only, to cancel email go" & vbNewLine & _
"to 'File' - 'Send To' and click 'Mail Recipient')", , "INSTRUCTIONS..."
Else
.EnvelopeVisible = True
Do Until .EnvelopeVisible
Loop
Application.CommandBars("Worksheet Menu Bar").Controls("Cancel &Email").Enabled = True
End If
End With
End Sub
Private Sub NeverShowAgain()
Call DeleteToolbar
With ActiveWorkbook
Select Case .BuiltinDocumentProperties("Comments")
Case "FirstTime"
.BuiltinDocumentProperties("Comments") = "NeverShow"
Application.DisplayAlerts = False
.Save
Application.DisplayAlerts = True
Call ManualSend
Case "Initialized"
.BuiltinDocumentProperties("Comments") = "NeverShow"
Application.DisplayAlerts = False
.Save
Application.DisplayAlerts = True
Call AutoSend
End Select
End With
End Sub
Private Sub ExitSub()
Call DeleteToolbar
End Sub
Private Sub DeleteToolbar()
On Error Resume Next
Application.CommandBars("Email Activesheet in body of email").Delete
End Sub
Private Sub EscapeProcedure()
ActiveWorkbook.EnvelopeVisible = False
Application.CommandBars("Worksheet Menu Bar").Controls("Cancel &Email").Enabled = False
End Sub
Option Explicit
Sub SendSheetProgrammatically_Example()
With Application
.ScreenUpdating = False
On Error Resume Next
Select Case ActiveWorkbook.BuiltinDocumentProperties("Comments")
Case Is <> "SentBefore"
With ActiveWorkbook
.EnvelopeVisible = True
.BuiltinDocumentProperties("Comments") = "SentBefore"
End With
Case "SentBefore"
ActiveWorkbook.EnvelopeVisible = True
With .CommandBars("Send To")
.Controls.Add Type:=msoControlButton, ID:=3708
With .Controls("Send Now")
.Execute
.Delete
End With
End With
End Select
.ScreenUpdating = True
End With
End Sub
|
Test the code:
|
- Download the attachment, extract and open the MailSheet workbook
- Select any sheet and click 'Email Active Sheet'.
- A toolbar with 3 buttons will appear, click the central button.
- The very first time this code is run a small dialog box will then appear, Click OK or Cancel
- Type the recipient(s) into the 'To' field and click the 'Send This Sheet' button.
- Select another sheet, a toolbar with 5 buttons will appear, click the button of your choice.
- (To add functionality to Excel, the code above can be pasted into Personal.xls and the "Email Active Sheet" button and all the email options are then available every time a workbook is opened).
- The attachment also includes two other workbooks, one gives one method for inserting a message into the sheet to be sent.
|