Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
2.60/5 (3 votes)
See more:
Sir,
I have make an application in which I have taken word file in which we can insert value in word.. I crate class file like.'AutomateWord.cs' as following code.
C#
 public static class Document
    {
        public static bool Process(string strWordDoc, string strPDFDoc, ArrayList dReplacement)
        {
            //A set of objects needed to pass into the calls
            object oMissing = System.Reflection.Missing.Value;
            object oFalse = false;
            object oTrue = true;
            //The variable that will store the return value
            bool bolOutput = true;
            //Creates the needed objects (the application and the document)
            Word._Application oWord;
            Word._Document oDoc;
            //Checks to see if the file does not exist (which would throw an error)
            if (!System.IO.File.Exists(strWordDoc))
            {
                //Since the file does not exist, write out the
                //error to the console and exit
                Console.WriteLine("The file does not exist on the specified        path.");
                return false;
            }

            try
            {
                oWord = new Word.Application();
               
                oWord.Visible = true;
              
                oDoc = oWord.Documents.Open(strWordDoc, oFalse, oTrue);
               
                foreach (Word.Bookmark oRange in oDoc.Content.Bookmarks)
                {
                    for (int i = 0; i < dReplacement.Count; i++)
                    {
                        string[] value = dReplacement[i].ToString().Split(':');

                        if (oRange.Name == value[0].ToString())
                        {
                            oRange.Range.Text = value[1].ToString();
                        }

                    }
                }
               
                oDoc.ExportAsFixedFormat(strPDFDoc, Word.WdExportFormat.wdExportFormatPDF);
               
                bolOutput = true;
            }
            catch (Exception ex)
            {
                //Here is where you put your logging code
                Console.WriteLine(ex.ToString());
                bolOutput = false;
            }
            finally
            { //Releases the objects
                oDoc = null;
                oWord = null;
            }
            return bolOutput;

        }



    }

we use bookmark and replacement of in word file..
   on button click  I have write code as follow..
    
      Dictionary<string,> dkeywords = new Dictionary<string,>();
            /*create object "**date**" in word doc file */
            ArrayList ar = new ArrayList();
            ar.Add("date" + ":" + Label2.Text);
            //ar.Add("date" + ":" + lbldate1.Text);
            /*create object "**company name**" in word doc file */
            ar.Add("id" + ":" + spi);
            /*create object "**customer name**" in word doc file */
            ar.Add("sr" + ":" + srno);
            /*create object "**price1**" in word doc file */
            ar.Add("company" + ":" + TextBox2.Text);
            /*create object "**price2**" in word doc file 
            dkeywords.Add("*Address*", ad);
            dkeywords.Add("*date1*", lbldate1.Text);
            /*create object "**company name**" in word doc file */
            ar.Add("id1" + ":" + spi);
            /*create object "**customer name**" in word doc file */
            ar.Add("sr1" + ":" + srno);
            /*create object "**price1**" in word doc file */
            ar.Add("company1" + ":" + TextBox2.Text);
            /*create object "**price2**" in word doc file */
            ar.Add("address" + ":" + ad);
            ar.Add("address1" + ":" + ad);
            /*create object "**price3**" in word doc file */
            ar.Add("client" + ":" + DropDownList1.Text);
            ar.Add("client1" + ":" + DropDownList1.Text);
            ar.Add("design" + ":" + dec);
            ar.Add("mo" + ":" + spm);
            ar.Add("design1" + ":" + dec);
            ar.Add("mo1" + ":" + spm);
            ar.Add("design2" + ":" + dec);
            ar.Add("mo2" + ":" + spm);
            ar.Add("sign" + ":" + sp);
            ar.Add("sign1" + ":" + sp);
            ar.Add("sign2" + ":" + sp);
            ar.Add("price" + ":" + TextBox9.Text);
            Random randamnumber = new Random();
            int i5 = randamnumber.Next(1, 9000);
           
            sale.Document.Process(Environment.CurrentDirectory + "\\Best Offer - Pinnacle BK3 CNC.doc",
                Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\ Pinnacle BK3 CNC" + Convert.ToInt32(i5) + ".pdf", ar);
            System.Diagnostics.Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\  Pinnacle BK3 CNC" + Convert.ToInt32(i5) + ".pdf");

the problem is to face the word file cannot open & also pdf not created,so give me the proper solution for this problem.

Thanks sir
Posted
Updated 21-Jun-12 2:28am
v2

1 solution

hi,

refer this link
Convert Word-Documents to PDF on an ASP.NET Server[^]

Best Luck
 
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