static void Main(string[] args) { string[] columns = { "A", "B", "C", "D", "E", "F", "G", "H", "I" }; Console.Clear(); Console.WriteLine("Please type the full address to the word document or folder with multiply word documents(docx) you want to exctract comments from:"); string path = Console.ReadLine(); Console.WriteLine("Thank you! Now Please provide the full puth to the Excel spreadsheet you want to create and put codes in to:"); string excelPath = Console.ReadLine(); Console.WriteLine("Do you want to Clean File from Content Controls: yes/no"); string contentClean = Console.ReadLine(); Console.WriteLine("Thank you! \n\r Input from: " + path + "\n\r Output: " + excelPath); FilesProcessing files = new FilesProcessing(path); string[] titles = { "Id", "Comment", "Text" }; if (contentClean == "yes") { files.fileEntries.ForEach(delegate(TranscriptFile transcript) { transcript.ConsolePrint(); WordDocument document = new WordDocument(transcript.path); document.DeleteContentControls(); document.SaveAndClose(); }); } ExcelDocument excelDocument = new ExcelDocument(excelPath, titles); excelDocument.Create(); files.fileEntries.ForEach(delegate(TranscriptFile transcript) { transcript.ConsolePrint(); WordDocument document = new WordDocument(transcript.path); RecordsList records = document.GetCommentsWithText(); //records.ConsolePrint(); string[][] data = records.Transform(titles); columns = columns.SubArray(0, 3); //excelDocument.InsertWorksheet(transcript.title); excelDocument.InsertText(transcript.title, columns, data); }); }
public RecordsList GetCommentsWithText() { RecordsList records = new RecordsList(); WordprocessingCommentsPart commentsPart = wordDocument.MainDocumentPart.WordprocessingCommentsPart; if (commentsPart != null && commentsPart.Comments != null) { foreach (Comment comment in commentsPart.Comments.Elements <Comment>()) { OpenXmlElement rangeStart = wordDocument.MainDocumentPart.Document.Descendants <CommentRangeStart>().Where(c => c.Id == comment.Id).FirstOrDefault(); //bool breakLoop = false; //rangeStart = rangeStart.Parent; rangeStart = rangeStart.NextSibling(); string commentText = ""; while (!(rangeStart is CommentRangeEnd)) { try { if (!string.IsNullOrWhiteSpace(rangeStart.InnerText)) { commentText += rangeStart.InnerText; } rangeStart = rangeStart.NextSibling(); } catch (NullReferenceException ex) { Console.WriteLine(ex.Message); Console.WriteLine("NullReference Exception on " + comment.InnerText + " with highlited text: " + commentText); commentText += " !!!ERROR WHILE EXTRACTING THIS TEXT!!!"; break; } } Record record = new Record(comment.Id, comment.InnerText, commentText); records.Add(record); } } else { return(records); } return(records); }