Hi
I am using the Microsoft Word 14.0 Object Library on VS2010 in a WPF application to export a batch of word documents from a database to PDFs. It is working fine for any normal word document but not for those that have a space at the end of the filename. Example: test .doc, test .docx
I have debugged the code and it reads the file fine but on exporting it, it gives the "The directory is not valid" error. I tried manually exporting a file to PDF with a space at the end of the filename from MS Word and it worked perfectly fine.
The code to open and export a word document:-
var wordApplication = new Microsoft.Office.Interop.Word.Application(); var wordDocument = wordApplication.Documents.Open(documentPath); string[] filePath = documentPath.Split('\\'); string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(documentName); destinationPath = "C:\\Users\\manvir singh\\" + filePath[filePath.Length - 5] + "\\" + filePath[filePath.Length - 4] + "\\" + filePath[filePath.Length - 3] + "\\" + filePath[filePath.Length - 2] + "\\" + fileNameWithoutExtension; if (!Directory.Exists(destinationPath)) { Directory.CreateDirectory(destinationPath); } string pdfPath = destinationPath + "\\" + fileNameWithoutExtension + ".pdf"; wordDocument.ExportAsFixedFormat(pdfPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
Can I please be assisted on this?
Thanks