microsoft_excel:macros:email_activeworkbook_as_outlook_attachment
Differences
This shows you the differences between two versions of the page.
microsoft_excel:macros:email_activeworkbook_as_outlook_attachment [2021/08/04 14:21] – created peter | microsoft_excel:macros:email_activeworkbook_as_outlook_attachment [2021/08/04 15:24] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Microsoft Excel - Macros - Email ActiveWorkbook As Outlook Attachment ====== | ||
- | |||
- | <code bash> | ||
- | Sub EmailWorkbook() | ||
- | ' | ||
- | ' | ||
- | |||
- | Dim SourceWB As Workbook | ||
- | Dim DestinWB As Workbook | ||
- | Dim OutlookApp As Object | ||
- | Dim OutlookMessage As Object | ||
- | Dim TempFileName As Variant | ||
- | Dim ExternalLinks As Variant | ||
- | Dim TempFilePath As String | ||
- | Dim FileExtStr As String | ||
- | Dim DefaultName As String | ||
- | Dim UserAnswer As Long | ||
- | Dim x As Long | ||
- | |||
- | Set SourceWB = ActiveWorkbook | ||
- | |||
- | 'Check for macro code residing in | ||
- | If Val(Application.Version) >= 12 Then | ||
- | If SourceWB.FileFormat = 51 And SourceWB.HasVBProject = True Then | ||
- | UserAnswer = MsgBox(" | ||
- | "If you proceed the VBA code will not be included in your email attachment. " & _ | ||
- | "Do you wish to proceed?", | ||
- | | ||
- | If UserAnswer = vbNo Then Exit Sub ' | ||
- | | ||
- | End If | ||
- | End If | ||
- | |||
- | ' | ||
- | TempFilePath = Environ$(" | ||
- | |||
- | ' | ||
- | If SourceWB.Saved Then | ||
- | DefaultName = Left(SourceWB.Name, | ||
- | Else | ||
- | DefaultName = SourceWB.Name | ||
- | End If | ||
- | |||
- | 'Ask user for a file name | ||
- | TempFileName = Application.InputBox(" | ||
- | "File Name", Type:=2, Default: | ||
- | | ||
- | If TempFileName = False Then Exit Sub ' | ||
- | | ||
- | ' | ||
- | If SourceWB.Saved = True Then | ||
- | FileExtStr = " | ||
- | Else | ||
- | FileExtStr = " | ||
- | End If | ||
- | |||
- | ' | ||
- | Application.ScreenUpdating = False | ||
- | Application.EnableEvents = False | ||
- | Application.DisplayAlerts = False | ||
- | |||
- | 'Save Temporary Workbook | ||
- | SourceWB.SaveCopyAs TempFilePath & TempFileName & FileExtStr | ||
- | Set DestinWB = Workbooks.Open(TempFilePath & TempFileName & FileExtStr) | ||
- | |||
- | 'Break External Links | ||
- | ExternalLinks = DestinWB.LinkSources(Type: | ||
- | |||
- | 'Loop Through each External Link in ActiveWorkbook and Break it | ||
- | On Error Resume Next | ||
- | For x = 1 To UBound(ExternalLinks) | ||
- | DestinWB.BreakLink Name: | ||
- | Next x | ||
- | On Error GoTo 0 | ||
- | | ||
- | 'Save Changes | ||
- | DestinWB.Save | ||
- | |||
- | ' | ||
- | On Error Resume Next | ||
- | Set OutlookApp = GetObject(class: | ||
- | Err.Clear | ||
- | If OutlookApp Is Nothing Then Set OutlookApp = CreateObject(class: | ||
- | | ||
- | If Err.Number = 429 Then | ||
- | MsgBox " | ||
- | GoTo ExitSub | ||
- | End If | ||
- | On Error GoTo 0 | ||
- | |||
- | ' | ||
- | Set OutlookMessage = OutlookApp.CreateItem(0) | ||
- | |||
- | ' | ||
- | On Error Resume Next | ||
- | With OutlookMessage | ||
- | .To = "" | ||
- | .CC = "" | ||
- | .BCC = "" | ||
- | | ||
- | .Body = " | ||
- | | ||
- | | ||
- | End With | ||
- | On Error GoTo 0 | ||
- | |||
- | 'Close & Delete the temporary file | ||
- | DestinWB.Close SaveChanges: | ||
- | Kill TempFilePath & TempFileName & FileExtStr | ||
- | |||
- | 'Clear Memory | ||
- | Set OutlookMessage = Nothing | ||
- | Set OutlookApp = Nothing | ||
- | | ||
- | ' | ||
- | ExitSub: | ||
- | Application.ScreenUpdating = True | ||
- | Application.EnableEvents = True | ||
- | Application.DisplayAlerts = True | ||
- | |||
- | End Sub | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== References ===== | ||
- | |||
- | https:// | ||
- | |||
microsoft_excel/macros/email_activeworkbook_as_outlook_attachment.1628086866.txt.gz · Last modified: 2021/08/04 14:21 by peter