本文主要是介绍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();//关闭剪贴板
//-------------------------------------------------
这篇关于Word Process Using OLE Automation的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!