Skip to content

Comman Failed Error in doc.saveas method.

Vasanth Krishnan

Vasanth Krishnan

Asked Jul 23, 2013, 10:26 PM EDT1 answer
Share
LinkedInX

Hi

I am trying to convert doc to pdf using the following code.


C#
public void ConvertPdf()
  {
      object sFilename = "Report5.doc";
      object FromLocation = Server.MapPath("~/Form5/" + sFilename.ToString());
      string ChangeExtension = sFilename.ToString().Replace(".doc", ".pdf");
      object ToLocation = Server.MapPath("~/Form5PDF/" + ChangeExtension);

      //string ConvertResult = word2pdf.Word2PdfCOnversion(FromLocation, ToLocation);

      Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();

      object oMissing = System.Reflection.Missing.Value;

      word.Visible = false;
      word.ScreenUpdating = false;
 
      Document doc = word.Documents.Open(ref FromLocation, ref oMissing,
      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
Code
      ref oMissing, ref oMissing, ref oMissing, ref oMissing);

      doc.Activate();
      object fileFormat = WdSaveFormat.wdFormatPDF;
 
      doc.SaveAs(ref FromLocation,
      ref fileFormat, ref oMissing, ref oMissing,
      ref oMissing, ref oMissing, ref oMissing, ref oMissing,
      ref oMissing, ref oMissing, ref oMissing, ref oMissing,
      ref oMissing, ref oMissing, ref oMissing, ref oMissing);
 
      object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
      ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
      doc = null;
}

In that line doc.SaveAs, i am getting the error Command failed error. I am using microsoft visual web developer tool to develop the site.

Hope this is clear. Please let me know if this is not.

Thanks.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Try this
    In the doc.SaveAs method you need to specify the output file name and not the original file name.

                 doc.SaveAs(ref outputFileName,
                ref fileFormat, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0