vc操作word的简单例子

void CMyDialog::OnBnClickedOk()
{



if(IDOK!=AfxMessageBox(_T("看好了,就要新建一个文档了")))
return;



CApplication app;
app.CreateDispatch(_T("Word.Application"));
app.put_Visible(TRUE);



CDocuments docs=app.get_Documents();


CComVariant Template(_T("")); //为了简单,没有使用WORD的文档模板
CComVariant NewTemplate(false),DocumentType(0),Visible;
docs.Add(&Template,&NewTemplate,&DocumentType,&Visible);


CSelection sel=app.get_Selection();

sel.TypeText(_T("我是被写入的句子!"));




CDocument0 doc=app.get_ActiveDocument(); //得到ActiveDocument
CComVariant FileName(_T("c:\\Hello.doc")); //文件名
CComVariant FileFormat(0); //重点,看下面的说明
CComVariant LockComments(false),Password(_T(""));
CComVariant AddToRecentFiles(true),WritePassword(_T(""));
CComVariant ReadOnlyRecommended(true),EmbedTrueTypeFonts(false);
CComVariant SaveNativePictureFormat(false),SaveFormsData(false);
CComVariant SaveAsAOCELetter(false);
 


CComVariant Encoding(msoEncodingUSASCII);
CComVariant InsertLineBreaks(false);
CComVariant AllowSubstitutions(false);
CComVariant LineEnding(wdCRLF );
CComVariant AddBiDiMarks(false );


 
/*doc.SaveAs( &FileName, &FileFormat, &LockComments, &Password, \
  &AddToRecentFiles, &WritePassword, &ReadOnlyRecommended, \
  &EmbedTrueTypeFonts, &SaveNativePictureFormat,&SaveFormsData,\
 &SaveAsAOCELetter, &Encoding, &InsertLineBreaks, &AllowSubstitutions, \
  &LineEnding,&AddBiDiMarks);

*/


COleVariant opt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);


doc.SaveAs( &FileName, &FileFormat,opt , opt,\
opt, opt,opt , \
opt, opt,opt,\
opt, opt,opt ,opt, \
opt,opt);




sel.ReleaseDispatch();
doc.ReleaseDispatch();
docs.ReleaseDispatch();


CComVariant SaveChanges(false),OriginalFormat,RouteDocument;


app.Quit(&SaveChanges,&OriginalFormat,&RouteDocument);



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值