Word Process Using OLE Automation

Word Process Using OLE Automation

使用OLE Automation 自动化程序的接口,提取word文本及其格式,word抓图等;

WORD的type library  /Program   Files/Microsoft   Office/Office/MSWORD9.OLB

 

// WORD文本及格式提取

void CWordOffice::Process()
{
    m_ListPage.RemoveAll();
    m_wdDoc.Select();
    Selection Sel = m_wdApp.GetSelection();
    long lCurrentStart = Sel.GetStart();
    long lCurrentEnd = Sel.GetEnd();
    long lDocumentEnd=Sel.GetEnd();
    Range range;
    int mIndex=0,pageNo =0;

    PAGENODE pageNode;
    pageNode.pageNo = pageNo;
    range = m_wdDoc.Range (
                COleVariant(lCurrentStart),COleVariant(lCurrentEnd));
    Paragraphs paragraphs = range.GetParagraphs();

    Paragraph paragraph;
    float fontSize = 12;
    long bold = 0;
    long italic = 0;
    long color    = 0;
    CString str;
    _Font font;
    bool bTitle = false;
    static long WHITE_COLOR = RGB(255,255,255);
    long lParag = paragraphs.GetCount();
    CTextNodeList textNodeList;
    for (int i=1;i<=lParag;i++)
    {
        paragraph = paragraphs.Item(i);
        Range paraRange = paragraph.GetRange();
        CString strText = paraRange.GetText();
        //strText.TrimRight();
        // TODO
        // If remove empty lines
        if (IsEmptyLine(strText) && m_nFilterEmptyLine)
            continue;
        strText.Replace(_T("/r/n"),_T("/n"));
        strText.Replace(_T("/r"),_T("/n"));
        if (strText.GetLength()-1!=strText.ReverseFind(_T('/n')))
        {
            strText += _T('/n');
        }

        font = paraRange.GetFont();   
        // font size
        float size = font.GetSize();
        // font bold
        bold    = font.GetBold();
        italic    = font.GetItalic();
        color    = font.GetColor();
        bold    = bold ? 1 : bold;
        italic    = italic ? 1 : italic;

 

        // if there're more than 1 font, 9999 returned.
        if (font.m_lpDispatch && size <1000)
            fontSize = size;
        font.ReleaseDispatch();
        paraRange.ReleaseDispatch();
        paragraph.ReleaseDispatch();

        if (m_nFilterWhiteChar && color==WHITE_COLOR)
            continue;

        TEXTNODE *lastNode = NULL;
        if (textNodeList.GetSize()>0)
        {
            lastNode = &textNodeList[textNodeList.GetSize()-1];
        }

        bool bCat = false;
        if (lastNode)
        {
            bCat = CatText (
                lastNode->text,lastNode->size, lastNode->bold,
                lastNode->italic,strText,fontSize,bold,italic,m_nCatFlag,
                lastNode->text_sec
                );
            if (bCat)
            {
                ++lastNode->num;
                lastNode->text_sec = strText;
            }
        }

        if ( !lastNode || !bCat)
        {
            TEXTNODE textNode;
            textNode.text = strText;
            textNode.type = NODETYPE_CONTENT;
            textNode.size = size;
            textNode.bold = bold;
            textNode.italic = italic;
            textNodeList.Add(textNode);
        }
    }
    FilterArticle(&pageNode.articleList,&textNodeList);

    paragraphs.ReleaseDispatch();
    range.ReleaseDispatch();

    m_ListPage.Add(pageNode);    // Add PageNode to Page List
    WriteArticleToXML();
}

 

// 抓图
//-------------------------------------------------
    range.CopyAsPicture();
     
    ::OpenClipboard(NULL); //打开剪贴板
    HANDLE hClip=::GetClipboardData(CF_ENHMETAFILE);//图元文件
    HENHMETAFILE hEnhMetaFile=(HENHMETAFILE)hClip;
    CString strN="";
    strN.Format("-w%d",mIndex++);
    HENHMETAFILE hMetaFile=CopyEnhMetaFile(hEnhMetaFile,(LPCTSTR)(strN+".emf"));
    DeleteEnhMetaFile(hMetaFile);
    EmptyClipboard();//清空剪贴板
    CloseClipboard();//关闭剪贴板
//-------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值