Exemple #1
0
        private void CreateWordFile(string strFilePath)
        {
            //Creating the instance of Word Application
            Word.Application newApp = new Word.ApplicationClass();
            object           Source = strFilePath;
            // specifying the Source & Target file names
            string strNewPath = strFilePath.Replace(".html", ".rtf");

            object Target = strNewPath;

            // Use for the parameter whose type are not known or
            // say Missing
            object Unknown = Type.Missing;

            Word.Document objDoc;
            // Source document open here
            // Additional Parameters are not known so that are
            // set as a missing type
            objDoc = newApp.Documents.Open(ref Source, ref Unknown,
                                           ref Unknown, ref Unknown, ref Unknown,
                                           ref Unknown, ref Unknown, ref Unknown,
                                           ref Unknown, ref Unknown, ref Unknown,
                                           ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

            // Specifying the format in which you want the output file
            //object format = Word.WdSaveFormat.wdFormatDocument;
            object format = Word.WdSaveFormat.wdFormatRTF;

            objDoc.Activate();


            foreach (Paragraph rPrg in newApp.ActiveDocument.Paragraphs)
            {
                rPrg.Range.Font.Name = "VNI-Times";
                rPrg.Range.Font.Size = 14;
            }
            newApp.Selection.TypeParagraph();
            newApp.Selection.Font.Name = "VNI-Times";
            //Changing the format of the document
            newApp.ActiveDocument.SaveAs(ref Target, ref format,
                                         ref Unknown, ref Unknown, ref Unknown,
                                         ref Unknown, ref Unknown, ref Unknown,
                                         ref Unknown, ref Unknown, ref Unknown,
                                         ref Unknown, ref Unknown, ref Unknown,
                                         ref Unknown, ref Unknown);

            newApp.ActiveDocument.Save();
            // for closing the application
            newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
        }
        static void Main(string[] args)
        {
            // Find the full path of our document

            System.IO.FileInfo ExecutableFileInfo = new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
            object             docFileName        = System.IO.Path.Combine(ExecutableFileInfo.DirectoryName, "document.doc");

            // Create the needed Word.Application and Word.Document objects

            object nullObject = System.Reflection.Missing.Value;

            Word.Application application = new Word.ApplicationClass();
            Word.Document    document    = application.Documents.Open(ref docFileName, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject);


            string wholeTextContent = document.Content.Text;

            wholeTextContent = wholeTextContent.Replace('\r', ' ');     // Delete lines between paragraphs
            string[] splittedTextContent = wholeTextContent.Split(' '); // Get the separate words

            int index = 1;

            foreach (string singleWord in splittedTextContent)
            {
                if (singleWord.Trim().Length > 0)     // We don´t need to store white spaces
                {
                    Console.WriteLine("Word: " + singleWord + "(position: " + index.ToString() + ")");
                    index++;
                }
            }

            // Dispose Word.Application and Word.Document objects resources

            document.Close(ref nullObject, ref nullObject, ref nullObject);
            application.Quit(ref nullObject, ref nullObject, ref nullObject);
            document    = null;
            application = null;

            Console.ReadLine();
        }