Click here to Skip to main content
15,907,183 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I don't have very english experience
I have a problem when I goes to Attachment a Pdf on a Mail for Send on my application.

I'm usign OpenXml for create a .docx document with information of a Db, tnen that .docx try to converter in a Pdf using Interop Word, and create a Mail with the Pdf Attachment and Send but the file is damaged and don´t open.

I have this code to converter

C#
public static bool ConvertDocument(string file)
        {
            object missing = System.Reflection.Missing.Value;

            Word.Application word = null;
            Word.Document doc = null;

            try
            {
                word = new Word.Application();
                word.Visible = false;
                word.ScreenUpdating = false;

                Object filename = (Object)file;

                doc = word.Documents.Open(ref filename, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing);
                doc.Activate();

                if (Path.GetExtension(file) == ".docx")
                    file = file.Replace(".docx", ".pdf");
                else
                    file = file.Replace(".doc", ".pdf");

                object fileFormat = Word.WdSaveFormat.wdFormatPDF;

                doc.ExportAsFixedFormat(file, Word.WdExportFormat.wdExportFormatPDF, false, Word.WdExportOptimizeFor.wdExportOptimizeForPrint,
                    Word.WdExportRange.wdExportAllDocument, 1, 1, Word.WdExportItem.wdExportDocumentContent, true, true, Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks,
                    true, true, false, ref missing);
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                object saveChanges = Word.WdSaveOptions.wdSaveChanges;
                if (doc != null)
                {
                    ((Word._Document)doc).Close(ref saveChanges, ref missing, ref missing);
                    doc = null;
                }

                if (word != null)
                {
                    ((Word._Application)word).Quit(ref saveChanges, ref missing, ref missing);
                    word = null;
                }
            }

            return true;
        }


Thanks from Colombia if anyone can help :)
Posted

1 solution

I used Save As instead of export and it works perfectly.I have Office 2007 and Windowx XP/VS 2010.

public bool ConvertDocument(string file)
       {
           object missing = System.Reflection.Missing.Value;

           Word.Application word = null;
           Word.Document doc = null;

           try
           {
               word = new Word.Application();
               word.Visible = false;
               word.ScreenUpdating = false;

               Object filename = (Object)file;

               doc = word.Documents.Open(ref filename, ref missing,
                   ref missing, ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing);
               doc.Activate();

               if (Path.GetExtension(file) == ".docx")
                   file = file.Replace(".docx", ".pdf");
               else
                   file = file.Replace(".doc", ".pdf");

               object pdf = Word.WdSaveFormat.wdFormatPDF;

               doc.SaveAs(file, ref pdf, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing);

           }
           catch (Exception ex)
           {
               return false;
           }
           finally
           {

                   ((Word._Document)doc).Close(ref missing, ref missing, ref missing);
                   doc = null;



                   ((Word._Application)word).Quit(ref missing, ref missing, ref missing);
                   word = null;

           }

           return true;
       }
 
Share this answer
 
Comments
Estephanny_777 11-Sep-12 17:04pm    
Hello and thanks Ashraff Ali Wahab, but I try with the code you post and didn't work anyway, the file on the mail still is damaged, ¿any other solution? is important please, and thanks again :)
Estephanny_777 11-Sep-12 17:13pm    
Hello and thanks Ashraff Ali Wahab, but I try with the code you post and didn't work anyway, the file on the mail still is damaged, ¿any other solution? is important please
Ashraff Ali Wahab 11-Sep-12 17:29pm    
Then there might be something wrong with your machine,I have tried it and i am able to get the PDF without any issue.can you try in different machine.

Also put your machine configuration also.
Estephanny_777 12-Sep-12 16:26pm    
Hi, look Ashraff the file on the folder open and don't show dameged, but when i'm going to Attachment in the email get damaged and dont open,

thanks

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