//Create a new document Documents oDocs; _Document oDoc; oDocs = oWordApp.GetDocuments(); //Add a new document oDoc = oDocs.Add(vOpt, vOpt); //Note for Word 2000: The Add method has 4 arguments in Word 2000. If //you wrapped the classes from the Word type library (msword9.olb), //modify the Add method to provide 4 optional arguments: // oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt);
Documents oDocs;
_Document oDoc;
oDocs = oWordApp.GetDocuments();
//Add a new document
oDoc = oDocs.Add(vOpt, vOpt);
//Note for Word 2000: The Add method has 4 arguments in Word 2000. If
//you wrapped the classes from the Word type library (msword9.olb),
//modify the Add method to provide 4 optional arguments:
// oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt);
CString FileName,LoadFile,SaveFile,SaveFileName,Temp1;
CHAR FilePath[255];
// BSTR TEMP1;
VARIANT Temp;
COleVariant TEMP2;
CTime NowTime;
hr=CoInitialize(NULL);
// TEMP1="E:\\练习\\OFFICE\\OpenOffice\\12月6日工作日志.doc";
// wcscat((BSTR)TEMP1,OLESTR("\\开发人员工作日志模板.dot"));
Word::_ApplicationPtr MyOffice("Word.Application.9");
::GetCurrentDirectory(255,FilePath);
FileName.Format("%s",FilePath);
LoadFile=FileName+"\\开发人员工作日志模板.dot";
TEMP2=(COleVariant)LoadFile;
Temp=TEMP2.Detach();
if (MyOffice) {
// hr=MyOffice->Documents->Open(&Temp);
hr=MyOffice->Documents->Add(&Temp);
//hr=MyOffice->ActiveDocument->Activate();
//hr=MyOffice->ActiveWindow->put_Visible(VARIANT_BOOL(TRUE)); //显示word窗口
//这中间你可以添加其他你要处理的功能
if(FAILED(hr))
{
::AfxMessageBox("文件打开显示错误");
MyOffice->Quit();
}
NowTime=CTime::GetCurrentTime();
Temp1=NowTime.Format("%m月%d") ;
SaveFileName=Temp1+"日工作日志.doc";
SaveFile=FileName+"\\"+SaveFileName;
TEMP2.Clear();
TEMP2=(COleVariant)SaveFile;
Temp=TEMP2.Detach();
TEMP2.Clear();
hr=MyOffice->ActiveDocument->SaveAs(&Temp);
// hr=MyOffice->Documents->Save(&Temp); //错误原因:保存文件如果是Documents是全保存 所以应该使用ActiveDocument
if(FAILED(hr))
{
::AfxMessageBox("文件保存失败");
MyOffice->ActiveWindow->put_Visible(VARIANT_BOOL(TRUE));
}
::AfxMessageBox("文件保存成功!");
MyOffice->Quit();
}
// MyOffice->Quit();
// MyOffice->Release();
return 0;