原文地址http://blog.sina.com.cn/s/blog_557d25460100guyb.html
一
下面先说明如何在VC6.0中创建 MFC 项目。该示例使 Microsoft Excel 自动运行。您可以将前 4 个步骤用于任何项目,在使用其他应用程序时可修改其余步骤。
以下列表包含 Microsoft Office 2003 应用程序类型库的文件名:
Microsoft Office Access 2003 Msacc.olb
BOOL CAutoProjectApp::InitInstance() { if(!AfxOleInit()) // Your addition starts here { AfxMessageBox("Could not initialize COM dll"); return FALSE; } // End of your addition AfxEnableControlContainer(); . . . }
7.将#include
添加到 AutoProjectDlg.cpp 程序文件开头.
8.将自动化代码添加到 CAutoProjectDlg::OnRun() 中:
void CAutoProjectDlg::OnRun()
{
_Application app; // app is the Excel _Application object
// Start Excel and get Application object...
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't start Excel.");
}
else
{
//Make Excel Visible and display a message
app.SetVisible(TRUE);
AfxMessageBox ("Excel is Running!");
}
}
9.生成并运行项目。结果:单击对话框中的“运行”按钮时,将启动 Microsoft Excel。激活“Auto_Excel”对话框并关闭消息框。CAutoProjectDlg::OnRun() 函数结束时,Microsoft Excel 将退出,因为这时应用程序变量超出了作用域。虽然以上步骤阐述了如何使 Microsoft Excel 自动运行,但您还可以使用同样的方法使其他应用程序自动运行。
二
VC操作Word2003例子
Sample Code:
_Application objWord; //定义Word应用程序对象(Word.application) // Convenient values declared as ColeVariants. COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); // Get the IDispatch pointer and attach it to the objWord object. if (!objWord.CreateDispatch("Word.Application")) { AfxMessageBox("Couldn't get Word object."); return; } objWord.SetVisible(TRUE); //This shows the application. Documents docs(objWord.GetDocuments());//定义Word Documents对象(Word.Documents) _Document testDoc; //定义Word Document对象(Word.Document) testDoc.AttachDispatch(docs.Open( //可看成VB语句set testDoc = Word.documents.Open(…) COleVariant("C://Test.doc",VT_BSTR), covFalse, // Confirm Conversion. covFalse, // ReadOnly. covFalse, // AddToRecentFiles. covOptional, // PasswordDocument. covOptional, // PasswordTemplate. covFalse, // Revert. covOptional, // WritePasswordDocument. covOptional, // WritePasswordTemplate. covOptional) // Format. // Last argument for Word 97 covOptional, // Encoding // New for Word 2000/2002 covTrue, // Visible covOptional, // OpenConflictDocument covOptional, // OpenAndRepair (long)0, // DocumentDirection wdDocumentDirection LeftToRight covOptional // NoEncodingDialog ) // Close Open parameters ); // Close AttachDispatch(?) AfxMessageBox("Now printing 2 copies on the active printer"); testDoc.PrintOut(covFalse, // Background. //可看成VB语句testDoc.PrintOut(…) covOptional, // Append. covOptional, // Range. covOptional, // OutputFileName. covOptional, // From. covOptional, // To. covOptional, // Item. COleVariant((long)2), // Copies. covOptional, // Pages. covOptional, // PageType. covOptional, // PrintToFile. covOptional, // Collate. covOptional, // ActivePrinterMacGX. covOptional // ManualDuplexPrint. covOptional, // PrintZoomColumn New with Word 2002 covOptional, // PrintZoomRow ditto covOptional, // PrintZoomPaperWidth ditto covOptional); // PrintZoomPaperHeight ditto // If you wish to Print Preview the document rather than print it, // you can use the PrintPreview member function instead of the // PrintOut member function: // testDoc[i].PrintPreview. objWord.Quit(covFalse, // SaveChanges. covTrue, // OriginalFormat. covFalse // RouteDocument. ); You may need to modify the code in CAutoProjectDlg::OnRun() to indicate the correct path for your document Test.doc. The document is referenced in the following line: testDoc.AttachDispatch(docs.Open( COleVariant("C://My Docs//Test.doc",VT_BSTR)...
三
下面介绍在VS2005或2008中进行自动化时的注意点: