3 Replies - 2336 Views - Last Post: 04 September 2010 - 04:22 PM Rate Topic: -----

#1 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

email Word File contents

Posted 04 September 2010 - 05:14 AM

hello
I have an application where I create MS Word object and then fill it with some data and tables
after that I have the choice either to save the word file or to send the contents as an email without saving them

so the problem i want to get all the contents of the document (pictures, tables, ...etc) and insert them into an email body

here is the code that creates the contents


            Globals.gallery.MoveFirst(); //This is a recordset to a table
            string[] picNames = new string[Globals.gallery.RecordCount];
            Image[] picImages = new Image[Globals.gallery.RecordCount];
            // get an array of images to be added to a table
            for (int i = 0; i < Globals.gallery.RecordCount; i++)
            {
                picNames[i] = Globals.gallery.Fields["Name"].Value.ToString();
                picImages[i] = Image.FromFile(Globals.gallery.Fields["Path"].Value.ToString());
                Globals.gallery.MoveNext();
            }

            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            object missing = System.Reflection.Missing.Value;
            object isVisible = true;
            // Create a new Document, by calling the Add function in the Documents collection
            Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            // need to see the created document, so make it visible
            wordApp.Visible = true;
            aDoc.Activate();
            // Global Constant enumerations are members of Word and can be assigned to properties
            // Set alignment to the center of the document

            //wordApp.Activedocument.PageSetup.TopMargin = (float)20;
            //wordApp.Activedocument.PageSetup.BottomMargin = (float)10;

            // Set up the header information.
            object start = 0;
            object end = 0;
            Microsoft.Office.Interop.Word.Range rng = wordApp.Activedocument.Range(ref start, ref end);
            rng.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            rng.InsertBefore("Gallery");
            rng.Font.Name = "Arial";
            rng.Font.Size = 30;
            rng.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlue;
            rng.InsertParagraphAfter();

            rng.SetRange(rng.End, rng.End);

            rng.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            rng.InsertBefore(Globals.currentCust + " Favorites List");
            rng.Font.Name = "Ancestory SF";
            rng.Font.Size = 20;
            rng.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkGreen;
            rng.InsertParagraphAfter();

            rng.SetRange(rng.End, rng.End);

            rng.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
            rng.InsertBefore(Globals.currentCust + " Notes:");
            rng.Font.Name = "Ancestory SF";
            rng.Font.Size = 15;
            rng.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBrown;
            rng.InsertParagraphAfter();

            rng.SetRange(rng.End, rng.End);
            
            int startObj = 0;
            Globals.users.Find("UID=" + Globals.id, 0, ADODB.SearchDirectionEnum.adSearchForward, startObj);
            rng.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
            rng.InsertBefore(Globals.users.Fields["UNotes"].Value.ToString());
            rng.Font.Name = "Arial Black";
            rng.Font.Size = 10;
            rng.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
            rng.InsertParagraphAfter();

            rng.SetRange(rng.End, rng.End);

            Object defaultTablebehavior = Type.Missing;
            Object autoFitbehavior = Type.Missing;
            wordApp.Activedocument.Tables.Add(rng, 1, 2,
                ref defaultTablebehavior, ref autoFitbehavior);

            Microsoft.Office.Interop.Word.Table tbl = wordApp.Activedocument.Tables[1];
            tbl.Range.Font.Size = 8;
            tbl.Range.Font.Name = "Verdana";

            Object style = "Colorful Grid - Accent 4";
            tbl.set_Style(ref style);
            tbl.ApplyStyleFirstColumn = false;
            tbl.ApplyStyleLastColumn = false;
            tbl.ApplyStyleLastRow = false;

            // Insert header text and format the columns.
            tbl.Cell(1, 1).Range.Text = "Picture";

            Microsoft.Office.Interop.Word.Range rngCell;
            rngCell = tbl.Cell(1, 2).Range;
            rngCell.Text = "Name";
            rngCell.ParagraphFormat.Alignment =
                Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            // Fill the table with data.            


            Object beforeRow = Type.Missing;

            for (int i = 0; i < Globals.gallery.RecordCount; i++)
            {
                tbl.Rows.Add(ref beforeRow);
                Bitmap newBit = new Bitmap(picImages[i], new Size(100, 100));
                newBit.Save("temp.jpg");
                string path = Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf("\\")) + "\\temp.jpg";
                rngCell = tbl.Cell(i + 2, 1).Range;
                object obj = (object)rngCell;
                rngCell.InlineShapes.AddPicture(path, ref missing, ref missing, ref obj);
                tbl.Cell(i + 2, 2).Range.Text = picNames[i];
            }





till this line I didn't save the document
and I want to take these contents with the same format and copy them to the email body


            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("ahmedn32004@yahoo.com", "Ahmedn1");
            mail.To.Add(new MailAddress("ahmedn32004@yahoo.com", "Ahmedn1 Receiver"));
            mail.Subject = "Test";



any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: email Word File contents

#2 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

Re: email Word File contents

Posted 04 September 2010 - 08:58 AM

I could find some way

mail.Body = wordApp.Activedocument.Content.Text;


but the problem I need it with the same format, pictures and colors as they are
how can I do this ?
Was This Post Helpful? 0
  • +
  • -

#3 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: email Word File contents

Posted 04 September 2010 - 02:57 PM

How can you write code like that and then be stuck on how to output it to an email?
Was This Post Helpful? 0
  • +
  • -

#4 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

Re: email Word File contents

Posted 04 September 2010 - 04:22 PM

View PostImdsm, on 04 September 2010 - 01:57 PM, said:

How can you write code like that and then be stuck on how to output it to an email?


it is my first time to deal with emails
and the email body is just a text

I need to insert all the tables, pictures and formats in the email body

I had the idea of saving the file as an html
then copy the html code into the body

but when I did that the pictures didn't appear
all the text and formats and tables are copied but not the pictures
and the alignment options also

object html = Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf("\\")) + "\\tempHtml.html";
            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
            wordApp.Activedocument.SaveAs(ref html, ref format,
                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);
            System.Threading.Thread.Sleep(3000);
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("ahmedn32004@yahoo.com", "Ahmedn1");
            mail.To.Add(new MailAddress("ahmedn32004@yahoo.com", "Ahmedn1 Receiver"));
            mail.Subject = "Test";
            mail.IsBodyHtml = true;
            StreamReader sr = new StreamReader((string)html);
            mail.Body = sr.ReadToEnd();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1