执行到CreateDispatch(_T("Word.Application")函数就推出程序了,调用createdispatch函数失败
,在InitInstance()函数中已经执行初始化,代码如下if(CoInitialize(NULL)!=S_OK) { AfxMessageBox("初始化COM组件失败!"); return FALSE; }
执行文件中代码如下:
_Application wordApp;
Documents wordDocs;
_Document wordDoc;
Selection wordSelection;
Range wordRange;
Tables wordTables;
Table wordTable;
Cell wordCell;
Cells wordCells;
//Paragraphs wordParagraphs;
//Paragraph wordParagraph;
_Font wordFont;
Shading wordShading;
int nRow=0;
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CComVariant defaultBehavior(1),AutoFitBehavior(0);
//创建word application实例
if (!wordApp.CreateDispatch(_T("Word.Application")))
{
AfxMessageBox("Word CreateDispatch Failed!");
return FALSE;
}
/* if (!wordApp.CreateDispatch(("word.Application")))
{
AfxMessageBox("Word CreateDispatch Failed!");
return false;
} */
执行到此处是直接退出程序了
wordApp.SetVisible(TRUE);//make visible
wordApp.Activate();//激活
wordDocs=wordApp.GetDocuments();//get documents object
CString strDocTemplate;
CString strPath;
char szFullPath[_MAX_PATH];
::GetModuleFileName(NULL,szFullPath,_MAX_PATH);
strDocTemplate.Format("%s",szFullPath);
int nPos=strDocTemplate.ReverseFind(' ');
strDocTemplate=strDocTemplate.Left(nPos);
strPath=strDocTemplate;
TRACE1("%s\n",strDocTemplate);
// strDocTemplate+="""Template""Report.doc";
// wordDoc=wordDocs.Open(COleVariant(strDocTemplate),&vOpt,&vOpt,&vOpt,&vOpt,
// &vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vTrue,&vOpt,&vOpt,&vOpt,&vOpt);
CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
wordDoc=wordDocs.Add(&tpl,&NewTemplate,&DocType,&Visble);
wordSelection=wordApp.GetSelection();
//wordTables.AttachDispatch(wordDoc.GetTables());
wordTables=wordDoc.GetTables();
//wordParagraphs = wordDoc.GetParagraphs();
//wordParagraph=wordParagraphs.GetLast();
//wordRange=wordParagraph.GetRange();
//wordSelection.EndKey(COleVariant((short)6),COleVariant((short)0));
//move insertion point to the end of the story
/*
wdUnits:
wdCharacter=1
wdWord=2
wdSentence=3
wdParagraph=4
wdSection=8
wdStory=6
wdCell=12
wdColumn=9
wdRow=10
wdTable=15
wdMovementType:
wdMove=0
wdExtend=1
*/
//move to end of story
wordSelection.EndOf(COleVariant((short)6),COleVariant((short)0));
//1.1 RxLev Full
wordSelection.TypeText("1. 统计报告");
wordSelection.HomeKey(&CComVariant(5),&CComVariant(1));
//Format the line with selection
wordFont = wordSelection.GetFont();
wordFont.SetBold(9999998);//wdToggle
wordSelection.EndOf(&CComVariant(5),&CComVariant(0));
wordSelection.TypeParagraph();
wordSelection.TypeText("(1.1). 分段统计");
wordSelection.TypeParagraph();
wordFont.SetBold(9999998);//wdToggle
wordRange=wordSelection.GetRange();
//add table
//nRow=m_nRange1+1;
wordTable=wordTables.Add(wordRange,5/*row*/,4/*column*/,&defaultBehavior,&AutoFitBehavior);
wordRange=wordTable.GetRange();
//wordRange.MoveEnd(COleVariant((short)15),COleVariant((short)1));
//wordRange.Select();
//move end of table
//wordSelection.EndOf(COleVariant((short)15),COleVariant((short)0));
//insert rows
//wordSelection.InsertRowsBelow(COleVariant((short)5));
//选择第一个单元,进而选择第一行进行格式化
wordCell=wordTable.Cell(1,1);
wordCell.Select();
//select the row with current selection
wordSelection.EndKey(&CComVariant(10),&CComVariant(1));
//Format the row with selection
//wordFont = wordSelection.GetFont();
wordFont.SetBold(9999998);//wdToggle
wordCells=wordSelection.GetCells();
wordShading = wordCells.GetShading();
wordShading.SetTexture(0);
wordShading.SetBackgroundPatternColor(14737632);//15987699 14737632 adColorBlue
wordShading.SetForegroundPatternColor(-16777216);//-16777216 wdColorAutomatic
//move to end of table
//wordSelection.EndOf(COleVariant((short)15),COleVariant((short)0));
//wordParagraph=wordParagraphs.GetLast();
//wordRange=wordParagraph.GetRange();
//wordRange.MoveEnd(COleVariant((short)4),COleVariant((short)1));
//wordRange.SetText("Test");
//wordSelection=wordApp.GetSelection();
//wordSelection.MoveEnd(COleVariant((short)6),COleVariant((short)1));
wordCell=wordTable.Cell(1,1);
wordCell.Select();
wordSelection.TypeText("统计项目");
wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
wordSelection.TypeText("采样");
wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
wordSelection.TypeText("百分比");
wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
wordSelection.TypeText("累计百分比");
//
//
wordSelection.EndOf(COleVariant((short)6),COleVariant((short)0));
wordSelection.TypeParagraph();
wordSelection.TypeText("(1.2). 分段统计");
wordSelection.HomeKey(&CComVariant(5),&CComVariant(1));
//Format the line with selection
wordFont = wordSelection.GetFont();
wordFont.SetBold(9999998);//wdToggle
wordSelection.EndOf(&CComVariant(5),&CComVariant(0));
wordSelection.TypeParagraph();
wordFont.SetBold(9999998);//wdToggle
wordRange=wordSelection.GetRange();
//add table
//nRow=m_nRange1+1;
wordTable=wordTables.Add(wordRange,5/*row*/,4/*column*/,&defaultBehavior,&AutoFitBehavior);
wordRange=wordTable.GetRange();
//选择第一个单元,进而选择第一行进行格式化
wordCell=wordTable.Cell(1,1);
wordCell.Select();
//select the row with current selection
wordSelection.EndKey(&CComVariant(10),&CComVariant(1));
//Format the row with selection
wordFont = wordSelection.GetFont();
wordFont.SetBold(9999998);
wordCells=wordSelection.GetCells();
wordShading = wordCells.GetShading();
wordShading.SetTexture(0);
wordShading.SetBackgroundPatternColor(14737632);//15987699
wordShading.SetForegroundPatternColor(-16777216);
wordCell=wordTable.Cell(1,1);
wordCell.Select();
wordSelection.TypeText("Range");
wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
wordSelection.TypeText("Samples");
wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
wordSelection.TypeText("Percentage");
wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
wordSelection.TypeText("Calculation");
//
//Save document as report
SYSTEMTIME sysTime;
GetLocalTime(&sysTime);
CString strReport;
strReport.Format("%d年-%d月-%d日 %d-%d-%d 报告",sysTime.wYear,sysTime.wMonth,
sysTime.wDay,sysTime.wHour,sysTime.wMinute,sysTime.wSecond);
strReport=strPath+""""+strReport;
wordDoc.SaveAs(COleVariant(strReport),COleVariant((short)0),vOpt,vOpt,vOpt,vOpt,vOpt,vOpt
,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
//Release com
wordFont.ReleaseDispatch();
wordCells.ReleaseDispatch();
wordShading.ReleaseDispatch();
wordTable.ReleaseDispatch();
wordTables.ReleaseDispatch();
wordRange.ReleaseDispatch();
wordSelection.ReleaseDispatch();
wordDoc.RecheckSmartTags();
wordDocs.ReleaseDispatch();
wordApp.ReleaseDispatch();
return TRUE;
你电脑中安装Office了没有,是不是完整版的还是所谓绿色精简版的。使用的操作系统是不是山寨盗版。
只有完整版的Office,才能保证Office VBA COM对象可以被正常调用。
完整版的office,电脑系统不是原版的