Esempio n. 1
0
        internal override void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Free managed
                if (_application != null)
                {
                    _application.Dispose();
                    _application = null;
                }
            }

            base.Dispose(true);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            using (var word = new Word.Application())
            {
                word.Visible = true;

                Console.WriteLine("Version: " + word.Version.ToString());

                //word.Documents.Add().Dispose();

                using (var doc = word.Documents.Add())
                {
                    word.Selection.TypeText("fred");

                    doc.ExportAsFixedFormat(@"c:\temp\out.pdf", Word.Enums.ExportFormat.PDF);
                }
            }
        }
Esempio n. 3
0
        private void button1_Click(object sender, EventArgs e)
        {
            using (var word = new Word.Application())
            {
                word.Visible = true;

                Console.WriteLine("Version: " + word.Version.ToString());

                //word.Documents.Add().Dispose();

                using (var doc = word.Documents.Add())
                {
                    word.Selection.TypeText("fred");

                    doc.ExportAsFixedFormat(@"c:\temp\out.pdf", Word.Enums.ExportFormat.PDF);

                }
            }
        }
Esempio n. 4
0
        private void btnExistingWord_Click(object sender, EventArgs e)
        {
            using (var word = new Word.Application())
            {
                word.Visible = true;

                Console.WriteLine("Version: " + word.Version.ToString());

                //word.Documents.Add().Dispose();

                using (var doc = (sender == btnNewWord) ?
                    word.Documents.Add() :
                    word.Documents.Open(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName) + "\\Templates\\Open Tester.doc"))
                {
                    //MessageBox.Show(doc.Name);

                    //doc.SaveAs("c:\\test.doc");

                    doc.Activate();

                    Console.WriteLine(doc.Name);
                    Console.WriteLine(doc.FullName);
                    Console.WriteLine(doc.Path);

                    Console.WriteLine("Read only?: " + doc.ReadOnly.ToString());

                    //doc.Close(false);

                    //MessageBox.Show(doc.Sections.Count.ToString());
                    //int x = 0;
                    foreach (Word.Section section in doc.Sections)
                    {
                        //x++;
                        //MessageBox.Show("Index: " + section.Index);
                        //MessageBox.Show("Section: " + section.Index + "\n\n" + section.Range.Text);
                    }
                    //MessageBox.Show(x.ToString());

                    using (Word.Section sec1 = doc.Sections[1])
                    {
                        //MessageBox.Show("Section 1 index: " + sec1.Index.ToString());

                        using (Word.HeadersFooters headers = sec1.Headers)
                        {
                            //MessageBox.Show("Headers: " + headers.Count.ToString());
                            int x = 0;
                            foreach (Word.HeaderFooter header in headers)
                            {
                                x++;

                                //MessageBox.Show("Index: " + header.Index + (header.IsHeader ? " is header" : " is footer") + "\n\n" + header.Range.Text);

                                if (x == 1)
                                    header.Range.Text = "Some other header";
                            }
                            //MessageBox.Show(x.ToString());
                        }

                        using (Word.HeadersFooters footers = sec1.Footers)
                        {
                            //MessageBox.Show("Footers: " + footers.Count.ToString());
                            int x = 0;
                            foreach (Word.HeaderFooter footer in footers)
                            {
                                x++;
                                //MessageBox.Show("Index: " + footer.Index + (footer.IsHeader ? " is header" : " is footer") + "\n\n" + footer.Range.Text);
                            }
                            //MessageBox.Show(x.ToString());
                        }
                    }

                    using (var w = doc.ActiveWindow)
                        w.WindowState = STC.Automation.Office.Word.Enums.WindowState.Normal;

                    if (doc.Bookmarks.Exists("text"))
                    {
                        Console.WriteLine("Found 'text' bookmark");

                        using (var bookmark = doc.Bookmarks["text"])
                        {
                            bookmark.Select();
                        }
                    }

                    if (doc.Bookmarks.Exists("Red"))
                    {
                        Console.WriteLine("Found 'Red' bookmark");

                        doc.Bookmarks["Red"].Select();

                        using (var font = word.Selection.Font)
                        {
                            Console.WriteLine(font.Color.ToString());
                            font.Color = Color.Teal;
                        }
                    }

                    if (doc.Bookmarks.Exists("Bold"))
                    {
                        doc.Bookmarks["Bold"].Select();
                        using (var font = word.Selection.Font)
                        {
                            Console.WriteLine("Bold is bold: {0}", font.Bold);
                            font.Bold = null;
                        }
                    }

                    if (doc.Bookmarks.Exists("NotBold"))
                    {
                        doc.Bookmarks["NotBold"].Select();
                        using (var font = word.Selection.Font)
                        {
                            Console.WriteLine("NotBold is bold: {0}", font.Bold);
                            font.Bold = null;
                        }
                    }

                    if (doc.Bookmarks.Exists("PartlyBold"))
                    {
                        doc.Bookmarks["PartlyBold"].Select();
                        using (var font = word.Selection.Font)
                        {
                            Console.WriteLine("PartlyBold is bold: {0}", font.Bold);
                            font.Bold = null;
                        }
                    }

                    if (doc.Bookmarks.Exists("TwoColumn"))
                    {
                        doc.Bookmarks["TwoColumn"].Select();
                        using (var cols = word.Selection.Columns)
                            Console.WriteLine("TwoColumn column count: {0}", cols.Count);
                    }

                    if (doc.Bookmarks.Exists("ThreeColumn"))
                    {
                        doc.Bookmarks["ThreeColumn"].Select();
                        using (var cols = word.Selection.Columns)
                            Console.WriteLine("ThreeColumn column count: {0}", cols.Count);
                    }

                    doc.Select();
                    using (var find = word.Selection.Find)
                    {
                        find.ClearFormatting();
                        find.Text = "Some text";
                        find.Replacement.ClearFormatting();
                        find.Replacement.Text = "Other bologna";
                        find.Execute(STC.Automation.Office.Word.Enums.Replace.One);
                    }
                }
                

                /*foreach (Word.Document doc in word.Documents)
                {
                    MessageBox.Show(doc.Name);

                    doc.Dispose();

                    break;
                }*/

                //MessageBox.Show("Closed");

                //word.Quit();
            }
        }
        private void btnExistingWord_Click(object sender, EventArgs e)
        {
            using (var word = new Word.Application())
            {
                word.Visible = true;

                Console.WriteLine("Version: " + word.Version.ToString());

                //word.Documents.Add().Dispose();

                using (var doc = (sender == btnNewWord) ?
                                 word.Documents.Add() :
                                 word.Documents.Open(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName) + "\\Templates\\Open Tester.doc"))
                {
                    //MessageBox.Show(doc.Name);

                    //doc.SaveAs("c:\\test.doc");

                    doc.Activate();

                    Console.WriteLine(doc.Name);
                    Console.WriteLine(doc.FullName);
                    Console.WriteLine(doc.Path);

                    Console.WriteLine("Read only?: " + doc.ReadOnly.ToString());

                    //doc.Close(false);

                    //MessageBox.Show(doc.Sections.Count.ToString());
                    //int x = 0;
                    foreach (Word.Section section in doc.Sections)
                    {
                        //x++;
                        //MessageBox.Show("Index: " + section.Index);
                        //MessageBox.Show("Section: " + section.Index + "\n\n" + section.Range.Text);
                    }
                    //MessageBox.Show(x.ToString());

                    using (Word.Section sec1 = doc.Sections[1])
                    {
                        //MessageBox.Show("Section 1 index: " + sec1.Index.ToString());

                        using (Word.HeadersFooters headers = sec1.Headers)
                        {
                            //MessageBox.Show("Headers: " + headers.Count.ToString());
                            int x = 0;
                            foreach (Word.HeaderFooter header in headers)
                            {
                                x++;

                                //MessageBox.Show("Index: " + header.Index + (header.IsHeader ? " is header" : " is footer") + "\n\n" + header.Range.Text);

                                if (x == 1)
                                {
                                    header.Range.Text = "Some other header";
                                }
                            }
                            //MessageBox.Show(x.ToString());
                        }

                        using (Word.HeadersFooters footers = sec1.Footers)
                        {
                            //MessageBox.Show("Footers: " + footers.Count.ToString());
                            int x = 0;
                            foreach (Word.HeaderFooter footer in footers)
                            {
                                x++;
                                //MessageBox.Show("Index: " + footer.Index + (footer.IsHeader ? " is header" : " is footer") + "\n\n" + footer.Range.Text);
                            }
                            //MessageBox.Show(x.ToString());
                        }
                    }

                    using (var w = doc.ActiveWindow)
                        w.WindowState = STC.Automation.Office.Word.Enums.WindowState.Normal;

                    if (doc.Bookmarks.Exists("text"))
                    {
                        Console.WriteLine("Found 'text' bookmark");

                        using (var bookmark = doc.Bookmarks["text"])
                        {
                            bookmark.Select();
                        }
                    }

                    if (doc.Bookmarks.Exists("Red"))
                    {
                        Console.WriteLine("Found 'Red' bookmark");

                        doc.Bookmarks["Red"].Select();

                        using (var font = word.Selection.Font)
                        {
                            Console.WriteLine(font.Color.ToString());
                            font.Color = Color.Teal;
                        }
                    }

                    if (doc.Bookmarks.Exists("Bold"))
                    {
                        doc.Bookmarks["Bold"].Select();
                        using (var font = word.Selection.Font)
                        {
                            Console.WriteLine("Bold is bold: {0}", font.Bold);
                            font.Bold = null;
                        }
                    }

                    if (doc.Bookmarks.Exists("NotBold"))
                    {
                        doc.Bookmarks["NotBold"].Select();
                        using (var font = word.Selection.Font)
                        {
                            Console.WriteLine("NotBold is bold: {0}", font.Bold);
                            font.Bold = null;
                        }
                    }

                    if (doc.Bookmarks.Exists("PartlyBold"))
                    {
                        doc.Bookmarks["PartlyBold"].Select();
                        using (var font = word.Selection.Font)
                        {
                            Console.WriteLine("PartlyBold is bold: {0}", font.Bold);
                            font.Bold = null;
                        }
                    }

                    if (doc.Bookmarks.Exists("TwoColumn"))
                    {
                        doc.Bookmarks["TwoColumn"].Select();
                        using (var cols = word.Selection.Columns)
                            Console.WriteLine("TwoColumn column count: {0}", cols.Count);
                    }

                    if (doc.Bookmarks.Exists("ThreeColumn"))
                    {
                        doc.Bookmarks["ThreeColumn"].Select();
                        using (var cols = word.Selection.Columns)
                            Console.WriteLine("ThreeColumn column count: {0}", cols.Count);
                    }

                    doc.Select();
                    using (var find = word.Selection.Find)
                    {
                        find.ClearFormatting();
                        find.Text = "Some text";
                        find.Replacement.ClearFormatting();
                        find.Replacement.Text = "Other bologna";
                        find.Execute(STC.Automation.Office.Word.Enums.Replace.One);
                    }
                }


                /*foreach (Word.Document doc in word.Documents)
                 * {
                 *  MessageBox.Show(doc.Name);
                 *
                 *  doc.Dispose();
                 *
                 *  break;
                 * }*/

                //MessageBox.Show("Closed");

                //word.Quit();
            }
        }