Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using BookmarksConv.Properties;
  7. using DevExpress.XtraGrid.Columns;
  8. using Word = Microsoft.Office.Interop.Word;
  9. using Excel = Microsoft.Office.Interop.Excel;
  10. using System.Threading;
  11. using System.Globalization;
  12.  
  13.  
  14. namespace BookmarksConv
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         private readonly List<BookmarkSwitches> _bookmarkSwitches;
  19.         private readonly List<WordFiles> _wordFiles;
  20.  
  21.         public Form1()
  22.         {
  23.  
  24.             InitializeComponent();
  25.  
  26.             _bookmarkSwitches = new List<BookmarkSwitches>();
  27.             _wordFiles = new List<WordFiles>();
  28.  
  29.             bindingSource1.RaiseListChangedEvents = true;
  30.             bindingSource2.RaiseListChangedEvents = true;
  31.         }
  32.  
  33.         private bool LoadXcelBookmarks(string filename)
  34.         {
  35.             Cursor = Cursors.WaitCursor;
  36.             progressBarControl1.Properties.Maximum = (int)spinEdit2.Value;
  37.             progressBarControl1.Position = (int)spinEdit1.Value;
  38.  
  39.             bool res = false;
  40.             if (File.Exists(filename))
  41.             {
  42.                 System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
  43.                 Object empty = System.Reflection.Missing.Value;
  44.                 Excel.Application excelApp = new Excel.ApplicationClass();
  45.  
  46.                 try
  47.                 {
  48.                     Excel.Workbook excelWorkBook = excelApp.Workbooks.Open(filename,
  49.                                                                            0, true, 5,
  50.                                                                            "", "", true, Excel.XlPlatform.xlWindows,
  51.                                                                            "\t", false, false,
  52.                                                                            0, true, true, empty);
  53.  
  54.                     var excelWorkSheet = (Excel.Worksheet)excelWorkBook.Worksheets.get_Item(1);
  55.  
  56.                     bindingSource1.DataSource = null;
  57.                     _bookmarkSwitches.Clear();
  58.  
  59.                     for (var i = (int)spinEdit1.Value; i < (int)spinEdit2.Value; i++)
  60.                     {
  61.                         var range1 = (Excel.Range)excelWorkSheet.Cells[i, textEdit2.Text];
  62.                         var range2 = (Excel.Range)excelWorkSheet.Cells[i, textEdit1.Text];
  63.  
  64.                         var b = new BookmarkSwitches(range1.Value2 != null ? range1.Value2.ToString() : "",
  65.                                                                   range2.Value2 != null ? range2.Value2.ToString() : "");
  66.                         _bookmarkSwitches.Add(b);
  67.                         progressBarControl1.Position += 1;
  68.                     }
  69.  
  70.                     bindingSource1.DataSource = _bookmarkSwitches;
  71.                     res = true;
  72.                 }
  73.                 finally
  74.                 {
  75.                     excelApp.Quit();
  76.                     Cursor = Cursors.Default;
  77.                 }
  78.             }
  79.  
  80.             return res;
  81.         }
  82.  
  83.         private bool LoadWordFiles(string path, string extension)
  84.         {
  85.             bool res = false;
  86.             Cursor = Cursors.WaitCursor;
  87.             try
  88.             {
  89.                 var dirInfo = new DirectoryInfo(path);
  90.  
  91.                 FileInfo[] fileInfo = dirInfo.GetFiles("*" + extension);
  92.                 foreach (FileInfo file in fileInfo)
  93.                 {
  94.                     if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden ||
  95.                         (file.Attributes & FileAttributes.System) == FileAttributes.System)
  96.                         continue;
  97.  
  98.                     if (String.Compare(file.Extension, extension) == 0)
  99.                         _wordFiles.Add(new WordFiles(file.FullName));
  100.                 }
  101.  
  102.                 DirectoryInfo[] dirs = dirInfo.GetDirectories();
  103.                 foreach (DirectoryInfo dir in dirs)
  104.                 {
  105.                     if (!LoadWordFiles(dir.FullName, extension))
  106.                         break;
  107.                 }
  108.  
  109.                 res = true;
  110.                 Cursor = Cursors.Default;
  111.             }
  112.             catch (Exception ex)
  113.             {
  114.                 MessageBox.Show(ex.Message);
  115.                 Cursor = Cursors.Default;
  116.             }
  117.  
  118.             return res;
  119.         }
  120.  
  121.  
  122.         private static void DoBookmark(Word.Document doc, BookmarkSwitches switches, BookmarkProcessingType processingType)
  123.         {
  124.             switch (processingType)
  125.             {
  126.                 case BookmarkProcessingType.Zamena:
  127.  
  128.                     Object b = switches.Bookmark;
  129.                     Word.Range range = doc.Bookmarks.get_Item(ref b).Range;
  130.                     range.InsertAfter(switches.Zamena);
  131.                     doc.Bookmarks.get_Item(ref b).Delete();
  132.  
  133.                     break;
  134.  
  135.                 case BookmarkProcessingType.Preimenovanje:
  136.  
  137.                     Object p = switches.Bookmark;
  138.                     Word.Bookmark bb = doc.Bookmarks.get_Item(ref p);
  139.                     Object rangep = bb.Range;
  140.                     bb.Select();
  141.                     doc.Bookmarks.Add(switches.Zamena, ref rangep);
  142.                     bb.Delete();
  143.  
  144.                     break;
  145.  
  146.                 case BookmarkProcessingType.Tagovanje:
  147.  
  148.                     Word.Find find = doc.Content.Find;
  149.                     Object findText = switches.Bookmark;
  150.                     Object findReplacementText = switches.Zamena;
  151.                     Object findForward = true;
  152.                     Object findWrap = Word.WdFindWrap.wdFindContinue;
  153.                     Object findFormat = false;
  154.                     Object findMatchCase = false;
  155.                     Object findMatchWholeWord = true;
  156.                     Object findMatchWildcards = false;
  157.                     Object findMatchSoundsLike = false;
  158.                     Object findMatchAllWordForms = false;
  159.                     Object empty = System.Reflection.Missing.Value;
  160.                     Object replace = true;
  161.  
  162.                     find.Execute(ref findText, ref findMatchCase, ref findMatchWholeWord,
  163.                                  ref findMatchWildcards, ref findMatchSoundsLike, ref findMatchAllWordForms,
  164.                                  ref findForward, ref findWrap,
  165.                                  ref findFormat, ref findReplacementText, ref replace, ref empty,
  166.                                  ref empty, ref empty, ref empty
  167.                         );
  168.  
  169.                     break;
  170.             }
  171.         }
  172.  
  173.         private bool ProcessWordFiles(BookmarkProcessingType processingType)
  174.         {
  175.             bool res = false;
  176.             Object empty = System.Reflection.Missing.Value;
  177.  
  178.             Cursor = Cursors.WaitCursor;
  179.  
  180.             Word.Application wordApp = new Word.ApplicationClass();
  181.  
  182.             progressBarControl1.Position = 1;
  183.             progressBarControl1.Properties.Maximum = _wordFiles.Count;
  184.  
  185.             int row = 0;
  186.             //GridColumn col = null;
  187.  
  188.             try
  189.             {
  190.  
  191.                 foreach (WordFiles wf in _wordFiles)
  192.                 {
  193.                     Object docName = wf.FullPath;
  194.                     wordApp.Documents.Open(ref docName,
  195.                                            ref empty, ref empty, ref empty, ref empty, ref empty,
  196.                                            ref empty, ref empty, ref empty, ref empty,
  197.                                            ref empty, ref empty, ref empty, ref empty, ref empty, ref empty);
  198.  
  199.                     Word.Document doc = wordApp.ActiveDocument;
  200.                     try
  201.                     {
  202.                         foreach (BookmarkSwitches bs in _bookmarkSwitches)
  203.                         {
  204.  
  205.                             if (!String.IsNullOrEmpty(bs.Bookmark) && !String.IsNullOrEmpty(bs.Zamena))
  206.                             {
  207.                                 if (doc.Bookmarks.Exists(bs.Bookmark) ||
  208.                                     processingType == BookmarkProcessingType.Tagovanje)
  209.                                     DoBookmark(doc, bs, processingType);
  210.                             }
  211.                         }
  212.  
  213.                         doc.Save();
  214.                     }
  215.                     finally
  216.                     {
  217.                         doc.Close(ref empty, ref empty, ref empty);
  218.                     }
  219.  
  220.                     switch (processingType)
  221.                     {
  222.                         case BookmarkProcessingType.Zamena:
  223.                             wf.Zamena = true;
  224.                             //col = gridView2.Columns[1];
  225.                             break;
  226.                         case BookmarkProcessingType.Preimenovanje:
  227.                             wf.Preimenovanje = true;
  228.                             //col = gridView2.Columns[2];
  229.                             break;
  230.                         case BookmarkProcessingType.Tagovanje:
  231.                             wf.Tagovanje = true;
  232.                             //col = gridView2.Columns[3];
  233.                             break;
  234.                     }
  235.  
  236.                     progressBarControl1.Position += 1;
  237.                     //gridView2.RefreshRowCell(row, col);
  238.                     gridView2.FocusedRowHandle = row;
  239.                     row++;
  240.                 }
  241.  
  242.                 res = true;
  243.  
  244.             }
  245.             finally
  246.             {
  247.                 //wordApp.Quit(ref empty, ref empty, ref empty);
  248.                 wordApp.Quit();
  249.                 Cursor = Cursors.Default;
  250.             }
  251.  
  252.             return res;
  253.         }
  254.  
  255.         private void SimpleButton1Click(object sender, EventArgs e)
  256.         {
  257.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  258.             {
  259.                 if (!LoadXcelBookmarks(openFileDialog1.FileName))
  260.                 {
  261.                     MessageBox.Show(Resources.LoadXcelFailed, Resources.ErrorCaption, MessageBoxButtons.OK,
  262.                                     MessageBoxIcon.Error);
  263.                 }
  264.             }
  265.         }
  266.  
  267.         private void SimpleButton5Click(object sender, EventArgs e)
  268.         {
  269.             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  270.             {
  271.                 _wordFiles.Clear();
  272.                 bindingSource2.DataSource = null;
  273.                 LoadWordFiles(folderBrowserDialog1.SelectedPath, comboBoxEdit1.Text);
  274.                 bindingSource2.DataSource = _wordFiles;
  275.             }
  276.         }
  277.  
  278.  
  279.         private bool CheckCanProcess()
  280.         {
  281.             bool res = _wordFiles.Count > 0 && _bookmarkSwitches.Count > 0;
  282.             if (!res)
  283.                 MessageBox.Show(Resources.CanNotProcessTxt,
  284.                                 Resources.WarningCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  285.             return res;
  286.         }
  287.  
  288.         private bool IsDone(BookmarkProcessingType type)
  289.         {
  290.             bool res = false;
  291.  
  292.             if (_wordFiles.Count > 0)
  293.             {
  294.                 switch (type)
  295.                 {
  296.                     case BookmarkProcessingType.Zamena:
  297.                         res = _wordFiles[0].Zamena;
  298.                         break;
  299.                     case BookmarkProcessingType.Preimenovanje:
  300.                         res = _wordFiles[0].Preimenovanje;
  301.                         break;
  302.                     case BookmarkProcessingType.Tagovanje:
  303.                         res = _wordFiles[0].Tagovanje;
  304.                         break;
  305.                 }
  306.             }
  307.             return res;
  308.         }
  309.  
  310.  
  311.         private void SimpleButton2Click(object sender, EventArgs e)
  312.         {
  313.             if (IsDone(BookmarkProcessingType.Zamena))
  314.             {
  315.                 MessageBox.Show(Resources.ProcessingDoneTxt, Resources.WarningCaption, MessageBoxButtons.OK,
  316.                                 MessageBoxIcon.Warning);
  317.                 return;
  318.             }
  319.  
  320.             if (CheckCanProcess())
  321.             {
  322.                 if (ProcessWordFiles(BookmarkProcessingType.Zamena))
  323.                 {
  324.                     MessageBox.Show(Resources.ReplaceFinishedOK, Resources.ReplaceFinishedOKCaption, MessageBoxButtons.OK,
  325.                                     MessageBoxIcon.Information);
  326.                 }
  327.             }
  328.         }
  329.  
  330.         private void SimpleButton3Click(object sender, EventArgs e)
  331.         {
  332.             if (IsDone(BookmarkProcessingType.Preimenovanje))
  333.             {
  334.                 MessageBox.Show(Resources.ProcessingDoneTxt, Resources.WarningCaption, MessageBoxButtons.OK,
  335.                                 MessageBoxIcon.Warning);
  336.                 return;
  337.             }
  338.  
  339.             if (CheckCanProcess())
  340.             {
  341.                 if (ProcessWordFiles(BookmarkProcessingType.Preimenovanje))
  342.                 {
  343.                     MessageBox.Show(Resources.RenameFinishedOk, Resources.RenameFinishedOkCaption,
  344.                                     MessageBoxButtons.OK,
  345.                                     MessageBoxIcon.Information);
  346.                 }
  347.             }
  348.         }
  349.  
  350.         private void SimpleButton4Click(object sender, EventArgs e)
  351.         {
  352.             if (IsDone(BookmarkProcessingType.Tagovanje))
  353.             {
  354.                 MessageBox.Show(Resources.ProcessingDoneTxt, Resources.WarningCaption, MessageBoxButtons.OK,
  355.                                 MessageBoxIcon.Warning);
  356.                 return;
  357.             }
  358.  
  359.             if (CheckCanProcess())
  360.             {
  361.                 if (ProcessWordFiles(BookmarkProcessingType.Tagovanje))
  362.                 {
  363.                     MessageBox.Show(Resources.TagsFinishedOK, Resources.TagFinishedOKCaption, MessageBoxButtons.OK,
  364.                                     MessageBoxIcon.Information);
  365.                 }
  366.             }
  367.         }
  368.     }
  369.  
  370.     internal enum BookmarkProcessingType
  371.     {
  372.         Zamena,
  373.         Preimenovanje,
  374.         Tagovanje,
  375.     }
  376.  
  377.     public class BookmarkSwitches
  378.     {
  379.         public string Bookmark { get; set; }
  380.         public string Zamena { get; set; }
  381.  
  382.         public BookmarkSwitches(string bookmark, string zamena)
  383.         {
  384.             Bookmark = bookmark;
  385.             Zamena = zamena;
  386.         }
  387.     }
  388.  
  389.     public class WordFiles : INotifyPropertyChanged
  390.     {
  391.         private bool _zamena;
  392.  
  393.         public string FullPath { get; set; }
  394.         public string FileName { get; set; }
  395.  
  396.         public bool Zamena
  397.         {
  398.             get { return _zamena; }
  399.             set
  400.             {
  401.                 if (value != _zamena)
  402.                 {
  403.                     _zamena = value;
  404.                     NotifyChanged("Zamena");
  405.                 }
  406.             }
  407.         }
  408.  
  409.         public bool Preimenovanje { get; set; }
  410.         public bool Tagovanje { get; set; }
  411.  
  412.         public WordFiles(string fullPath)
  413.         {
  414.             FullPath = fullPath;
  415.             FileName = Path.GetFileName(FullPath);
  416.             Zamena = false;
  417.             Preimenovanje = false;
  418.             Tagovanje = false;
  419.         }
  420.  
  421.         public event PropertyChangedEventHandler PropertyChanged;
  422.  
  423.         private void NotifyChanged(string info)
  424.         {
  425.             if (PropertyChanged != null)
  426.             {
  427.                 PropertyChanged(this, new PropertyChangedEventArgs(info));
  428.             }
  429.         }
  430.     }
  431. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement