using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Word = Microsoft.Office.Interop.Word; using System.IO; using System.Diagnostics; namespace Word_Page_Counter_2 { public partial class frm_WordPageCounter : Form { static Word.ApplicationClass _wordApp = new Word.ApplicationClass(); public frm_WordPageCounter() { InitializeComponent(); Init(); } /// /// Initializes OpenFileDialog /// private void Init() { openFileDialog1.Multiselect = true; openFileDialog1.FileName = ""; } /// /// Add the words docs to the DataGridView /// /// /// private void btn_AddDocs_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { foreach (string fileName in openFileDialog1.FileNames) { int n = dgv_FilePaths.Rows.Add(); dgv_FilePaths.Rows[n].Cells[0].Value = fileName; } } } /// /// Run StartPageCounter method /// /// /// private void btn_CountPages_Click(object sender, EventArgs e) { StartPageCounter(); } /// /// Opens Word with the doubleclicked Document /// /// /// private void dgv_FilePaths_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) { string path = dgv_FilePaths.Rows[e.RowIndex].Cells[0].Value as string; if (!string.IsNullOrEmpty(path)) { FileInfo openFile = new FileInfo(path); if (openFile.Exists) { Process.Start(openFile.FullName); } } } /// /// Calculates Page number for selected cells/rows /// /// /// private void dgv_FilePaths_SelectionChanged(object sender, EventArgs e) { int SelectedPageNumber = 0; foreach (DataGridViewCell cell in dgv_FilePaths.SelectedCells) { DataGridViewRow row = cell.OwningRow; string number = row.Cells[1].Value as string; int currentPageNumber = 0; if (!String.IsNullOrEmpty(number)) { bool isNumber = Int32.TryParse(number,out currentPageNumber); } SelectedPageNumber += currentPageNumber; } CalculateTotalNumber(); tbx_SelectedPageNumber.Text = SelectedPageNumber.ToString(); } /// /// Checks path and page number information of each row. Calls GetPageAmount for every valid row /// private void StartPageCounter() { foreach (DataGridViewRow row in dgv_FilePaths.Rows) { string pathCell = row.Cells[0].Value as String; string pagesCell = row.Cells[1].Value as String; if (!String.IsNullOrEmpty(pathCell) && String.IsNullOrEmpty(pagesCell)) { string path = row.Cells[0].Value.ToString(); row.Cells[1].Value = GetPageNumberByFilename(path); } } CalculateTotalNumber(); } /// /// http://www.codeproject.com/KB/cs/mswordcount.aspx with some edits /// /// /// private static string GetPageNumberByFilename(string name) { try { object missing = System.Reflection.Missing.Value; object readOnly = false; object isVisible = true; object fileName = name; Word.Document 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); Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages; int num = aDoc.ComputeStatistics(stat, ref missing); object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; object originalFormat = Word.WdOriginalFormat.wdOriginalDocumentFormat; object routeDocument = true; aDoc.Close(ref saveChanges, ref originalFormat, ref routeDocument); return num.ToString(); } catch (Exception ex) { return "Error: " + ex.Message; } } /// /// Calculates total number and writes result to texbox /// /// private void CalculateTotalNumber() { int TotalPageNumber = 0; foreach (DataGridViewRow row in dgv_FilePaths.Rows) { string number = row.Cells[1].Value as string; int currentPageNumber = 0; if (!String.IsNullOrEmpty(number)) { bool isNumber = Int32.TryParse(number, out currentPageNumber); } TotalPageNumber += currentPageNumber; } tbx_TotalNumber.Text = TotalPageNumber.ToString(); } private void llbl_Visit_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start(@"http://techcrawler.riedme.de/?p=2256"); } } }