コード例 #1
0
        private OpenFileDialog G_OpenFileDialog; //定义浏览文件夹字段

        private void btn_New_Click(object sender, EventArgs e)
        {
            btn_Get.Enabled = false;                                    //停用统计按钮
            ThreadPool.QueueUserWorkItem(                               //开始线程池
                (pp) =>                                                 //使用lambda表达式
            {
                G_wa = new Microsoft.Office.Interop.Word.Application(); //创建应用程序对象
                object P_OpenFileDialog =                               //创建object对象
                                          G_OpenFileDialog.FileName;
                Word.Document P_Document = G_wa.Documents.Open(         //打开Word文档
                    ref P_OpenFileDialog, ref G_missing, ref G_missing, ref G_missing
                    , ref G_missing, ref G_missing, ref G_missing, ref G_missing
                    , ref G_missing, ref G_missing, ref G_missing, ref G_missing
                    , ref G_missing, ref G_missing, ref G_missing, ref G_missing);
                int P_count = 0;    //定义计数器并初始化为0
                foreach (Word.Paragraph paragraph in P_Document.Paragraphs)
                {
                    Word.Range P_Range_temp = paragraph.Range; //得到段落文本范围
                    foreach (char P_chr in P_Range_temp.Text)  //遍历每一个字符
                    {
                        P_count =                              //计数器开始计数
                                  P_chr.ToString() != "\r" ?
                                  rbtn_Blank.Checked ? ++P_count :
                                  P_chr.ToString() != " " ? ++P_count :
                                  P_count : P_count;
                    }
                }
                ((Word._Application)G_wa.Application).Quit( //退出应用程序
                    ref G_missing, ref G_missing, ref G_missing);
                this.Invoke(                                //调用窗体线程
                    (MethodInvoker)(() =>                   //使用lambda表达式
                {
                    MessageBox.Show(                        //提示已经创建Word
                        string.Format("{0}共{1}个字符",
                                      rbtn_Blank.Checked ? "记空格" : "不记空格",
                                      P_count.ToString()), "提示!");
                    btn_Get.Enabled = true;        //启用统计按扭
                }));
            });
        }
コード例 #2
0
        private OpenFileDialog G_OpenFileDialog; //定義瀏覽資料夾欄位

        private void btn_New_Click(object sender, EventArgs e)
        {
            btn_Get.Enabled = false;                                    //停用統計按鈕
            ThreadPool.QueueUserWorkItem(                               //開始線程池
                (pp) =>                                                 //使用lambda表達式
            {
                G_wa = new Microsoft.Office.Interop.Word.Application(); //建立應用程式物件
                object P_OpenFileDialog =                               //建立object物件
                                          G_OpenFileDialog.FileName;
                Word.Document P_Document = G_wa.Documents.Open(         //打開Word文件檔
                    ref P_OpenFileDialog, ref G_missing, ref G_missing, ref G_missing
                    , ref G_missing, ref G_missing, ref G_missing, ref G_missing
                    , ref G_missing, ref G_missing, ref G_missing, ref G_missing
                    , ref G_missing, ref G_missing, ref G_missing, ref G_missing);
                int P_count = 0;    //定義計數器並初始化為0
                foreach (Word.Paragraph paragraph in P_Document.Paragraphs)
                {
                    Word.Range P_Range_temp = paragraph.Range; //得到段落文字範圍
                    foreach (char P_chr in P_Range_temp.Text)  //深度搜尋每一個字符
                    {
                        P_count =                              //計數器開始計數
                                  P_chr.ToString() != "\r" ?
                                  rbtn_Blank.Checked ? ++P_count :
                                  P_chr.ToString() != " " ? ++P_count :
                                  P_count : P_count;
                    }
                }
                ((Word._Application)G_wa.Application).Quit( //退出應用程式
                    ref G_missing, ref G_missing, ref G_missing);
                this.Invoke(                                //呼叫視窗線程
                    (MethodInvoker)(() =>                   //使用lambda表達式
                {
                    MessageBox.Show(                        //提示已經建立Word
                        string.Format("{0}共{1}個字符",
                                      rbtn_Blank.Checked ? "記空格" : "不記空格",
                                      P_count.ToString()), "提示!");
                    btn_Get.Enabled = true;        //啟用統計按扭
                }));
            });
        }