0

I have a template word file in SQL server database database that i need to change sum texts on it. I try to download the file first to temp files and then I load it to my code. is there is a method to load the file directly to my code I using this (saving file to drive first) after that I need to change image with other image in DB.

public Boolean save_agaza_file(string pattth)
    {
        Boolean result = false;
        Document wordDoc = new Document();
        Application wordApp = new Application();
        try
        {
            //OBJECT OF MISSING "NULL VALUE"

            Object oMissing = System.Reflection.Missing.Value;

            Object oTemplatePath = pattth;



            //wordDoc = new Document();

            wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
            DateTime day_start = DateTime.Now.AddDays(1);
            DateTime day_end = DateTime.Now.AddDays(2);
            foreach (Range rr in wordDoc.StoryRanges)
            {


            }
            foreach (Field myMergeField in wordDoc.Fields)
            {


                Range rngFieldCode = myMergeField.Code;

                String fieldText = rngFieldCode.Text;



                // ONLY GETTING THE MAILMERGE FIELDS

                if (fieldText.StartsWith(" MERGEFIELD"))
                {

                    // THE TEXT COMES IN THE FORMAT OF

                    // MERGEFIELD  MyFieldName  \\* MERGEFORMAT

                    // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"

                    Int32 endMerge = fieldText.IndexOf("\\");

                    Int32 fieldNameLength = fieldText.Length - endMerge;

                    String fieldName = fieldText.Substring(11, endMerge - 11);

                    // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE

                    fieldName = fieldName.Trim();

                    // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//

                    // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

                    if (fieldName == "EmpName")
                    {

                        myMergeField.Select();

                        // wordApp.Selection.TypeText("محمد السيد زكى");
                        wordApp.Selection.TypeText(this.Reqested_emp.Name);

                    }
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.show(e.Message);
        }
    }

method to download

string sql = @" select * from Agaza_template where template_id=" + agaza_id + ";";
                if (Form1.conn.State != ConnectionState.Open)
                {
                    Form1.conn.Open();
                    SqlCommand command = new SqlCommand(sql, Form1.conn);
                    SqlDataReader reader = command.ExecuteReader();
                    // reader.Read();

                    //if (reader.HasRows)

                    if (reader.Read())
                    {
                        byte[] b = (byte[])reader[2];

                        //  string s = Path.GetFileName(reader[1].ToString());
                        string s = reader[1].ToString();
                        string result = Path.GetTempPath();
                         file_temp_name = result + s;
                        FileStream fs = new FileStream(result + s, FileMode.Create);


                        fs.Write(b, 0, b.Length);
                        fs.Close();



                    }
                }


                Form1.conn.Close();

            }
  • This looks like you are using the Word Interop API (`Microsoft.Office.Interop.Word`). Is this the case? Must you use this API to access your Word document? If so, AFAIK, there is no way to open a document (via Word interop) from a `Stream`. There may be some Interop workaround, but my recommendation would be to continue on the path you are already on. I would save the file to the Temp folder, which can be accessed using Environment variables (see [this question](https://stackoverflow.com/q/20130769/8061994) and its answers). Please let me know if I am misunderstanding your question. –  Sep 10 '18 at 22:28
  • exact like you say but i have problems on the method the program is to faster than write and read from hard drive so the program is stoped so i use Thread.Sleep(5000); which make the program delayed – Mohamed Embaby Sep 10 '18 at 22:46
  • I'm sorry, I don't know what you mean. I don't see a call to Thread.Sleep(5000) in your code. Are the read and write occurring simultaneously, and one operation is occurring faster than the other? –  Sep 10 '18 at 22:57

0 Answers0