Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-10-2015, 03:21 PM
Michel777 Michel777 is offline Saving Mails as PDF Windows XP Saving Mails as PDF Office 2007
Novice
Saving Mails as PDF
 
Join Date: Apr 2010
Posts: 5
Michel777 is on a distinguished road
Default Saving Mails as PDF

Hi,



how would it be possible to save selected Mails as PDF (every Mail in a separate PDF) ?

The name of PDF = Subject of the Mail.

Thanks a lot in advance,

Michel
Reply With Quote
  #2  
Old 01-11-2015, 06:38 AM
Michel777 Michel777 is offline Saving Mails as PDF Windows XP Saving Mails as PDF Office 2007
Novice
Saving Mails as PDF
 
Join Date: Apr 2010
Posts: 5
Michel777 is on a distinguished road
Default

Look:http://www.sperrysoftware.com/Outlook/save-as-pdf.asp
Reply With Quote
  #3  
Old 01-11-2015, 07:54 AM
gmayor's Avatar
gmayor gmayor is offline Saving Mails as PDF Windows 7 64bit Saving Mails as PDF Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It is a little fiddly, though easy enough to save the messages as PDF by using a handful of pretty standard functions.

Basically the process is to save the message as MHT format from Outlook, then open that file in Word and save it from there as PDF. Given that the processing is not instantaneous, I would add a progress indicator, but the following will do it without:

Code:
Option Explicit
Private wdApp As Object
Private wdDoc As Object
Private bStarted As Boolean
Const strPath As String = "C:\Path\Email Messages\"

Sub SaveSelectedMessagesAsPDF()
'Select the messages to process and run this macro
Dim olMsg As MailItem

CreateFolders strPath        'Create the folder to store the messages if not present
    'Open or Create a Word object
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err Then
        Set wdApp = CreateObject("Word.Application")
        bStarted = True
    End If
    On Error GoTo 0
    For Each olMsg In Application.ActiveExplorer.Selection
        SaveAsPDFfile olMsg
    Next olMsg
lbl_Exit:
    If bStarted Then wdApp.Quit
    Set wdApp = Nothing
    Exit Sub
End Sub

Sub SaveAsPDFfile(olItem As MailItem)
Dim olNS As NameSpace
Dim fso As Object, tmpFolder As Object
Dim tmpPath As String
Dim strFileName As String
Dim strName As String
Dim oRegEx As Object

    Set olNS = Application.GetNamespace("MAPI")

    'Get the user's TempFolder to store the temporary file
    Set fso = CreateObject("Scripting.FileSystemObject")
    tmpPath = fso.GetSpecialFolder(2)

    'construct the filename for the temp mht-file
    strName = "email_temp.mht"
    tmpPath = tmpPath & "\" & strName

    'Save temporary file
    olItem.SaveAs tmpPath, 10
   
    'Open the temporary file in Word
    Set wdDoc = wdApp.Documents.Open(Filename:=tmpPath, _
                                     AddToRecentFiles:=False, _
                                     Visible:=False, _
                                     Format:=7)

    'Create a file name from the message subject
    strFileName = olItem.Subject
    'Remove illegal filename characters
    Set oRegEx = CreateObject("vbscript.regexp")
    oRegEx.Global = True
    oRegEx.Pattern = "[\/:*?""<>|]"
    strFileName = Trim(oRegEx.Replace(strFileName, "")) & ".pdf"
    strFileName = FileNameUnique(strPath, strFileName, "pdf")
    strFileName = strPath & strFileName

    'Save As pdf
    wdDoc.ExportAsFixedFormat OutputFileName:= _
                              strFileName, _
                              ExportFormat:=17, _
                              OpenAfterExport:=False, _
                              OptimizeFor:=0, _
                              Range:=0, _
                              From:=0, _
                              To:=0, _
                              Item:=0, _
                              IncludeDocProps:=True, _
                              KeepIRM:=True, _
                              CreateBookmarks:=0, _
                              DocStructureTags:=True, _
                              BitmapMissingFonts:=True, _
                              UseISO19005_1:=False

    ' close the document and Word
    wdDoc.Close
    'Cleanup
    Set olNS = Nothing
    Set olItem = Nothing
    Set wdDoc = Nothing
    Set oRegEx = Nothing
lbl_Exit:
    Exit Sub
End Sub

Private Function CreateFolders(strPath As String)
Dim strTempPath As String
Dim lngPath As Long
Dim vPath As Variant
    vPath = Split(strPath, "\")
    strPath = vPath(0) & "\"
    For lngPath = 1 To UBound(vPath)
        strPath = strPath & vPath(lngPath) & "\"
        If Not FolderExists(strPath) Then MkDir strPath
    Next lngPath
lbl_Exit:
    Exit Function
End Function

Private Function FileNameUnique(strPath As String, _
                               strFileName As String, _
                               strExtension As String) As String
Dim lngF As Long
Dim lngName As Long
    lngF = 1
    lngName = Len(strFileName) - (Len(strExtension) + 1)
    strFileName = Left(strFileName, lngName)
    Do While FileExists(strPath & strFileName & Chr(46) & strExtension) = True
        strFileName = Left(strFileName, lngName) & "(" & lngF & ")"
        lngF = lngF + 1
    Loop
    FileNameUnique = strFileName & Chr(46) & strExtension
lbl_Exit:
    Exit Function
End Function

Private Function FolderExists(ByVal PathName As String) As Boolean
Dim nAttr As Long
    On Error GoTo NoFolder
    nAttr = GetAttr(PathName)
    If (nAttr And vbDirectory) = vbDirectory Then
        FolderExists = True
    End If
NoFolder:
    Exit Function
End Function

Private Function FileExists(ByVal Filename As String) As Boolean
Dim nAttr As Long
    On Error GoTo NoFile
    nAttr = GetAttr(Filename)
    If (nAttr And vbDirectory) <> vbDirectory Then
        FileExists = True
    End If
NoFile:
    Exit Function
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving sent mails to specific folder kammil121 Outlook 0 10-22-2014 02:26 AM
Saving Mails as PDF E-Mails Encrypted wildwilly5891 Outlook 1 10-22-2011 06:16 PM
Save my e-mails, Please help! lawpeder Outlook 2 07-08-2011 03:32 AM
Saving Mails as PDF Sending mails Heini Outlook 1 07-19-2009 05:27 AM
Saving Mails as PDF Download of mails to both outlook and web-based mails ran_sushmi Outlook 2 03-26-2009 05:37 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:41 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft