I would like to make an outlook macro to save a mail body and HEADER just as if it were printed out by outlook or pdfcreator. The Sender, cc, bcc , time, to, subject are data that must be in the pdf.
Using this post and others:
I coded this macro that:
- takes the selected mails in outlook
- makes a new folder in hardcoded folder
- print with the wordeditor the body mail as PDF
My issue is that the wordEditor objecto doesn't save the HEADER of the mailItem. It is important for me because I have the information o fwho sent it, when, the e-mail address, Subject etc I want to know how to add the header from the wordEditor object.
Option Explicit
Sub mail_to_pdf_sof()
Dim outApp As Object, objOutlook As Object, objFolder As Object, myItems As Object, myItem As Object, coll As Object, Sel As Object, objInspector As Object, objDoc As Object
Dim psName As String, pdfName As String, strFolderpath As String, Path As String, time_record As String, FileName As String
Dim rol As Integer, indice As Integer, i As Integer
Set outApp = CreateObject("Outlook.Application")
Set objOutlook = outApp.GetNamespace("MAPI")
' PATH TO SAVE PDFs
Path = "F:\"
Path = Path & Format(Date, "yyyy-mm-dd") & " - Mail to PDF" & "\"
On Error Resume Next
MkDir Path
On Error GoTo 0
' GET MAILS SELECTED IN OUTLOOK FOR THE CONVERSION AND SAVE TO PDF
Set coll = New VBA.Collection
If TypeOf Application.ActiveWindow Is Outlook.Inspector Then
coll.Add Application.ActiveInspector.CurrentItem
Else
Set Sel = Application.ActiveExplorer.Selection
For i = 1 To Sel.Count
coll.Add Sel(i)
Next
End If
' SET COUNTERS
rol = 1
indice = 1
time_record = Format(Now, "yyyymmddhhmm")
' SAVE EACH MAIL AS PDF BUT WITHOUT THE HEADER
For Each myItem In coll
' ELIMINATES CHARACTER THAT ARE NOT ALLOWD AND SET A MAX TO FILE NAME LENGTH
FileName = myItem.SenderName & " - " & myItem.Subject
FileName = Replace(FileName, ":", "")
FileName = Replace(FileName, "|", "-")
FileName = Replace(FileName, "/", "-")
FileName = Replace(FileName, "\", "-")
FileName = Replace(FileName, "\\", "-")
FileName = Replace(FileName, Chr(34), "")
If Len(FileName) > 90 Then
FileName = Left(FileName, 90)
End If
' SAVE AS PDF
Set objInspector = myItem.GetInspector
Set objDoc = objInspector.WordEditor
objDoc.ExportAsFixedFormat Path & time_record & " - " & rol & " - " & "Mail - " & FileName & ".pdf", 17
Set objInspector = Nothing
Set objDoc = Nothing
rol = rol + 1
indice = indice + 1
Next myItem
End Sub
I wonder if anyone knows a sensible solution to this problem, thanks!