none
modify word and send document RRS feed

  • Question

  • i can read a word document like this:

    FileStream fs = new FileStream(@"C:\\test.docx", FileMode.Open, FileAccess.Read, FileShare.Read);             
    Package pack = Package.Open(fs);
    Uri uri = new Uri("/word/document.xml", UriKind.Relative);
    PackagePart part = pack.GetPart(uri);
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(part.GetStream(FileMode.Open, FileAccess.Read));

    is it olso possible that i can make changes in this document.

    example: in this document you can find a customer name, this name i would like to change.

     

    when all changes are done, can i send this document trough e-mail?

    and how?

    regards

    Wednesday, March 30, 2011 11:11 AM

Answers

All replies

  • Hi Kimvlas,

    Thank you for posting.

    It is possible to make changes in the word document and then send it through E-mail. We can get the customer name node first and change it's value.

    After modifying the document, we can send the document via E-mail. If you want to programmatically send it, you can use System.Net.Mail or Outlook PIA. Below are the articles which contain the code snippet:

    http://timzaman.wordpress.com/2011/03/15/send-gmail-with-c-c-sharp/

    http://softarchitect.wordpress.com/2010/09/18/software-architecture-to-send-email-with-attachments-via-c-4-0-office-interop-outlook/

    Hope these can help you and just feel free to follow up after you have tried.

    Best Regards,

     


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by kimvlas Wednesday, April 6, 2011 2:09 PM
    Thursday, March 31, 2011 3:11 AM
  • thankx for your replay.

    i can send the document when i save it to a directory.

    is it possible that is read a word document.

    i make some changes and than, place the document in the filestream en send it with e-mail.

    without save the new document?

    the code that i use at the moment you will see below.

    File.Copy(@"C:\Bonnr.docx", @"C:\" + BonId + ".docx", true);

    Microsoft.Office.Interop.Word.Document aDoc = null;
                        object filename = @"C:\" + BonId + ".docx";
                        if (File.Exists((string)filename))
                        {
                            object readOnly = false;
                            object isVisible = false;
                            wordApp.Visible = false;
                            aDoc = wordApp.Documents.Open(ref filename, ref missing,
                ref readOnly, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref isVisible, ref missing, ref missing,
                ref missing, ref missing);
                            aDoc.Activate();
                            this.FindAndReplace(wordApp, "<customer>", "Flip");
                            aDoc.Save();
                            aDoc.Close(ref missing, ref missing, ref missing);                       
                        }
                        else

                            MessageBox.Show("File does not exist.", "No File", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        killprocess("winword");

     

    FileStream fileStream = new FileStream(@"C:\" + BonId + ".docx", FileMode.Open, FileAccess.Read);
                    fileStream.Close();
                    try
                    {
                        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("smtp.server");

                        mail.From = new MailAddress("test@mail.com");
                        mail.To.Add("test@mail.com");
                        mail.Subject = "Test Mail";
                        mail.Body = "This is for testing SMTP mail";
                        mail.Attachments.Add(new Attachment(@"C:\" + BonId + ".docx"));
                        SmtpServer.Send(mail);
                        MessageBox.Show("mail Send");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }

     

    Thursday, March 31, 2011 9:17 AM
  • Hi Kimvlas,

    I think you can send an opened word document via System.Net.Mail. In addition, I have found that word PIA contains its own built-in method which can send Email, just use Document.SendEmail() can make it.

    Hope this can help you and just feel free to follow up.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Bruce Song Friday, April 1, 2011 8:19 AM
    Friday, April 1, 2011 8:16 AM
  • Hi Kimvlas,

    Have you resolved your issue yet, and do the suggestions help you? If you still have any concern on the thread,just feel free to follow up. We are happy to help with you.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 6, 2011 7:04 AM
  • Hi,

    i was away for a few days.

    i have a new issieu, on a different window i want to print the document.

    i don't want to save the document, because i want to print it ownly.

    i will remove the "save" code, and before the close i want toe print my "aDoc":

    aDoc.Save();
    aDoc.Close(ref missing, ref missing, ref missing);    

    i have some code that prints a document.

    wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
                    object filename = "c: \myFile.docx";
                    object missingValue = Type.Type.Missing;
                    Word.Document document = wordApp.Documents.OpenOld(ref filename,
                    ref missing, ref missing,
                    ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing);
                    wordApp.ActivePrinter = "";
                    object myTrue = true; // Print in background
                    object myFalse = false;
                    wordApp.ActiveDocument.PrintOutOld(ref myTrue,
                    ref myFalse, ref missing, ref missing, ref missing,
                        missing, ref missing,
                    ref missing, ref missing, ref missing, ref myFalse,
                        ref missing, ref missing, ref missing);
                    document.Close(ref missing, ref missing, ref missing);
                    while (wordApp.BackgroundPrintingStatus > 0)
                    {
                        System.Threading.Thread.Sleep(250);
                    }
                    wordApp.Quit(ref missing, ref missing, ref missing);

     

    i'm missing something, in my code i will print a exsisting document.

    how can i change this and use the change document in memory?

    or is this not possible?

    regards,

     

    Wednesday, April 6, 2011 2:17 PM
  • Hi Kimvlas,

    In this thread, we are mainly discussing about modifying and sending word document. Your new question is not directly related to the original issue, it would be better if you open up a new thread for the new question. In this way, our discussion here will not deviate too much from the original issue. This will make answer searching in the forum easier and be beneficial to other community members as well.

    Thank you for your understanding.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 12, 2011 2:21 AM