Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.IO;
- using BookmarksConv.Properties;
- using DevExpress.XtraGrid.Columns;
- using Word = Microsoft.Office.Interop.Word;
- using Excel = Microsoft.Office.Interop.Excel;
- using System.Threading;
- using System.Globalization;
- namespace BookmarksConv
- {
- public partial class Form1 : Form
- {
- private readonly List<BookmarkSwitches> _bookmarkSwitches;
- private readonly List<WordFiles> _wordFiles;
- public Form1()
- {
- InitializeComponent();
- _bookmarkSwitches = new List<BookmarkSwitches>();
- _wordFiles = new List<WordFiles>();
- bindingSource1.RaiseListChangedEvents = true;
- bindingSource2.RaiseListChangedEvents = true;
- }
- private bool LoadXcelBookmarks(string filename)
- {
- Cursor = Cursors.WaitCursor;
- progressBarControl1.Properties.Maximum = (int)spinEdit2.Value;
- progressBarControl1.Position = (int)spinEdit1.Value;
- bool res = false;
- if (File.Exists(filename))
- {
- System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
- Object empty = System.Reflection.Missing.Value;
- Excel.Application excelApp = new Excel.ApplicationClass();
- try
- {
- Excel.Workbook excelWorkBook = excelApp.Workbooks.Open(filename,
- 0, true, 5,
- "", "", true, Excel.XlPlatform.xlWindows,
- "\t", false, false,
- 0, true, true, empty);
- var excelWorkSheet = (Excel.Worksheet)excelWorkBook.Worksheets.get_Item(1);
- bindingSource1.DataSource = null;
- _bookmarkSwitches.Clear();
- for (var i = (int)spinEdit1.Value; i < (int)spinEdit2.Value; i++)
- {
- var range1 = (Excel.Range)excelWorkSheet.Cells[i, textEdit2.Text];
- var range2 = (Excel.Range)excelWorkSheet.Cells[i, textEdit1.Text];
- var b = new BookmarkSwitches(range1.Value2 != null ? range1.Value2.ToString() : "",
- range2.Value2 != null ? range2.Value2.ToString() : "");
- _bookmarkSwitches.Add(b);
- progressBarControl1.Position += 1;
- }
- bindingSource1.DataSource = _bookmarkSwitches;
- res = true;
- }
- finally
- {
- excelApp.Quit();
- Cursor = Cursors.Default;
- }
- }
- return res;
- }
- private bool LoadWordFiles(string path, string extension)
- {
- bool res = false;
- Cursor = Cursors.WaitCursor;
- try
- {
- var dirInfo = new DirectoryInfo(path);
- FileInfo[] fileInfo = dirInfo.GetFiles("*" + extension);
- foreach (FileInfo file in fileInfo)
- {
- if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden ||
- (file.Attributes & FileAttributes.System) == FileAttributes.System)
- continue;
- if (String.Compare(file.Extension, extension) == 0)
- _wordFiles.Add(new WordFiles(file.FullName));
- }
- DirectoryInfo[] dirs = dirInfo.GetDirectories();
- foreach (DirectoryInfo dir in dirs)
- {
- if (!LoadWordFiles(dir.FullName, extension))
- break;
- }
- res = true;
- Cursor = Cursors.Default;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- Cursor = Cursors.Default;
- }
- return res;
- }
- private static void DoBookmark(Word.Document doc, BookmarkSwitches switches, BookmarkProcessingType processingType)
- {
- switch (processingType)
- {
- case BookmarkProcessingType.Zamena:
- Object b = switches.Bookmark;
- Word.Range range = doc.Bookmarks.get_Item(ref b).Range;
- range.InsertAfter(switches.Zamena);
- doc.Bookmarks.get_Item(ref b).Delete();
- break;
- case BookmarkProcessingType.Preimenovanje:
- Object p = switches.Bookmark;
- Word.Bookmark bb = doc.Bookmarks.get_Item(ref p);
- Object rangep = bb.Range;
- bb.Select();
- doc.Bookmarks.Add(switches.Zamena, ref rangep);
- bb.Delete();
- break;
- case BookmarkProcessingType.Tagovanje:
- Word.Find find = doc.Content.Find;
- Object findText = switches.Bookmark;
- Object findReplacementText = switches.Zamena;
- Object findForward = true;
- Object findWrap = Word.WdFindWrap.wdFindContinue;
- Object findFormat = false;
- Object findMatchCase = false;
- Object findMatchWholeWord = true;
- Object findMatchWildcards = false;
- Object findMatchSoundsLike = false;
- Object findMatchAllWordForms = false;
- Object empty = System.Reflection.Missing.Value;
- Object replace = true;
- find.Execute(ref findText, ref findMatchCase, ref findMatchWholeWord,
- ref findMatchWildcards, ref findMatchSoundsLike, ref findMatchAllWordForms,
- ref findForward, ref findWrap,
- ref findFormat, ref findReplacementText, ref replace, ref empty,
- ref empty, ref empty, ref empty
- );
- break;
- }
- }
- private bool ProcessWordFiles(BookmarkProcessingType processingType)
- {
- bool res = false;
- Object empty = System.Reflection.Missing.Value;
- Cursor = Cursors.WaitCursor;
- Word.Application wordApp = new Word.ApplicationClass();
- progressBarControl1.Position = 1;
- progressBarControl1.Properties.Maximum = _wordFiles.Count;
- int row = 0;
- //GridColumn col = null;
- try
- {
- foreach (WordFiles wf in _wordFiles)
- {
- Object docName = wf.FullPath;
- wordApp.Documents.Open(ref docName,
- ref empty, ref empty, ref empty, ref empty, ref empty,
- ref empty, ref empty, ref empty, ref empty,
- ref empty, ref empty, ref empty, ref empty, ref empty, ref empty);
- Word.Document doc = wordApp.ActiveDocument;
- try
- {
- foreach (BookmarkSwitches bs in _bookmarkSwitches)
- {
- if (!String.IsNullOrEmpty(bs.Bookmark) && !String.IsNullOrEmpty(bs.Zamena))
- {
- if (doc.Bookmarks.Exists(bs.Bookmark) ||
- processingType == BookmarkProcessingType.Tagovanje)
- DoBookmark(doc, bs, processingType);
- }
- }
- doc.Save();
- }
- finally
- {
- doc.Close(ref empty, ref empty, ref empty);
- }
- switch (processingType)
- {
- case BookmarkProcessingType.Zamena:
- wf.Zamena = true;
- //col = gridView2.Columns[1];
- break;
- case BookmarkProcessingType.Preimenovanje:
- wf.Preimenovanje = true;
- //col = gridView2.Columns[2];
- break;
- case BookmarkProcessingType.Tagovanje:
- wf.Tagovanje = true;
- //col = gridView2.Columns[3];
- break;
- }
- progressBarControl1.Position += 1;
- //gridView2.RefreshRowCell(row, col);
- gridView2.FocusedRowHandle = row;
- row++;
- }
- res = true;
- }
- finally
- {
- //wordApp.Quit(ref empty, ref empty, ref empty);
- wordApp.Quit();
- Cursor = Cursors.Default;
- }
- return res;
- }
- private void SimpleButton1Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- if (!LoadXcelBookmarks(openFileDialog1.FileName))
- {
- MessageBox.Show(Resources.LoadXcelFailed, Resources.ErrorCaption, MessageBoxButtons.OK,
- MessageBoxIcon.Error);
- }
- }
- }
- private void SimpleButton5Click(object sender, EventArgs e)
- {
- if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
- {
- _wordFiles.Clear();
- bindingSource2.DataSource = null;
- LoadWordFiles(folderBrowserDialog1.SelectedPath, comboBoxEdit1.Text);
- bindingSource2.DataSource = _wordFiles;
- }
- }
- private bool CheckCanProcess()
- {
- bool res = _wordFiles.Count > 0 && _bookmarkSwitches.Count > 0;
- if (!res)
- MessageBox.Show(Resources.CanNotProcessTxt,
- Resources.WarningCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return res;
- }
- private bool IsDone(BookmarkProcessingType type)
- {
- bool res = false;
- if (_wordFiles.Count > 0)
- {
- switch (type)
- {
- case BookmarkProcessingType.Zamena:
- res = _wordFiles[0].Zamena;
- break;
- case BookmarkProcessingType.Preimenovanje:
- res = _wordFiles[0].Preimenovanje;
- break;
- case BookmarkProcessingType.Tagovanje:
- res = _wordFiles[0].Tagovanje;
- break;
- }
- }
- return res;
- }
- private void SimpleButton2Click(object sender, EventArgs e)
- {
- if (IsDone(BookmarkProcessingType.Zamena))
- {
- MessageBox.Show(Resources.ProcessingDoneTxt, Resources.WarningCaption, MessageBoxButtons.OK,
- MessageBoxIcon.Warning);
- return;
- }
- if (CheckCanProcess())
- {
- if (ProcessWordFiles(BookmarkProcessingType.Zamena))
- {
- MessageBox.Show(Resources.ReplaceFinishedOK, Resources.ReplaceFinishedOKCaption, MessageBoxButtons.OK,
- MessageBoxIcon.Information);
- }
- }
- }
- private void SimpleButton3Click(object sender, EventArgs e)
- {
- if (IsDone(BookmarkProcessingType.Preimenovanje))
- {
- MessageBox.Show(Resources.ProcessingDoneTxt, Resources.WarningCaption, MessageBoxButtons.OK,
- MessageBoxIcon.Warning);
- return;
- }
- if (CheckCanProcess())
- {
- if (ProcessWordFiles(BookmarkProcessingType.Preimenovanje))
- {
- MessageBox.Show(Resources.RenameFinishedOk, Resources.RenameFinishedOkCaption,
- MessageBoxButtons.OK,
- MessageBoxIcon.Information);
- }
- }
- }
- private void SimpleButton4Click(object sender, EventArgs e)
- {
- if (IsDone(BookmarkProcessingType.Tagovanje))
- {
- MessageBox.Show(Resources.ProcessingDoneTxt, Resources.WarningCaption, MessageBoxButtons.OK,
- MessageBoxIcon.Warning);
- return;
- }
- if (CheckCanProcess())
- {
- if (ProcessWordFiles(BookmarkProcessingType.Tagovanje))
- {
- MessageBox.Show(Resources.TagsFinishedOK, Resources.TagFinishedOKCaption, MessageBoxButtons.OK,
- MessageBoxIcon.Information);
- }
- }
- }
- }
- internal enum BookmarkProcessingType
- {
- Zamena,
- Preimenovanje,
- Tagovanje,
- }
- public class BookmarkSwitches
- {
- public string Bookmark { get; set; }
- public string Zamena { get; set; }
- public BookmarkSwitches(string bookmark, string zamena)
- {
- Bookmark = bookmark;
- Zamena = zamena;
- }
- }
- public class WordFiles : INotifyPropertyChanged
- {
- private bool _zamena;
- public string FullPath { get; set; }
- public string FileName { get; set; }
- public bool Zamena
- {
- get { return _zamena; }
- set
- {
- if (value != _zamena)
- {
- _zamena = value;
- NotifyChanged("Zamena");
- }
- }
- }
- public bool Preimenovanje { get; set; }
- public bool Tagovanje { get; set; }
- public WordFiles(string fullPath)
- {
- FullPath = fullPath;
- FileName = Path.GetFileName(FullPath);
- Zamena = false;
- Preimenovanje = false;
- Tagovanje = false;
- }
- public event PropertyChangedEventHandler PropertyChanged;
- private void NotifyChanged(string info)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(info));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement