Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Friends,

I want to convert word document to pdf using Microsoft.Office.Interop.Word dll.Please refer the code below:
C#
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;
            // Cast as Object for word Open method
            object filename = (object)SourcerFileName;

            // Use the dummy value as a placeholder for optional arguments
            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
Posted
Updated 1-Jan-12 22:18pm
v2
Comments
Lee Weston 1-Apr-12 9:08am    
I think I might be struggling with the same error because I have the 32-bit Office installation (Program Files (x86)) and I'm running on a 64-bit machine?

 
Share this answer
 
install save as pdf plugin for office 2007
http://www.microsoft.com/en-us/download/confirmation.aspx?id=7[^]

this will help u
 
Share this answer
 
Plz get the help from my frnd Google.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900