Reputation: 334
I have expended a good deal of effort trying to convert emails to PDF
.
I am using Delphi 10.4
although that is not necessarily relevant to the question.
I came up with a solution that involves extraction of the body from the email in whatever format (HTML
, RTF
or TXT
). I use INDY
for this or Outlook
if email is in MSG
format.
I then save the body to file and open it using MS Word
via automation. Then it should be a simple matter of saving the Word
document in PDF
format.
However, MS Word
doesn't seem to read html
files that well.
From the numerous samples of emails that I have tried, I have come across several issues which were complex to solve.
Examples:
html
tables expanding beyond the document's page width. I solved this by working out what the page width is, setting the offending table's width as fixed and setting it to the page width and finally resizing it's columns proportionately to its new width.html
tables with differing numbers of columns/cells per row. That causes a crash. I solved that by handling the exception and iterating through each table by row and working with its cells rather than columns.InlineShapes
, checking whether they are within a table and, if so setting their width to the cell width.There have been other issues, but I now have something that seems to work pretty well on a fairly disparate bunch of emails.
But I would think it incredibly likely that there will be new issues that will crop up from time to time and since this procedure is designed to deal unsupervised with batches of emails, this is a concern.
So my question is, does anyone know of a better way of dealing with this? For example, is there some simple way of getting Word to to "nicely" format the html
on loading so that it displays and saves to PDF
in a readable fashion similar to how it looks when you open the same email in Outlook
.
Upvotes: 0
Views: 144
Reputation: 381
Have you tried using the WordEditor property of the Outlook Inspector object? This returns the Microsoft Word Document Object Model of the message and you can export directly to PDF from that.
Here is a basic example...
Private Sub Demo()
Dim MailItem As MailItem
Dim FileName As String
FileName = "C:\Users\Sam\Desktop\Email.pdf"
Set MailItem = ActiveExplorer.Selection.Item(1)
With MailItem.GetInspector
.WordEditor.ExportAsFixedFormat FileName, 17
.Close 0
End With
MsgBox "Export complete"
End Sub
Upvotes: 1