基本信息
源码名称:Word文档批量打印源码
源码大小:0.22M
文件格式:.rar
开发语言:C#
更新时间:2017-04-30
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Interop.Word;


namespace PrintAllWordDoces
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;
            this.MaximumSize = this.Size;
            txtDir.Enabled = false;
            btnGetOut.Enabled = false;
            btnPrintAll.Enabled = false;
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSelectDir_Click(object sender, EventArgs e)
        {
            // 获取文件夹绝对路径    显示在 txtbox 控件里
            FolderBrowserDialog folder = new FolderBrowserDialog();
            if (folder.ShowDialog() == DialogResult.OK)
            {
                this.txtDir.Text = folder.SelectedPath;
                this.btnGetOut.Enabled = true;
                this.btnPrintAll.Enabled = true;

            }
            else
            {
                this.txtDir.Clear();
                this.btnGetOut.Enabled = false;
                this.btnPrintAll.Enabled = false;
            }

        }

        private void btnGetOut_Click(object sender, EventArgs e)
        {
            if (this.txtDir.Text.Trim() == "")
            {
                MessageBox.Show(this, "请先指定文件夹!", "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                String temp = this.txtDir.Text.Trim()   "\\";
                string[] filePaths = Directory.GetFiles(@temp, "*.docx");
                if (filePaths.Length < 1)
                {
                    MessageBox.Show(this, "未发现WORD文档!", "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                this.btnQuit.Enabled = false;
                this.btnGetOut.Enabled = false;
                this.btnPrintAll.Enabled = false;

                if (!Directory.Exists(@temp   "Dir8\\"))  //若文件夹不存在则新建文件夹
                {
                    Directory.CreateDirectory(@temp   "Dir8\\"); //新建文件夹
                }

                //启动Word程序
                _Application WordApp = new Microsoft.Office.Interop.Word.Application();
                object oMissing = System.Reflection.Missing.Value;


                for (int i = 0; i < filePaths.Length; i  )
                {
                    object filePath = filePaths[i].Trim(); //这里是Word文件的路径
                    //打开文件
                    _Document WordDoc = WordApp.Documents.Open(
                        ref filePath, 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);
                    //下面是取得打开文件的页数
                    int pages = WordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref oMissing);
                    //关闭文件
                    WordDoc.Close(WdSaveOptions.wdDoNotSaveChanges, ref oMissing, ref oMissing);
                    WordDoc = null;
                    if (pages > 8)
                    {
                        string fullFileName = System.IO.Path.GetFileName((String)filePath);

                        File.Move((String)filePath, @temp   "Dir8\\"   fullFileName);
                    }
                }
                //退出Word程序
                WordApp.Quit();
                WordApp = null;
                this.btnQuit.Enabled = true;
                this.btnGetOut.Enabled = true;
                this.btnPrintAll.Enabled = true;
                MessageBox.Show(this, "处理完毕!", "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void btnPrintAll_Click(object sender, EventArgs e)
        {
            if (this.txtDir.Text.Trim() == "")
            {
                MessageBox.Show(this, "请先指定文件夹!", "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                String temp = this.txtDir.Text.Trim()   "\\";
                string[] filePaths = Directory.GetFiles(@temp, "*.docx");
                if (filePaths.Length < 1)
                {
                    MessageBox.Show(this, "未发现WORD文档!", "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                this.btnQuit.Enabled = false;
                this.btnGetOut.Enabled = false;
                this.btnPrintAll.Enabled = false;

                //启动Word程序
                _Application WordApp = new Microsoft.Office.Interop.Word.Application();
                object oMissing = System.Reflection.Missing.Value;

                for (int i = 0; i < filePaths.Length; i  )
                {
                    object filePath = filePaths[i].Trim(); //这里是Word文件的路径
                    //打开文件
                    _Document WordDoc = WordApp.Documents.Open(
                        ref filePath, 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);
                    //打印文档
                    WordDoc.PrintOut();
                    //关闭文件
                    WordDoc.Close(WdSaveOptions.wdDoNotSaveChanges, ref oMissing, ref oMissing);
                    WordDoc = null;
                }
                //退出Word程序
                WordApp.Quit();
                WordApp = null;
                this.btnQuit.Enabled = true;
                this.btnGetOut.Enabled = true;
                this.btnPrintAll.Enabled = true;
                MessageBox.Show(this,"处理完毕!","系统信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }

        }
    }
}