none
How to read the data from a word .doc file? RRS feed

解答

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    //vincent
    using System.Diagnostics;

    namespace WindowsApplication16
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                this.timer1.Start();
                print();           
            }
            private void print()
            {
                try
                {
                    // Declaring the object variables we will need later
                    object varFileName = "c:\\temp\\test.docx";
                    object varFalseValue = false;
                    object varTrueValue = true;
                    object varMissing = Type.Missing;
                    // Create a reference to MS Word application
                    Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
                    // Creates a reference to a word document
                    Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
                    // Activate the document
                    varDoc.Activate();

                    // Print the document
                    object Background = true;
                    object PrintToFile = true;
                    object OutputFileName = "c:\\temp\\test.tif";

                    //varDoc.PrintOut(ref varTrueValue, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing,ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
                    varDoc.PrintOut(ref Background, ref varFalseValue, ref varMissing, ref OutputFileName, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref PrintToFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);               
                   
                    varDoc.Close(ref varMissing, ref varMissing, ref varMissing);
                    varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
                    // Send mail with this document as an attachment
                    //varDoc.SendMail();
                }
                catch (Exception varE)
                {
                    MessageBox.Show("Error:\n" + varE.Message, "Error message");
                }
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                // Get all instances of Notepad running on the local
                Process[] localByName = Process.GetProcessesByName("rundll32");
                foreach (Process tempProcess in localByName)
                {
                    tempProcess.Kill();
                }
            }
        }
    }

    2007年7月11日 上午 03:40

所有回覆

2007年7月8日 上午 11:08
  • 感謝回應
    2007年7月9日 上午 01:00
  • 請提供程式碼

    範例

    非常感謝

    2007年7月10日 上午 07:45
  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    //vincent
    using System.Diagnostics;

    namespace WindowsApplication16
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                this.timer1.Start();
                print();           
            }
            private void print()
            {
                try
                {
                    // Declaring the object variables we will need later
                    object varFileName = "c:\\temp\\test.docx";
                    object varFalseValue = false;
                    object varTrueValue = true;
                    object varMissing = Type.Missing;
                    // Create a reference to MS Word application
                    Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
                    // Creates a reference to a word document
                    Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
                    // Activate the document
                    varDoc.Activate();

                    // Print the document
                    object Background = true;
                    object PrintToFile = true;
                    object OutputFileName = "c:\\temp\\test.tif";

                    //varDoc.PrintOut(ref varTrueValue, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing,ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
                    varDoc.PrintOut(ref Background, ref varFalseValue, ref varMissing, ref OutputFileName, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref PrintToFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);               
                   
                    varDoc.Close(ref varMissing, ref varMissing, ref varMissing);
                    varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
                    // Send mail with this document as an attachment
                    //varDoc.SendMail();
                }
                catch (Exception varE)
                {
                    MessageBox.Show("Error:\n" + varE.Message, "Error message");
                }
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                // Get all instances of Notepad running on the local
                Process[] localByName = Process.GetProcessesByName("rundll32");
                foreach (Process tempProcess in localByName)
                {
                    tempProcess.Kill();
                }
            }
        }
    }

    2007年7月11日 上午 03:40