Essentially I need to create a PDF archiver that saves the content of a MailItem into a PDF file. The code is below:
mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
string pdfPath = Path.Combine(fullPath + fileName + ".pdf");
Microsoft.Office.Interop.Word.Document doc = mailItem.GetInspector.WordEditor;
doc.SaveAs(pdfPath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF);
The exception happens on the .SaveAs method. What I've essentially boiled it down to, is that it has something to do with the file path, as I tried changing the file path to be shorter, to which the exception did not occur. The problem is, that it has to be in the longer file path structure. I did also consider that maybe it reached the max length of a file path (255), but from what I could tell by running pdfPath.Length, the length came out to be 81.
Does anyone have any ideas?
I looked into this a bit more, turns out that when working with file paths, you can easily use a single /
for navigating the path, say for example: C:/User/Desktop
.
This is the case, UNTIL
you work with Word documents. Word doesn't appreciate single /
, but prefers double \\
. So, C:/User/Desktop
becomes C:\\User\\Desktop
.
Reason why I'm saying this, is because, while there is a File path Max length, I did not reach it.
The problem seemed to happen because there was a space in a folder name, like: C:/User/Desktop/Folder Name
.
I then used the double backslash instead, and it worked perfectly.