COMVisual Studio 6Visual C++ 7.0Windows 2000Visual C++ 6.0Windows XPMFCIntermediateDevVisual StudioWindowsC++
Demonstration of OLE Automation of MS Word





3.00/5 (8 votes)
Feb 25, 2002
1 min read

146837

23038
An article that demonstrates automation by showing how to split a MS-Word file based on Bookmarks
Introduction
Here is a code sample of OLE Automation of Microsoft Word. The project accepts two folders as a Source path and Destination path. It opens each Document file in the Source Path and Checks for bookmarks which start with 'P', if it finds one, it will create a new file using the bookmarked contents.
Steps:
-
Create a Dialog Project with MFC App Wizard, Tick the Automation support checkbox.
Add 2 push buttons called 'Ellipsis1 and 'Ellipsis2'' and label them
"source..." and 'destination...' (these are the browse buttons). Also
add an 'Automate' button (this will launch the automation).
- Open Class Wizard, Goto Automation tab,
- Select Add Class
- Select From a type Library
- Select the file MSWORD*.OLD, where * can be anything depending on the Office installed on the machine.
- From the List select the classes _
Application
, _Document
,Documents
,Bookmarks
,Bookmark
,Range
andSelection
. - Add two variables m_
SourcePath
& m_DestinationPath
asCString
to the Edit Boxes in the Dialog. - For the Ellipsis1 'click' event add the following code. This code is called when the browse button is clicked and opens a 'Browse' dialog for the source path.
-
For the Ellipsis2 click event add the following Code
void CWord1Dlg::OnEllipsis2() { BROWSEINFO browseInfo; browseInfo.hwndOwner = NULL ; browseInfo.pidlRoot = NULL; browseInfo.lpszTitle = "Enter The Destination Path"; browseInfo.ulFlags = 0 ; browseInfo.lpfn = NULL; browseInfo.lParam = 0; LPITEMIDLIST lpItemIDList; if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo))!= NULL) { SHGetPathFromIDList(lpItemIDList,m_DestinationPath.GetBuffer(256)); UpdateData(FALSE); } }
-
Add the following code to the 'Automate' Button
void CWord1Dlg::OnAutomate() { COleVariant m_True((short)TRUE), m_False((short)FALSE), m_Long((long)DISP_E_PARAMNOTFOUND, VT_ERROR); long startPos,endPos,m_LoopVar; BOOL m_FoundTheFile; CString m_FileName; UpdateData(TRUE); if(m_SourcePath=="" || m_DestinationPath =="") { MessageBox("Either Source Or Destination Path is Invalid", "Easy Split Error",MB_ICONINFORMATION); return; } _Application appObj; if(!appObj.CreateDispatch("Word.Application")) { AfxMessageBox("Coud Not Create The Application Object"); } appObj.SetVisible(FALSE); Documents docsObj(appObj.GetDocuments()); _Document docObj,tempDocObj; CFileFind m_FileFind; m_FoundTheFile = m_FileFind.FindFile(m_SourcePath+"\\*.doc"); while(m_FoundTheFile) { m_FoundTheFile=m_FileFind.FindNextFile(); docObj.AttachDispatch(docsObj.Open(COleVariant(m_FileFind.GetFilePath()), m_False,m_False,m_False,m_Long,m_Long,m_False, m_Long,m_Long,m_Long )); Bookmarks m_Books(docObj.GetBookmarks()); Bookmark m_Book; Selection selObj(appObj.GetSelection()); startPos=0; endPos=0; for(m_LoopVar=1;m_LoopVar<= m_Books.GetCount();m_LoopVar++) { startPos=endPos ; docObj.Activate(); m_Book.AttachDispatch(m_Books.Item(COleVariant(m_LoopVar))); if(m_Book.GetName().GetAt(0)!='P') continue; endPos= m_Book.GetEnd(); selObj.AttachDispatch(appObj.GetSelection()); selObj.SetRange(startPos,endPos); selObj.Copy(); docsObj.Add(m_Long , m_Long ); selObj.AttachDispatch(appObj.GetSelection()); selObj.Paste(); tempDocObj.AttachDispatch(appObj.GetActiveDocument()); m_FileName.Format("%s%d.doc", m_DestinationPath+"\\" + m_FileFind.GetFileTitle(), m_LoopVar); tempDocObj.SaveAs(COleVariant(m_FileName),m_Long,m_False, COleVariant(""), m_True,COleVariant(""), m_False,m_False,m_False,m_False,m_False); selObj.Shrink(); m_Book.DetachDispatch(); } } tempDocObj.Close(m_Long,m_Long,m_Long); docObj.Close(m_Long,m_Long,m_Long); docsObj.Close(m_Long,m_Long,m_Long); appObj.Quit(m_Long,m_Long,m_Long); m_FileFind.Close(); MessageBox("Completed Successfully","Easy Split",MB_ICONINFORMATION); }
void CWord1Dlg::OnEllipsis1() { BROWSEINFO browseInfo; browseInfo.hwndOwner = NULL ; browseInfo.pidlRoot = NULL; browseInfo.lpszTitle = "Enter The Source Path"; browseInfo.ulFlags = 0 ; browseInfo.lpfn = NULL; browseInfo.lParam = 0; LPITEMIDLIST lpItemIDList; if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo))!= NULL) { SHGetPathFromIDList(lpItemIDList,m_SourcePath.GetBuffer(256)); UpdateData(FALSE); } }