/// <summary> /// This method will either accept or reject tracked changes. /// </summary> private void HandleTrackedChanges() { if (m_handleTrackedChanges && m_wordDoc.Revisions.Count > 0) { if (m_acceptTrackedChanges) { m_wordDoc.AcceptAllRevisions(); } else { m_wordDoc.RejectAllRevisions(); } } }
private void button2_Click(object sender, EventArgs e) { if (path == "") { MessageBox.Show("Please select the root folder with the browse button."); } else { button2.Text = "Running..."; string[] wordFiles = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Where(s => s.EndsWith("Instructor.doc") && !s.StartsWith("~") || s.EndsWith("Instructor.docx") && !s.StartsWith("~") || s.EndsWith("Instructor.docm") && !s.StartsWith("~")).ToArray(); int fileAmount = wordFiles.Length; progressBar1.Maximum = fileAmount; foreach (string file in wordFiles) { try { object missing = System.Reflection.Missing.Value; Word.Application wordApp = new Word.ApplicationClass(); Word.Document aDoc = null; wordApp.Visible = false; wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; wordApp.Options.WarnBeforeSavingPrintingSendingMarkup = false; aDoc = wordApp.Documents.Open(file /*filename*/, false /*convert file prompt*/, false /*readonly*/, false /*recent files*/, ref missing /*read pass*/, ref missing /*read template pass*/, true /*reopen*/, ref missing /*write pass*/, ref missing /*write template pass*/, ref missing /*format*/, ref missing /*encoding*/, false /*visible client*/, ref missing /*repair*/, ref missing /*direction*/, ref missing /*NoEncodingDialog*/, ref missing /*XMLTransform*/); aDoc = wordApp.Documents.Add(file /*template*/, ref missing /*new template*/, ref missing /*doc type*/, false /*visible*/); aDoc.Activate(); aDoc.AcceptAllRevisions(); aDoc.TrackRevisions = false; if (aDoc.Comments.Count > 0) { aDoc.DeleteAllComments(); } string instructorPDF = file; if (file.EndsWith(".doc")) { instructorPDF = instructorPDF.Replace(".doc", ".pdf"); } else if (instructorPDF.EndsWith(".docm")) { instructorPDF = instructorPDF.Replace(".docm", ".pdf"); } else { instructorPDF = instructorPDF.Replace(".docx", ".pdf"); } //Console.WriteLine("Trying to save as " + instructorPDF); aDoc.SaveAs(instructorPDF, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF); // START OF VBA MACRO FROM NAV CANADA foreach (Word.Style style in aDoc.Styles) { if (style.NameLocal.Length > 4) { if (style.NameLocal.Substring(0, 5) == "Instr") { //Console.WriteLine("Removing all of " + style.NameLocal); wordApp.Selection.Find.set_Style(style); wordApp.Selection.Find.Execute(); while (wordApp.Selection.Find.Found) { wordApp.Selection.Delete(); wordApp.Selection.Find.Execute(); } } } wordApp.Selection.HomeKey(Unit: Word.WdUnits.wdStory); } Word.Find findObject = wordApp.Application.Selection.Find; findObject.ClearFormatting(); findObject.Text = "Instructor Manual"; findObject.Replacement.ClearFormatting(); findObject.Replacement.Text = "Student Manual"; object replaceAll = Word.WdReplace.wdReplaceAll; findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing); // END OF VBA MACRO FROM NAV CANADA // Update the fields (table of contents and such) foreach (Microsoft.Office.Interop.Word.Field field in aDoc.Fields) { field.Update(); } // End of updating fields // Saving docx string newFile = file.Replace("Instructor", "Student"); if (newFile.EndsWith(".docm")) { newFile = newFile.Replace(".docm", ".docx"); } else if (newFile.EndsWith(".doc")) { newFile = newFile.Replace(".doc", ".docx"); } // Saving PDF string newPDF = ""; if (newFile.EndsWith(".doc")) { newPDF = newFile.Replace(".doc", ".pdf"); } else if (newFile.EndsWith(".docm")) { newPDF = newFile.Replace(".docm", ".pdf"); } else { newPDF = newFile.Replace(".docx", ".pdf"); } //Console.WriteLine("Trying to save as " + newFile); //aDoc.SaveAs(newFile, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault); //Console.WriteLine("Trying to save as " + newPDF); aDoc.SaveAs(newPDF, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF); dropboxPDFs.Add(newPDF); } catch (Exception error) { textBox2.Text += "Couldn't open " + file + " " + e + "\r\n" + error; } progressBar1.Value++; progressBar1.Update(); } Process[] wordClients = Process.GetProcessesByName("WINWORD"); foreach (Process p in wordClients) { p.Kill(); } if (textBox2.Text == "") { if (checkBox1.Checked) { MessageBox.Show("All completed successfully. Now uploading to dropbox."); } else { MessageBox.Show("All completed successfully."); } } else { MessageBox.Show("Complete. Please verify the template of files listed above."); } if (checkBox1.Checked && token != "") { button2.Text = "Uploading..."; progressBar1.Value = 0; progressBar1.Maximum = dropboxPDFs.Count; foreach (string s in dropboxPDFs) { DropboxClient dropboxClient = new DropboxClient(token); string folder = "/" + comboBox2.Text; string[] filenamesplit = s.Split('\\'); string filename = filenamesplit[filenamesplit.Length - 1]; var content = System.IO.File.ReadAllBytes(s); try { Form1.UploadAsync(dropboxClient, folder, filename, content).Wait(); progressBar1.Value++; progressBar1.Update(); } catch { textBox2.Text += "Could not upload. " + filename + "\r\n"; } } MessageBox.Show("Completed uploading files."); } else if (checkBox1.Checked && token == "") { MessageBox.Show("Please put your token in a file named token.txt and restart the program."); } button2.Text = "Start"; } }