Word VBA snippets

De onderstaande code zal het actieve document - onder dezelfde naam in dezelfde directory - opslaan als PDF-document en deze als bijlage toevoegen aan een concept e-mail in Outlook.

Option Explicit

Dim objDoc As Document
Dim strPDF As String

Sub initEmailPDF()

    Set objDoc = ActiveDocument
    
    If objDoc.Path = "" Then
        MsgBox "Sla eerst het Word-document op!", vbExclamation
        Exit Sub
    End If
    
    strPDF = objDoc.Path & Application.PathSeparator & objDoc.Name & ".pdf"

    savePDF
    emailPDF

End Sub

Sub savePDF()

    objDoc.ExportAsFixedFormat OutputFileName:=strPDF, _
                                       ExportFormat:=wdExportFormatPDF, _
                                       OpenAfterExport:=False, _
                                       OptimizeFor:=wdExportOptimizeForPrint, _
                                       Range:=wdExportAllDocument, _
                                       IncludeDocProps:=True, _
                                       CreateBookmarks:=wdExportCreateWordBookmarks, _
                                       BitmapMissingFonts:=True

End Sub

Sub emailPDF()

    Dim objOutlook As Object
    Dim objMail As Object

    Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(0)

    With objMail
        .To = "aan@email.nl"
        .CC = "cc@email.nl"
        .Subject = "onderwerp"
        .Body = "bericht"
        .Attachments.Add strPDF
        .Display
    End With

    Set objMail = Nothing
    Set objOutlook = Nothing

End Sub