关于自动生成WORD文档的问题 我想作一个数据库的程序,将数据库中的数据打印到WORD文件中去。请问,怎样让程序自动生成WORD文档,并且将数据库中的数据输出到WORD文档中去?因为我想在打印的数据前加入一些报告信息,所以这样设计。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 possiblesearch q178749 in msdn, and also refer to the followingvoid CMailMergeDlg::OnRun() { //For optional arguments COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR); //Start Word _Application oApp; oApp.CreateDispatch("Word.Application"); if(!oApp) { AfxMessageBox("Cannot start Word."); return; } //Get the Documents collection so that you can add //a new Document for the mail merge "main document" Documents oDocs = oApp.GetDocuments(); _Document oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt); //Get the MailMerge property of the new document MailMerge oMMerge = oDoc.GetMailMerge(); //Set the document type as Form Letters oMMerge.SetMainDocumentType((long)0); //wdFormLetters = 0 //Set up the mail merge data source to the DSN "Northwind" and //an SQL statement CString sSQL= "SELECT * from Table1";// COleVariant ovar_connection("DSN=MS Access Database;DBQ=F:\\MASTERZ\\db2002.mdb;FIL=MS Access;"); COleVariant ovar_connection("table table1"); CString sname="F:\\MASTERZ\\db2002.mdb"; oMMerge.OpenDataSource(sname,//name vOpt, //Format vOpt, //ConfirmConversions vOpt, //ReadOnly vOpt,//LinkToSource vOpt, //AddToRecentFiles vOpt, //PasswordDocument vOpt, //PasswordTemplate vOpt, //Revert vOpt, //WritePasswordDocument vOpt,//WritePasswordTemplate ovar_connection,//Connection vOpt,// vOpt,//COleVariant(sSQL), //SQLStatement vOpt, //SQLStatement1 vOpt, //OpenExclusive vOpt);//SubType //Add the field codes and text to the document Selection oSel = oApp.GetSelection(); Range oRange; MailMergeFields oMMFlds = oMMerge.GetFields(); MailMergeField oMMFld; oRange = oSel.GetRange(); oMMFld = oMMFlds.Add(oRange, "CompanyName"); oSel.TypeParagraph(); oRange = oSel.GetRange(); oMMFld = oMMFlds.Add(oRange, "Address"); oSel.TypeParagraph();// oRange = oSel.GetRange();// oMMFld = oMMFlds.Add(oRange, "City");// oSel.TypeText(", ");// oRange = oSel.GetRange();// oMMFld = oMMFlds.Add(oRange, "Country");// oSel.TypeParagraph();// oSel.TypeParagraph();// oSel.TypeText("Dear ");// oRange = oSel.GetRange();// oMMFld = oMMFlds.Add(oRange, "ContactName");// oSel.TypeText(",");// oSel.TypeParagraph();// oSel.TypeParagraph();// oSel.TypeText("This letter is to inform you...");// oSel.TypeParagraph(); oSel.TypeParagraph(); oSel.TypeText("Sincerely, John Smith"); //Execute the mail merge and then close the //main document without saving changes oMMerge.SetDestination(0); //wdSendToNewDocument = 0 oMMerge.Execute(COleVariant((short)FALSE)); oDoc.Close(COleVariant((short)FALSE), vOpt, vOpt); //Make Word visible oApp.SetVisible(TRUE); } 给你一段MSDN上的源码 COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //Start a new instance of Microsoft Word _Application oWordApp; if (!oWordApp.CreateDispatch("Word.Application", NULL)) { AfxMessageBox("CreateDispatch failed.", MB_OK | MB_SETFOREGROUND); return; } try { //Create a new document Documents oDocs; _Document oDoc; oDocs = oWordApp.GetDocuments(); oDoc = oDocs.Add(vOpt, vOpt); PageSetup mPageSetup; mPageSetup=oDoc.GetPageSetup();//Öмä¼ÓÕâÒ»¾ä£¬Ïȵõ½ÎĵµµÄPagesetup mPageSetup.SetOrientation(1); oDoc.SetPageSetup(mPageSetup); //Add text to the document Selection oSel; oSel = oWordApp.GetSelection(); int i=0; while (i<100) { oSel.TypeText("one"); oSel.TypeParagraph(); oSel.TypeText("two"); oSel.TypeParagraph(); oSel.TypeText("three"); i++; } //Save the document _Document oActiveDoc; oActiveDoc = oWordApp.GetActiveDocument(); oWordApp.SetVisible(TRUE); // TRY THIS: // To see the error handling in action, change // the following line to: // // oActiveDoc.SaveAs(COleVariant((long)5), // COleVariant((short)0), // vFalse, COleVariant(""), vTrue, COleVariant(""), // vFalse, vFalse, vFalse, vFalse, vFalse); // // ALSO TRY THIS: // To see the error handling in action, change the // following line to: // // oActiveDoc.SaveAs(COleVariant("c:\\badpath\\doc1.doc"), // COleVariant((short)0), // vFalse, COleVariant(""), vTrue, COleVariant(""), // vFalse, vFalse, vFalse, vFalse, vFalse); oActiveDoc.SaveAs(COleVariant("c:\\doc1.doc"), COleVariant((short)0), vFalse, COleVariant(""), vTrue, COleVariant(""), vFalse, vFalse, vFalse, vFalse, vFalse); } catch(COleException *e) { LPVOID lpMsg; ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, e->m_sc, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsg, 0, NULL); ::MessageBox(NULL, (LPCTSTR)lpMsg, "COM Error", MB_OK | MB_SETFOREGROUND); ::LocalFree( lpMsg ); } catch(COleDispatchException *e) { char msg[512]; sprintf(msg, "Run-time error '%d':\n\n%s", e->m_scError & 0x0000FFFF, e->m_strDescription); ::MessageBox(NULL, msg, "Server Error", MB_OK | MB_SETFOREGROUND); } //Quit the application oWordApp.Quit(vFalse, vOpt, vOpt); 怎么用指针传输unsigned char类型的数组 求高手给解决一下!很急啊! Expression Blend操作: 使用behavior来控制Storyboard VARCHAR转变成NVARCHAR的SQL语句 如何用VC++实现像优化大师一样的登录窗口功能? 利用createprocess创建新的进程中如何查找新进程的路径? 求助,超难的问题(视频窗口上显示图片....) 新手问题,如果更新控件数据 怎么理解? 在 Windows 窗体中如何获取与本机相连打印机的名称?求解 有谁使用过 thumbs.db 的编程,用于图像显示(这个文件会在图片所在的目录下自动产生) 内存的问题。
search q178749 in msdn, and also refer to the following
void CMailMergeDlg::OnRun()
{
//For optional arguments
COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);
//Start Word
_Application oApp;
oApp.CreateDispatch("Word.Application");
if(!oApp)
{
AfxMessageBox("Cannot start Word.");
return;
}
//Get the Documents collection so that you can add
//a new Document for the mail merge "main document"
Documents oDocs = oApp.GetDocuments();
_Document oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt);
//Get the MailMerge property of the new document
MailMerge oMMerge = oDoc.GetMailMerge();
//Set the document type as Form Letters
oMMerge.SetMainDocumentType((long)0); //wdFormLetters = 0
//Set up the mail merge data source to the DSN "Northwind" and
//an SQL statement
CString sSQL= "SELECT * from Table1";
// COleVariant ovar_connection("DSN=MS Access Database;DBQ=F:\\MASTERZ\\db2002.mdb;FIL=MS Access;");
COleVariant ovar_connection("table table1");
CString sname="F:\\MASTERZ\\db2002.mdb";
oMMerge.OpenDataSource(sname,//name
vOpt, //Format
vOpt, //ConfirmConversions
vOpt, //ReadOnly
vOpt,//LinkToSource
vOpt, //AddToRecentFiles
vOpt, //PasswordDocument
vOpt, //PasswordTemplate
vOpt, //Revert
vOpt, //WritePasswordDocument
vOpt,//WritePasswordTemplate
ovar_connection,//Connection vOpt,//
vOpt,//COleVariant(sSQL), //SQLStatement
vOpt, //SQLStatement1
vOpt, //OpenExclusive
vOpt);//SubType
//Add the field codes and text to the document
Selection oSel = oApp.GetSelection();
Range oRange; MailMergeFields oMMFlds = oMMerge.GetFields();
MailMergeField oMMFld; oRange = oSel.GetRange();
oMMFld = oMMFlds.Add(oRange, "CompanyName");
oSel.TypeParagraph();
oRange = oSel.GetRange();
oMMFld = oMMFlds.Add(oRange, "Address");
oSel.TypeParagraph();// oRange = oSel.GetRange();
// oMMFld = oMMFlds.Add(oRange, "City");
// oSel.TypeText(", ");
// oRange = oSel.GetRange();
// oMMFld = oMMFlds.Add(oRange, "Country");
// oSel.TypeParagraph();
// oSel.TypeParagraph();
// oSel.TypeText("Dear ");
// oRange = oSel.GetRange();
// oMMFld = oMMFlds.Add(oRange, "ContactName");
// oSel.TypeText(",");
// oSel.TypeParagraph();
// oSel.TypeParagraph();
// oSel.TypeText("This letter is to inform you...");
// oSel.TypeParagraph();
oSel.TypeParagraph();
oSel.TypeText("Sincerely, John Smith");
//Execute the mail merge and then close the
//main document without saving changes
oMMerge.SetDestination(0); //wdSendToNewDocument = 0
oMMerge.Execute(COleVariant((short)FALSE));
oDoc.Close(COleVariant((short)FALSE), vOpt, vOpt);
//Make Word visible
oApp.SetVisible(TRUE);
}
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //Start a new instance of Microsoft Word
_Application oWordApp;
if (!oWordApp.CreateDispatch("Word.Application", NULL))
{
AfxMessageBox("CreateDispatch failed.",
MB_OK | MB_SETFOREGROUND);
return;
} try
{ //Create a new document
Documents oDocs;
_Document oDoc;
oDocs = oWordApp.GetDocuments();
oDoc = oDocs.Add(vOpt, vOpt);
PageSetup mPageSetup;
mPageSetup=oDoc.GetPageSetup();//Öмä¼ÓÕâÒ»¾ä£¬Ïȵõ½ÎĵµµÄPagesetup
mPageSetup.SetOrientation(1);
oDoc.SetPageSetup(mPageSetup);
//Add text to the document
Selection oSel;
oSel = oWordApp.GetSelection();
int i=0;
while (i<100)
{
oSel.TypeText("one");
oSel.TypeParagraph();
oSel.TypeText("two");
oSel.TypeParagraph();
oSel.TypeText("three");
i++;
} //Save the document
_Document oActiveDoc;
oActiveDoc = oWordApp.GetActiveDocument();
oWordApp.SetVisible(TRUE);
// TRY THIS:
// To see the error handling in action, change
// the following line to:
//
// oActiveDoc.SaveAs(COleVariant((long)5),
// COleVariant((short)0),
// vFalse, COleVariant(""), vTrue, COleVariant(""),
// vFalse, vFalse, vFalse, vFalse, vFalse);
//
// ALSO TRY THIS:
// To see the error handling in action, change the
// following line to:
//
// oActiveDoc.SaveAs(COleVariant("c:\\badpath\\doc1.doc"),
// COleVariant((short)0),
// vFalse, COleVariant(""), vTrue, COleVariant(""),
// vFalse, vFalse, vFalse, vFalse, vFalse); oActiveDoc.SaveAs(COleVariant("c:\\doc1.doc"),
COleVariant((short)0),
vFalse, COleVariant(""), vTrue, COleVariant(""),
vFalse, vFalse, vFalse, vFalse, vFalse);
}
catch(COleException *e)
{
LPVOID lpMsg;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, e->m_sc,
MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT),(LPTSTR) &lpMsg,
0, NULL);
::MessageBox(NULL, (LPCTSTR)lpMsg, "COM Error",
MB_OK | MB_SETFOREGROUND);
::LocalFree( lpMsg );
} catch(COleDispatchException *e)
{
char msg[512];
sprintf(msg, "Run-time error '%d':\n\n%s",
e->m_scError & 0x0000FFFF, e->m_strDescription);
::MessageBox(NULL, msg, "Server Error",
MB_OK | MB_SETFOREGROUND);
}
//Quit the application
oWordApp.Quit(vFalse, vOpt, vOpt);