C# WORD, and replace text, plus hebrew trick examlpe

the WordHandler class is the hard work of one of our ex-engineers.
pls remember that the number of parameters passed changes with the office versions
also u need to put the right Microsoft.Office.Interop.Word version, and its not according the office (word 14 is for office 10)
the trick is mine - the text backworded so a added in the word template val2 after val and it did the trick
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
namespace WordFactory
{
     class Program
     {
        
static Word.Application oWord;
         static Word.Document oWordDoc;
         static void Main(string[] args)
         {
              WordHandler wh = new WordHandler();
              string pathA = @"F:\****.dot";
              string path = @"F:\****.dot";
              wh.CreateWord(path, out oWord, out oWordDoc);
              //the TRUTH == 201123USD01
              string acc = "201123USD01";
              string acc1 = " USD01 ";
              string acc2 = " ";
              // 01USD201123 --> USD20112301
              // 201123USD01 --> USD01201123
              //yes i put #VAL2# replace it with " " and its ok!
              ReplaceText("#VAL#", acc);
              ReplaceText("#VAL2#", " ");
              oWordDoc.Activate();
        }
        static private void ReplaceText(String old, String newWord)
        {
                object o = oWordDoc.Content.Start;
                object o2 = oWordDoc.Content.End - 1;
                Word.Range r = oWordDoc.Range(ref o, ref o2);
                Object missing = System.Reflection.Missing.Value;
                object replaceAll = Word.WdReplace.wdReplaceAll;
                r.Find.Text = old;
                r.Find.Replacement.Text = newWord;
                r.Find.Execute(
ref missing, ref missing, ref missing,   // number of params depends on office version
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref replaceAll,
                        ref missing, ref missing, ref missing, ref missing);
         }
    }
   
   
    public class WordHandler
    {
             private Object oMissing = System.Reflection.Missing.Value;
             //OBJECTS OF FALSE AND TRUE
             private Object oTrue = true;
             private Object oFalse = false;
             public void KillApp()
            {
                   System.Diagnostics.
Process[] p = System.Diagnostics.Process.GetProcessesByName("WINWORD");
                   for (int i = 0; i < p.Length; i++)
                   {
                         try
                         {
                             p[i].Kill();
                         }
                         catch (Exception ex)
                         {
                                 if (ex.Message.ToLower().Contains("access is denied"))
                                         continue;
                          }
                    }
             }
       

             public void Print(ref Word.Document oWordDoc)
             {
                        object copies = "1";
                        object pages = "";
                        object range = Word.WdPrintOutRange.wdPrintAllDocument;
                        object items = Word.WdPrintOutItem.wdPrintDocumentContent;
                        object pageType = Word.WdPrintOutPages.wdPrintAllPages;
                        object oTrue = true;
                        object oFalse = false;
                        Object missing = System.Reflection.Missing.Value;
                        oWordDoc.PrintOut(ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing,
                                     ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
                                     ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
              }
        

             public void CreateWord(String filename, out Word.Application oWord, out Word.Document oWordDoc)
             {
                   KillApp();
                   //CREATING OBJECTS OF WORD AND DOCUMENT
                  oWord = new Word.Application();
                  oWord.Visible = true;
                  oWordDoc = new Word.Document();
                  oWord.Visible = true;
                  //MAKING THE APPLICATION VISIBLE
                  // oWord.Visible = true;
                  //ADDING A NEW DOCUMENT TO THE APPLICATION
                  // oworddoc = oword.documents.add(ref omissing, ref omissing, ref omissing, ref omissing);
                  object isVisible = true;
                  Object oSaveAsFile = (Object)filename;
                  oWordDoc = oWord.Documents.Open(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
                         ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                         ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing
                        , ref oMissing, ref oMissing);
            }
       

            public void SaveFile(String fileName, ref Word.Application oWord,
                                                  ref Word.Document oWordDoc, Boolean delete)
            {
                     Object oSaveAsFile = (Object)fileName;
                     if (delete)
                     {
                             if (System.IO.File.Exists(fileName))
                                    System.IO.File.Delete(fileName);
                     }
           
          else
                             oSaveAsFile = (Object)(fileName + DateTime.Now.Second.ToString());
                     oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
                                     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                     ref oMissing, ref oMissing);
           }
       

            public void CreateWordBasedOnTemplate(String filename, String basedOn,
                                           out Word.Application oWord, out Word.Document oWordDoc)
            {
           
          //CREATING OBJECTS OF WORD AND DOCUMENT
                     oWord = new Word.Application();
                     oWordDoc = new Word.Document();
                     //MAKING THE APPLICATION VISIBLE
                     // oWord.Visible = true;
                     //ADDING A NEW DOCUMENT TO THE APPLICATION
                     //oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                     Object oFilePath = basedOn;
                     oWordDoc = oWord.Documents.Add(ref oFilePath, ref oMissing, ref oMissing, ref oTrue);
                     //oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.InsertFile(
                     //       oFilePath, ref oMissing, ref oFalse, ref oFalse, ref oFalse);
                     Object oSaveAsFile = (Object)filename;
                   oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
                             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                             ref oMissing, ref oMissing);
            }
       

            public void FindAndReplace(Word.Application oWord, object findText, object replaceText)
           {
                 object matchCase = true;
                 object matchWholeWord = true;
                 object matchWildCards = false;
                 object matchSoundsLine = false;
                 object nmatchAllWordForms = false;
                 object forward = true;
                 object format = false;
                 object matchKashida = false;
                 object matchDiacritics = false;
                 object matchAlefHamza = false;
                 object matchControl = false;
                 object read_only = false;
                 object visible = true;
                 object replace = 2;
                 object wrap = 1;
                 oWord.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
                            ref matchWildCards, ref matchSoundsLine,
                            ref nmatchAllWordForms, ref forward, ref wrap, ref format, ref replaceText, ref replace,
                            ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
           }
     }
}

Comments

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem

Asp.Net Ending Response options, Response.End() vs CompleteRequest()