Hello Friends,
I want to convert word document to pdf using Microsoft.Office.Interop.Word dll.Please refer the code below:
public void ConvertWordToPDF(string SourcerFileName, string DestinationFolder)
{
Microsoft.Office.Interop.Word.Application _Word = new Microsoft.Office.Interop.Word.Application();
object _MissingValue = System.Reflection.Missing.Value;
_Word.Visible = false;
_Word.ScreenUpdating = false;
object filename = (object)SourcerFileName;
Document doc = _Word.Documents.Open(ref filename, ref _MissingValue,
ref _MissingValue, ref _MissingValue, ref _MissingValue, ref _MissingValue, ref _MissingValue,
ref _MissingValue, ref _MissingValue, ref _MissingValue, ref _MissingValue, ref _MissingValue,
ref _MissingValue, ref _MissingValue, ref _MissingValue, ref _MissingValue);
doc.Activate();
if (!Directory.Exists(DestinationFolder))
{
Directory.CreateDirectory(DestinationFolder);
}
string pdfFileName = Path.ChangeExtension(SourcerFileName, "pdf");
pdfFileName = pdfFileName.Substring(pdfFileName.LastIndexOf("\\") + 1, pdfFileName.Length - pdfFileName.LastIndexOf("\\") - 1);
object pdfoutputFileName = DestinationFolder + "\\" + pdfFileName;
object pdffileFormat = WdSaveFormat.wdFormatPDF;
doc.SaveAs(ref pdfoutputFileName,
ref pdffileFormat, ref _MissingValue, ref _MissingValue,
ref _MissingValue, ref _MissingValue, ref _MissingValue, ref _MissingValue,
ref _MissingValue, ref _MissingValue, ref _MissingValue, ref _MissingValue,
ref _MissingValue, ref _MissingValue, ref _MissingValue, ref _MissingValue);
object saveChangesPDF = WdSaveOptions.wdDoNotSaveChanges;
((_Document)doc).Close(ref saveChangesPDF, ref _MissingValue, ref _MissingValue);
_Word.NormalTemplate.Saved = true;
((_Application)_Word).Quit(ref _MissingValue, ref _MissingValue, ref _MissingValue);
File.Delete(outputFileName.ToString());
Marshal.ReleaseComObject(doc);
Marshal.ReleaseComObject(_Word);
}
The above code is working on some systems. But on some system it will give the below exception:
System.Runtime.InteropServices.COMException was unhandled
HelpLink=C:\Program Files (x86)\Microsoft Office\Office12\1033\WDMAIN11.CHM#36966
Message=Command failed
Source=Microsoft Word
ErrorCode=-2146824090
StackTrace:
at Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object& FileName, Object& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles, Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts, Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELetter, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Object& LineEnding, Object& AddBiDiMarks)
at GeneratePDF.ConvertWordToPdf.ConvertWordToPDF(String SourcerFileName, String DestinationFolder)
at WordToPDF.Form1.btnConvert_Click(Object sender, EventArgs e) in D:\Mohit\WordToPDF\Form1.cs:line 39
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WordToPDF.Program.Main() in D:\Mohit\WordToPDF\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Please help me out friends, I will be very thankful to all.
Thanks & Regards
Mohit Sharma