Exemplo n.º 1
0
        private void updateDocument(ref Word.Document doc)
        {
            try
            {
                doc.Fields.Update();

                foreach (Word.Section section in doc.Sections)
                {
                    doc.Fields.Update();                           // update each section

                    Word.HeadersFooters headers = section.Headers; //Get all headers
                    foreach (Word.HeaderFooter header in headers)
                    {
                        Word.Fields fields = header.Range.Fields;
                        foreach (Word.Field field in fields)
                        {
                            field.Update();  // update all fields in headers
                        }
                    }

                    Word.HeadersFooters footers = section.Footers;  //Get all footers
                    foreach (Word.HeaderFooter footer in footers)
                    {
                        Word.Fields fields = footer.Range.Fields;
                        foreach (Word.Field field in fields)
                        {
                            field.Update();  //update all fields in footers
                        }
                    }
                }

                foreach (Word.TableOfContents tableOfContents in doc.TablesOfContents)
                {
                    tableOfContents.Update();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (btnNext.Text == "Next")
            {
                LoadMergeData();
                tabControlAdvancedMerge.SelectedTab = tabControlAdvancedMerge.TabPages[1];
                btnNext.Text = "Merge";
            }
            else if (btnNext.Text == "Merge" & btnNext.Enabled)
            {
                Word.Fields wordFields = Globals.ThisAddIn.Application.ActiveDocument.Fields;
                if (wordFields.Count > 0 && dataTableMergeData != null)
                {
                    if (Globals.ThisAddIn.Application.ActiveDocument.Saved)
                    {
                        MessageBox.Show("File is saved");
                    }
                    else
                    {
                        Globals.ThisAddIn.Application.ActiveDocument.Save();
                    }
                    string keyField = ReadDocumentProperty("KeyField");
                    Outlook.Application outlookApp      = new Outlook.Application();
                    Outlook.Accounts    outlookAccounts = outlookApp.Session.Accounts;
                    Outlook.MailItem    mailItem        = outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

                    Word.Document wordDocument = Globals.ThisAddIn.Application.ActiveDocument;

                    var grouped = from table in dataTableMergeData.AsEnumerable()
                                  group table by new { keyCol = table[textBoxKeyField.Text] } into grp
                        select new
                    {
                        Value        = grp.Key,
                        ColumnValues = grp
                    };
                    foreach (var key in grouped)
                    {
                        Word.Application wordApplication = new Word.Application();
                        wordApplication.ShowAnimation = false;
                        wordApplication.Visible       = false;
                        object        missing     = System.Reflection.Missing.Value;
                        Word.Document newDocument = wordApplication.Documents.Open(wordDocument.Path + @"\" + wordDocument.Name, ReadOnly: true);

                        Word.MailMerge mailMerge = newDocument.MailMerge;


                        DataRow[] selectedRows = dataTableMergeData.Select(textBoxKeyField.Text + " ='" + key.Value.keyCol.ToString() + "'");

                        foreach (Word.MailMergeField mailMergeField in mailMerge.Fields)
                        {
                            if (mailMergeField.Code.Text.IndexOf(" MERGEFIELD " + "FirstName" + " ") > -1)
                            {
                                mailMergeField.Select();

                                mailMerge.Application.Selection.TypeText(selectedRows[1][2].ToString());
                            }
                            else if (mailMergeField.Code.Text.IndexOf(" MERGEFIELD " + "Product_name" + " ") > -1)
                            {
                                mailMergeField.Select();
                                mailMerge.Application.Selection.TypeText(selectedRows[1][4].ToString());
                                for (int i = 0; i < selectedRows.Length; i++)
                                {
                                    if (i < selectedRows.Length - 1)


                                    {
                                        for (int tableIndex = 0; tableIndex < newDocument.Tables.Count; tableIndex++)
                                        {
                                        }

/*                                        foreach  (Word.Table table in wordDocument.Tables)
 *                                      {
 *                                          foreach (Word.Row row in table.Rows)
 *                                          {
 *                                              foreach (Word.Cell cell in row.Cells)
 *                                              {
 *
 *                                              }
 *                                          }
 *
 *                                      }*/
                                        mailMerge.Application.Selection.InsertAfter("\r\n" + selectedRows[i + 1][4].ToString());
                                    }
                                }
                            }
                        }

                        mailItem.Subject = "Test";
                        mailItem.To      = "*****@*****.**";
                        mailItem.Body    = newDocument.Content.Text;
                        mailItem.Send();
                        newDocument.Close(false);
                        wordApplication.Quit(false);
                        Marshal.ReleaseComObject(newDocument);
                        Marshal.ReleaseComObject(wordApplication);
                    }
                }
            }
        }