//// This function demonstrates that we can add a comment to (nearly) every paragraph if we like. public static void AddToEveryPara(Document doc) { ConsoleC.WriteLine(ConsoleColor.White, "Trying to write a comment on all the paragraphs!"); //// Load the default instance of Document class. // Document doc = LoadDocument.Default(); for (int i = 1; i < doc.Paragraphs.Count; i++) { // Add a comment. try { object text = "This is a comment on Paragraph " + i + "."; doc.Comments.Add(doc.Paragraphs[i].Range, ref text); ConsoleC.WriteLine(ConsoleColor.Green, "Added a comment on Paragraph " + i + "!"); } catch (Exception ex) { ConsoleC.WriteLine(ConsoleColor.Red, "Failed to add a comment to Paragraph " + i + " — " + ex.ToString()); } } //Save to a new file. doc.SaveAs2(Filepath.Full().Replace(".docx", "_2.docx")); }
public static Word.Document Default() { try { object fileName = Filepath.Full(); Word.Application wordApp = new Word.Application { Visible = true }; Document aDoc = wordApp.Documents.Open(ref fileName, ReadOnly: false, Visible: true); aDoc.Activate(); Word.Application wordObject = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application"); Application word = (Microsoft.Office.Interop.Word.Application)wordObject; word.Visible = true; word.ScreenUpdating = false; return(word.ActiveDocument); } catch { throw new Exception("Error loading default document!"); } }
static void Main(string[] args) { //// Load a document we can play with. Document doc = LoadDocument.Default(); DocumentCheckSpelling.DocCheckSpelling(doc); // InlineLists.RunTests(); InlineLists.DetectAll(doc); // Comments.AddToEveryPara(doc); Headers.DetectHeaders(doc); Headers.DetectLineSpacingAfterBullets(doc); Language.LanguageChecker(doc); //// Save to a new file. doc.SaveAs2(Filepath.Full().Replace(".docx", "_2.docx")); //// Keep the console open even when the program has finished. ConsoleC.WriteLine(ConsoleColor.Green, "\nThe program has finished."); Console.ReadLine(); }
// private static InteropManager im = new InteropManager(Filepath.Folder(), Filepath.FileOnly()); // private static Word.Application wordDoc = im.getWord(); // private static Document doc = wordDoc.Application.ActiveDocument; // Document wordDoc = im.getWord(); //public Headers() //{ //} public static void DetectLineSpacingAfterBullets(Document doc) { ConsoleC.WriteLine(ConsoleColor.White, "Checking every bullet for 6pt line-spacing between indentation levels..."); int badSpacingCount = 0; int badSpacingFailCount = 0; // foreach (Paragraph paragraph in wordDoc.Application.ActiveDocument.Paragraphs) for (int i = 1; i < doc.Paragraphs.Count; i++) { Paragraph paragraph = doc.Paragraphs[i]; Paragraph paragraph2 = doc.Paragraphs[i + 1]; string listString = paragraph.Range.ListFormat.ListString; string listString2 = paragraph2.Range.ListFormat.ListString; if (listString != "" && listString2 != "" && paragraph.Format.LeftIndent != paragraph2.Format.LeftIndent) { Style style = paragraph.get_Style() as Style; string styleName = style.NameLocal; if (styleName != "Heading 1" && styleName != "Heading 2" && styleName != "Heading 3" && styleName != "Heading 4") { if (paragraph.Format.SpaceAfter == 6) { //Console.WriteLine(paragraph.Range.Text); //Console.WriteLine("That's the correct spacing"); } else { ConsoleC.WriteLine(ConsoleColor.Blue, paragraph.Range.Text); //Console.WriteLine("This paragraph's left indent is different to the next paragraph's left indent."); ConsoleC.WriteLine(ConsoleColor.Yellow, "Detected line-spacing that should be 6pt but isn’t."); badSpacingCount++; try { paragraph.Format.SpaceAfter = 6; ConsoleC.WriteLine(ConsoleColor.Green, "Spacing has been changed to 6pt."); Comments.Add(doc, paragraph, "Line-spacing has been changed to 6pt."); } catch { ConsoleC.WriteLine(ConsoleColor.Red, "Failed to automatically change line-spacing to 6pt."); Comments.Add(doc, paragraph, "Line-spacing needs to change to 6pt."); badSpacingFailCount++; } } } else { //ConsoleC.WriteLine(ConsoleColor.Green, "This paragraph is a heading."); } } } //// Give feedback having gone through the document. ConsoleC.WriteLine(ConsoleColor.White, "Finished checking every bullet."); ConsoleC.WriteLine( (badSpacingCount == 0 ? ConsoleColor.Green : ConsoleColor.Yellow), "There were " + badSpacingCount + " instances where the spacing after a bullet " + "needed to be changed to 6pt before a bullet of a different indentation." ); ConsoleC.WriteLine( (badSpacingFailCount == 0 ? ConsoleColor.Green : ConsoleColor.Red), "There are " + badSpacingFailCount + " instances where this could not be corrected automatically." ); //// Save to a new file. doc.SaveAs2(Filepath.Full().Replace(".docx", "_2.docx")); }