cancel
Showing results for 
Search instead for 
Did you mean: 

C# or .NET code to print .msg files to PDF with images

sonuiiml
Level 5

Hello Team, 
I have a requirement to convert .msg files in specific folders to pdf. I have a C# code which uses the HTML from the email and converts to pdf. The drawback is that the  images are not printed in pdf.  So I am wondering is there any code and dll packages that could replicate the operation of printing the email like the way one normally prints email(File->Print->PrintToPDF).

I have tried using the below links but couldn't implement it in BP

How to convert outlook email (.msg) to pdf c# ? - Microsoft Q&A

How do I convert a .msg file to a .pdf file using C#? - CodeProject

Please let me know



------------------------------
Susamay Halder Consultant
Consultant
Bruce Power
+1(437)217-1086
------------------------------
8 REPLIES 8

GAVIN.RUDLING
Level 7

Hi Susamay

Recently I have been working on something very similar.

I have uploaded a file, that I am still testing. I will tell you what it is supposed to do.

Converts an Outlook msg file to pdf format.
If no output path is provided the code will create a .pdf file in the same directory as your .msg file. The path to the .pdf file will be returned as an output of the action.

Please note that this code requires the Microsoft Outlook and Word applications to be installed on your machine.

The .html file created during the process will be deleted at the end.

Feel free to modify is as needed.



------------------------------
Gavin Rudling
Digital Consultant & Developer
Cog3nt
Africa/Johannesburg
0813906789
------------------------------

Hi Susamay

You can please ignore the 1st attachment, it does not work, some of the libraries were missing.

You can try this second attempt.

Kind regards

Gavin



------------------------------
Gavin Rudling
Digital Consultant & Developer
Cog3nt
Africa/Johannesburg
0813906789
------------------------------

Thank you so much Galvin. I am trying to run the code but it gives the below error 
Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))



------------------------------
Susamay Halder Consultant
Consultant
Bruce Power
+1(437)217-1086
------------------------------

I tried running the code in a virtual machine and it gives the the below error
"Cannot create ActiveX component."



------------------------------
Susamay Halder Consultant
Consultant
Bruce Power
+1(437)217-1086
------------------------------

Hi Susamay

One of my colleagues said he experienced the same thing.

As I said, this is something new I am trying. I have it working on a BPC platform. My colleague says it works on one of our clients' environments as well, but not on his local machine.

However he says if Outlook is open, it does work. I am trying to find out what the differences are in the environments.

Kind regards

Gavin



------------------------------
Gavin Rudling
Digital Consultant & Developer
Cog3nt
Africa/Johannesburg
0813906789
------------------------------

Hi Susamay

Can you try the following:

  1. DLL files: It might be that you need to add DLL: Microsoft.Office.Interop.Outlook.dll into the Blue Prism folder1.


------------------------------
Gavin Rudling
Digital Consultant & Developer
Cog3nt
Africa/Johannesburg
0813906789
------------------------------

Hi Gavin, 
Thank you so much for your support. I found the reason why the error was thrown. Its because of security policies. Its working fine in my personal machine since I am the administrator. However, for my office laptop the security policies are restricting it from happening at first place.
Below is the article for reference 
Program is trying to send an e-mail message on your behalf - Outlook | Microsoft Learn

After referring to the article I believe the below change needs to be implemented as an administrator

Found out that it was blocked by the following policy: HKEY_CURRENT_USER\Software\Policies\Microsoft\office\16.0\outlook\security\promptoomsaveas

The key was set to 0 = Automatically Deny. Changing the key to 2 = Automatically Approve solved the problem

Could you try in that machine where it is not working? I hope it will work



------------------------------
Susamay Halder Consultant
Consultant
Bruce Power
+1(437)217-1086
------------------------------

Hi Susamay

 

Thanks for the feedback much appreciated. I hope the Object helps.

 

Kind regards

Gavin