新建word文档,插入文本和表格

网上找的源码,新建一个word文档,并插入文本和表格,代码如下:

void CWordtestDlg::OnNewDoc() 
{
    COleVariant vTrue((short)TRUE);
    COleVariant vFalse((short)FALSE);
    COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
   
    //////////////////////////////////////////////////////////////////////////
    //开始一个新的Microsoft Word实例
    _Application oWordApp;
    if (!oWordApp.CreateDispatch("Word.Application", NULL))
    {
        AfxMessageBox("CreateDispatch failed.", MB_OK | MB_SETFOREGROUND);
        return;
    }
    oWordApp.SetVisible(FALSE);


    //////////////////////////////////////////////////////////////////////////
    //创建一个新的word文档
    Documents oDocs = oWordApp.GetDocuments();
    _Document oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt);
    
    
    //把文本添加到word文档
    Selection oSel = oWordApp.GetSelection();
    oSel.TypeText("one");						// 输入文本
    oSel.TypeParagraph();						// 输入段落标记
    oSel.TypeText("two");
    oSel.TypeParagraph();
    oSel.TypeText("three");
	oSel.TypeParagraph();
	oSel.TypeText("four");
    oSel.WholeStory();

    // 设置字体
    _Font font(oSel.GetFont());
    font.SetColor(RGB(255,0,0));
    font.SetSize(14.f);
    oSel.SetFont(font);

    //////////////////////////////////////////////////////////////////////////
    // 表格
    oSel.EndKey(COleVariant((short)wdStory), COleVariant((short)wdMove));
    Range     Rng  = oSel.GetRange();
    Tables    Tbls = oDoc.GetTables(); 
    Table     Tbl  = Tbls.Add(Rng, 5, 5, vOpt, vOpt);

    Borders bords = Tbl.GetBorders();
    bords.SetOutsideLineStyle(wdLineStyleThinThickSmallGap);
    bords.SetOutsideColorIndex(wdBlue);
    bords.SetInsideLineStyle(wdLineStyleDot);
    bords.SetInsideColorIndex(wdGray50);
    oSel.MoveRight(COleVariant((short)1),COleVariant((short)5),COleVariant((short)wdExtend));
    Cells cell = oSel.GetCells();
    cell.Merge();								// 合并单元格
   

//     oSel.MoveDown(COleVariant((short)4),COleVariant((short)1),COleVariant((short)0));
//     oSel.MoveDown(COleVariant((short)5),COleVariant((short)1),COleVariant((short)0));
    oSel.TypeText("123456789");
    oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)wdMove));
    oSel.TypeText("李明");
    oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)wdMove));
    oSel.TypeText("25");
    oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)wdMove));
    oSel.TypeText("技术员");
    oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)wdMove));
    oSel.TypeText("本科");
    oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)wdMove));
    oSel.TypeText("上海市虹口区民主路315号");


    //保存word文档
    _Document oActiveDoc = oWordApp.GetActiveDocument();

    COleVariant FileName(_T("D:\\Hello.doc"));	//文件名
    COleVariant FileFormat((short)wdFormatDocument);
    COleVariant LockComments((short)FALSE);
    COleVariant Password(_T(""));
    COleVariant AddToRecentFiles((short)TRUE);
    COleVariant WritePassword(_T(""));
    COleVariant ReadOnlyRecommended((short)FALSE);
    COleVariant EmbedTrueTypeFonts((short)FALSE);
    COleVariant SaveNativePictureFormat((short)FALSE);
    COleVariant SaveFormsData((short)FALSE);
    COleVariant SaveAsAOCELetter((short)FALSE);

    oActiveDoc.SaveAs(&FileName,&FileFormat,&LockComments,&Password,
        &AddToRecentFiles,&WritePassword,&ReadOnlyRecommended,
        &EmbedTrueTypeFonts,&SaveNativePictureFormat,&SaveFormsData,
		&SaveAsAOCELetter, vFalse, vFalse, vFalse, vFalse, vFalse);

    //退出word应用程序
    oWordApp.Quit(vOpt, vOpt, vOpt);
}
原文地址:https://www.cnblogs.com/joeblackzqq/p/1872182.html