From within Word, Excel and PowerPoint, you can save your document in the pdf-format. Additionally it allows you to directly share and send out the open document as a pdf-file attachment via Outlook.
On occasion it can be convenient to send out the pdf-version together with the original Office document. For instance;
- The document is ready for the final review before publishing or distributing but the final reviewer should also be able to make some last minute changes.
- Provide the option to people to read the document on their smartphone which do no have the Office app installed on their Android or iOS device.
- Accessibility reasons.
This guide explains how you can create that functionality with a macro in Word. It also includes code examples for using this macro in PowerPoint and Excel.
MailPdfPlus VBA macro
The MailPdfPlus macro allows you to send the document that you are currently working on in Word, Excel or PowerPoint in its original format (docx, xlsx or pptx) as well as a pdf-version thereof.
This would normally only be possible with additional manual actions of generating the pdf-file and attaching it after initiating the Share command.
When executing the macro, you’ll get prompted to save the current document first if you haven’t done that before. Otherwise it will do a background save. After this, it will open a new mail message with the current document attached and a pdf-version of it.
Note:
Unlike Word, macros in PowerPoint can only be saved within the presentation itself. This means that you’ll need to save the presentation as a “PowerPoint Macro-Enabled presentation (*.pptm)”.
Quick Install
Use the following instructions to configure the macro in Word;
- Download this code-file (
mailpdfplus.zip
) or copy the code below. - Open Word.
- Open the VBA Editor (keyboard shortcut ALT+F11).
- Extract the zip-file and import the
MailPdfPlus-Word.bas
file via File-> Import…
If you copied the code, paste it into a new module. - Add a reference to: Microsoft Outlook <version> Object Library.
Tools-> References…- Note: The word “<version>” stands for your version number of Outlook.
- Outlook 2010 = 14.0
- Outlook 2013 = 15.0
- Outlook 2016, 2019 and Microsoft 365 = 16.0
- Note: The word “<version>” stands for your version number of Outlook.
- Sign your code so you won’t get any security prompts and the macro won’t get disabled.
- Add a button for easy access to the macro.
- Repeat the process in Excel and PowerPoint using
MailPdfPlus-Excel.bas
andMailPdfPlus-PowerPoint.bas
respectively.
Add a button of the macro to the QAT for quick access to it.
Macro Code
The following code is contained in the zip-file referenced in the Quick Install. You can use the code below for review or manual installation.
Word
Sub MailPdfPlusOriginal() '================================================================= 'Description: Word macro to create a pdf-file of the current ' document and add both the original and the ' pdf-version of the document as an attachment to ' a new Outlook message. ' 'Important! This macro requires a reference added to the ' Microsoft Outlook <version> Object Library ' In VBA Editor: Tools-> References... ' 'author : Robert Sparnaaij 'version: 1.0 'website: https://www.howto-outlook.com/howto/mailpdfplus.htm '================================================================= 'Verify if the document has been saved before so that we have a path to work with. 'If not, notify the user that there will be a safe dialog first. If ActiveDocument.Path <> "" Then ActiveDocument.Save Else Dim Msg, Style, Title, Response Msg = "This document has not been saved before." & vbLf & _ "Please save the document to disk first." & vbLf & _ "Without saving first, only the pdf-file will be attached." Style = vbInformation + vbOKOnly Title = "Save current presentation" Response = MsgBox(Msg, Style, Title) Dim dlgSaveAs As FileDialog Dim strCurrentFile As String Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs) If dlgSaveAs.Show = -1 Then strCurrentFile = dlgSaveAs.SelectedItems(1) ActiveDocument.SaveAs (strCurrentFile) End If Set dlgSaveAs = Nothing End If 'Get the name of the open file and strip any extension. Dim MyFile As String MyFile = ActiveDocument.Name intPos = InStrRev(MyFile, ".") If intPos > 0 Then MyFile = Left(MyFile, intPos - 1) End If 'Get the user's TempFolder to store the created pdf item in. Dim FSO As Object, TmpFolder As Object Set FSO = CreateObject("scripting.filesystemobject") Set FileName = FSO.GetSpecialFolder(2) 'Create the full path name for the pdf-file FileName = FileName & "\" & MyFile & ".pdf" 'Save the current document as pdf in the user's temp folder. 'Note that we are going to include the document properties as well. 'If you do not want this set "IncludeDocProps" to False. ActiveDocument.ExportAsFixedFormat OutputFileName:= _ FileName, ExportFormat:= _ wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _ wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=0, To:=0, _ Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _ CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _ BitmapMissingFonts:=True, UseISO19005_1:=False 'Declare an Outlook application an a mail item. Dim oOutlookApp As Outlook.Application Dim oItem As Outlook.MailItem 'Start Outlook if it isn't running. Set oOutlookApp = GetObject(, "Outlook.Application") If Err <> 0 Then Set oOutlookApp = CreateObject("Outlook.Application") End If 'Create a new message. Set oItem = oOutlookApp.CreateItem(olMailItem) 'Add the attachments. oItem.Attachments.Add FileName oItem.Attachments.Add ActiveDocument.FullName 'Show the message. oItem.Display 'Cleanup Set FSO = Nothing Set FileName = Nothing Set oOutlookApp = Nothing Set oItem = Nothing End Sub
Excel
Sub MailPdfPlusOriginal() '================================================================= 'Description: Excel macro to create a pdf-file of the current ' workbook and add both the original and the ' pdf-version of the workbook as an attachment to ' a new Outlook message. ' 'Important! This macro requires a reference added to the ' Microsoft Outlook <version> Object Library ' In VBA Editor: Tools-> References... ' 'author : Robert Sparnaaij 'version: 1.0 'website: https://www.howto-outlook.com/howto/mailpdfplus.htm '================================================================= 'Verify if the workbook has been saved before so that we have a path to work with. 'If not, notify the user that there will be a safe dialog first. If ActiveWorkbook.Path <> "" Then ActiveWorkbook.Save Else Dim Msg, Style, Title, Response Msg = "This workbook has not been saved before." & vbLf & _ "Please save the workbook to disk first." & vbLf & _ "Without saving first, only the pdf-file will be attached." Style = vbInformation + vbOKOnly Title = "Save current presentation" Response = MsgBox(Msg, Style, Title) Dim dlgSaveAs As FileDialog Dim strCurrentFile As String Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs) If dlgSaveAs.Show = -1 Then strCurrentFile = dlgSaveAs.SelectedItems(1) ActiveWorkbook.SaveAs (strCurrentFile) End If Set dlgSaveAs = Nothing End If 'Get the name of the open file and strip any extension. Dim MyFile As String MyFile = ActiveWorkbook.Name intPos = InStrRev(MyFile, ".") If intPos > 0 Then MyFile = Left(MyFile, intPos - 1) End If 'Get the user's TempFolder to store the created pdf item in. Dim FSO As Object, TmpFolder As Object Set FSO = CreateObject("scripting.filesystemobject") Set Filename = FSO.GetSpecialFolder(2) 'Create the full path name for the pdf-file Filename = Filename & "\" & MyFile & ".pdf" 'Save the current workbook as pdf in the user's temp folder. 'Note that we are going to include the workbook properties as well. 'If you do not want this set "IncludeDocProperties" to False. ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ Filename, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _ False 'Declare an Outlook application an a mail item. Dim oOutlookApp As Outlook.Application Dim oItem As Outlook.MailItem 'Start Outlook if it isn't running. Set oOutlookApp = GetObject(, "Outlook.Application") If Err <> 0 Then Set oOutlookApp = CreateObject("Outlook.Application") End If 'Create a new message. Set oItem = oOutlookApp.CreateItem(olMailItem) 'Add the attachments. oItem.Attachments.Add Filename oItem.Attachments.Add ActiveWorkbook.FullName 'Show the message. oItem.Display 'Cleanup Set FSO = Nothing Set FileName = Nothing Set oOutlookApp = Nothing Set oItem = Nothing End Sub
PowerPoint
Sub MailPdfPlusOriginal() '================================================================= 'Description: PowerPoint macro to create a pdf-file of the current ' presentation and add both the original and the ' pdf-version of the presentation as an attachment to ' a new Outlook message. ' 'Important! This macro requires a reference added to the ' Microsoft Outlook <version> Object Library ' In VBA Editor: Tools-> References... ' 'author : Robert Sparnaaij 'version: 1.0 'website: https://www.howto-outlook.com/howto/mailpdfplus.htm '================================================================= 'Verify if the presentation has been saved before so that we have a path to work with. 'If not, notify the user that there will be a safe dialog first. If ActivePresentation.Path <> "" Then ActivePresentation.Save Else Dim Msg, Style, Title, Response Msg = "This presentation has not been saved before." & vbLf & _ "Please save the presentation to disk first." & vbLf & _ "Without saving first, only the pdf-file will be attached." Style = vbInformation + vbOKOnly Title = "Save current presentation" Response = MsgBox(Msg, Style, Title) Dim dlgSaveAs As FileDialog Dim strCurrentFile As String Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs) If dlgSaveAs.Show = -1 Then strCurrentFile = dlgSaveAs.SelectedItems(1) ActivePresentation.SaveAs (strCurrentFile) End If Set dlgSaveAs = Nothing End If 'Get the name of the open file and strip any extension. Dim MyFile As String MyFile = ActivePresentation.Name intPos = InStrRev(MyFile, ".") If intPos > 0 Then MyFile = Left(MyFile, intPos - 1) End If 'Get the user's TempFolder to store the created pdf item in. Dim FSO As Object, TmpFolder As Object Set FSO = CreateObject("scripting.filesystemobject") Set FileName = FSO.GetSpecialFolder(2) 'Create the full path name for the pdf-file FileName = FileName & "\" & MyFile & ".pdf" 'Save the current presentation as pdf in the user's temp folder. 'Note that we are going to include the document properties as well. 'For an overview of the available options see; 'https://docs.microsoft.com/en-us/office/vba/api/PowerPoint.Presentation.ExportAsFixedFormat ActivePresentation.ExportAsFixedFormat _ FileName, _ ppFixedFormatTypePDF, _ ppFixedFormatIntentPrint, _ msoFalse, _ ppPrintHandoutVerticalFirst, _ ppPrintOutputSlides, _ msoFalse, _ , , , _ True, False, True, True, False 'Declare an Outlook application an a mail item. Dim oOutlookApp As Outlook.Application Dim oItem As Outlook.MailItem 'Start Outlook if it isn't running. Set oOutlookApp = GetObject(, "Outlook.Application") If Err <> 0 Then Set oOutlookApp = CreateObject("Outlook.Application") End If 'Create a new message. Set oItem = oOutlookApp.CreateItem(olMailItem) 'Add the attachments. oItem.Attachments.Add FileName oItem.Attachments.Add ActivePresentation.FullName 'Show the message. oItem.Display 'Cleanup Set FSO = Nothing Set FileName = Nothing Set oOutlookApp = Nothing Set oItem = Nothing End Sub