编程词典
明日图书
明日软件
用户注册
用户登录
站内
  当前位置:编程词典服务网 >> 编程词典社区论坛 >> 统计Word文档页码
  · 统计Word文档页码

用户:  明日科技
发帖:  417 篇
作品:  1 个
回复:  2 篇
技术分:  805 分
等级:  
注册:  2009-09-07 06:37:23



发表于:2010-06-11 08:48:07
          楼主

在对Word文档进行操作时,最常用的并不是字符、段落的统计,而是页码的统计,尤其是批量统计文档的页码,可以在不打开文档的情况下进行统计,可以很好的提高工作效率,同样的,也是使用_Document类的ComputeStatistics方法,实例实现步骤如下:

1)创建一个基于对话框的应用程序,设置对话框标题为“统计Word文档页码”。

2)向对话框中添加一个静态控件、一个列表框控件、一个列表视图控件和两个按钮控件。界面布局如图1所示。



1  界面设计

3)在应用程序类(CCountPageApp)的InitInstance函数中初始化COM环境,代码如下。

::CoInitialize(NULL);

4)按<Ctrl+W>组合键打开类向导,向工程中添加_Application类、Documents类和_Document类。并为控件关联变量,如图2所示。



2  类向导

5)在CCountSpaceDlg类中应用需要使用的头文件,代码如下。

#include "msword9.h"

#include "atlbase.h"

#include <comdef.h>

6)处理“选择目录”按钮的单击事件,在该事件的处理函数中,通过文件打开对话框获得选择的Word文档路径,并将打开文档,计算空格数量,代码如下。

void CCountPageDlg::OnButselect()

{

        CString ReturnPach;                                                                                          //字符串变量

        TCHAR szPath[_MAX_PATH];                          //保存路径变量

        BROWSEINFO bi;                                       // BROWSEINFO结构变量

        bi.hwndOwner    = NULL;                               //HWND句柄

        bi.pidlRoot       = NULL;                               //默认值为NULL

        bi.lpszTitle       = _T("文件浏览对话框");                            //对话框标题

        bi.pszDisplayName = szPath;                              //选择文件夹路径

        bi.ulFlags        = BIF_RETURNONLYFSDIRS;            //标记

        bi.lpfn           = NULL;                              //默认值为NULL

        bi.lParam         = NULL;                              //回调消息

        LPITEMIDLIST pItemIDList = SHBrowseForFolder(&bi);      //显示文件浏览对话框

        if (pItemIDList)

        {

                if(SHGetPathFromIDList(pItemIDList,szPath))                           //判断是否获得文件夹路径

                        ReturnPach = szPath;                                                              //获得文件夹路径

        }

        else

        {

                ReturnPach = "";                                                                             //文件夹路径为空

        }

        m_Path.SetWindowText(ReturnPach);                                          //显示文件夹路径

 

        m_List.ResetContent();                                                                           //清空列表框控件

    CFileFind file;

    if(ReturnPach.Right(1) != "\")

        ReturnPach +="\*.doc";

    else

        ReturnPach +="*.doc";

    BOOL bf;

    bf = file.FindFile(ReturnPach);

    int i=1;

    while (bf)

    {

        bf = file.FindNextFile();

        if (!file.IsDots())

        {

            CString str;

            str.Format("%d",i);

            CString strName = file.GetFileName();

            m_List.AddString(strName);

            i++;

        }

    }

 

}

7)处理“统计页码”按钮的单击事件,在该事件的处理函数中,统计左侧列表中文档的页码,并计算总页码,代码如下。

void CCountPageDlg::OnButcount()

{

        CString strPath,strPage;

        m_Path.GetWindowText(strPath);

        _Application app; 

        Documents docs;

        _Document doc;

        strPath = strPath + "\";

        long sum = 0;

 

        for (int i=0;i<m_List.GetCount();i++)

        {

                CString strFile,file;

                m_List.GetText(i,strFile);

                file = strPath + strFile;

                CComVariant a (_T(file)),b(false),c(0),d(true);

                //初始化连接

                app.CreateDispatch("word.Application");

                docs.AttachDispatch(app.GetDocuments());

                doc.AttachDispatch(docs.Add(&a,&b,&c,&d));

               

                long page = doc.ComputeStatistics(2,&_variant_t(false));

                sum += page;

                strPage.Format("%d",page);

                m_Grid.InsertItem(i,file);

                m_Grid.SetItemText(i,1,strPage);

 

                app.Quit(&_variant_t(false),&_variant_t((long)0),&_variant_t((long)0));

                docs.ReleaseDispatch();

                doc.ReleaseDispatch();

                app.ReleaseDispatch();

        }

        strPage.Format("%d",sum);

        m_Grid.InsertItem(i,"总页码");

        m_Grid.SetItemText(i,1,strPage);

}

运行程序,效果如图3所示。



3  运行结果

 

 

 

 

本帖赏分:0 分   回复数:0 篇     下载附件
修改   删除   引用   回复
该帖暂无回复!
吉林省明日科技有限公司提供技术支持. 
Copyright © 1999-2008 吉林省明日科技有限公司. 吉ICP备10002740
当前时区 GMT+8, 今天是 
刷新页面 - 关于我们 - 设为首页