VS 2008オペレーションWORD 2007
8054 ワード
シンプルな構成
まずVCの下で簡単なダイアログプログラムを作成し、次にクラスウィザードを開き、右側のAdd Class-』Visual C+>>Type Libの中のMFCクラス」ファイルをクリックし、wordのパッケージクラスライブラリを見つけ、一般的にC:\Program Files(x 86)\Microsoft Office\OFFICE 12\MSWORD.OLB(word 2007).使用するクラスを追加します.
二置換import命令下で使用するヘッダファイルのimportを次の文に置換する
#import "C:\\Program Files (x86)\\Microsoft Office\\OFFICE12\\MSWORD.OLB"no_namespace raw_interfaces_only\rename("FindText","_FindText") \rename("Rectangle","_Rectangle") \rename("ExitWindows","_ExitWindows")
3使用可能なコード
まずVCの下で簡単なダイアログプログラムを作成し、次にクラスウィザードを開き、右側のAdd Class-』Visual C+>>Type Libの中のMFCクラス」ファイルをクリックし、wordのパッケージクラスライブラリを見つけ、一般的にC:\Program Files(x 86)\Microsoft Office\OFFICE 12\MSWORD.OLB(word 2007).使用するクラスを追加します.
二置換import命令下で使用するヘッダファイルのimportを次の文に置換する
#import "C:\\Program Files (x86)\\Microsoft Office\\OFFICE12\\MSWORD.OLB"no_namespace raw_interfaces_only\rename("FindText","_FindText") \rename("Rectangle","_Rectangle") \rename("ExitWindows","_ExitWindows")
3使用可能なコード
// VS2008+office2007
//
#include // VARIANT , CComVariant
#include "CApplication.h"
#include "CDocuments.h"
#include "CDocument0.h"
#include "CRange.h"
#include "CFont0.h"
#include "CParagraphs.h"
#include "CParagraph.h"
#include "CSelection.h"
#include "CTables0.h"
#include "CTable0.h"
#include "CCells.h"
#include "CCell.h"
#include "CBorders.h"
//
if ( !AfxOleInit() )
{
AfxMessageBox(_T(" COM !"));
return ;
}
CApplication WordApp ;
CDocuments Docs ;
CDocument0 Doc ;
// word
if (!WordApp.CreateDispatch(TEXT("Word.Application")) )
{
AfxMessageBox(" WORD !");
return ; // ,
}
// , ,
WordApp.put_Visible(true);
//
CString strFile("");
CComVariant tpl(strFile), NewTemplate(true), DocType(0), Visble;
//
Docs.AttachDispatch(WordApp.get_Documents());
Doc.AttachDispatch(Docs.Add(&tpl,&NewTemplate,&DocType,&Visble));
CFont0 oFont;
CParagraph oPara;
CParagraphs oParas;
CSelection oSel;
CRange oRange;
oSel = WordApp.get_Selection();
// ---------------------------------------
oSel.TypeText(" ");
oSel.TypeParagraph();
// ( )------------------------------------
oSel.TypeText(" ");
oSel.TypeParagraph();
//
CString strParag="";
CString str;
//
str=" G=";
strParag.Append(str);
str.Format("%fkg , ",100.11);
strParag.Append(str);
//
str=" Gf= ";
strParag.Append(str);
str.Format("%fkg , ",200.11);
strParag.Append(str);
//
str=" L= ";
strParag.Append(str);
str.Format("%fm ",300.11);
strParag.Append(str);
oSel.TypeText(strParag);
oSel.TypeParagraph();
// --------------------------------------------------
strParag="";
str=" N= ";
strParag.Append(str);
str.Format("%d , ",400);
strParag.Append(str);
str.Format("( 1 2/ 2 1, I=%d, J=%d),",3,4);
strParag.Append(str);
str.Format(" =%f,",5.11);
strParag.Append(str);
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 5 --------------------------------------------------
strParag="";
str.Format(" E=%f,",5.11);
strParag.Append(str);
str.Format(" I=%f,",5.11);
strParag.Append(str);
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 6 --------------------------------------------------
strParag="";
str.Format(" Lg=%f,",5.11);
strParag.Append(str);
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 7 --------------------------------------------------
strParag="";
str.Format(" Y_1=%f,Y_2=%f,Y_3=%f,",5.11,5.11,5.11);
strParag.Append(str);
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 8 --------------------------------------------------
strParag="";
str.Format(" X1=%f, X2=%f,X3=%f,X4=%f,",5.11,5.11,5.11,5.11);
strParag.Append(str);
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 9 --------------------------------------------------
strParag="";
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 10 --------------------------------------------------
strParag=" :";
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 11 --------------------------------------------------
strParag="";
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 12 --------------------------------------------------
strParag=" :F_1=,F_2=,F_3=,。。。。。。,";
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 13 --------------------------------------------------
strParag=" N1= ,N2= ,N3= ,";
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 14 --------------------------------------------------
strParag=" 、 1 . , , :; , :; , :";
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 15 --------------------------------------------------
strParag=" 1 、 ";
oSel.TypeText(strParag);
oSel.TypeParagraph();
// 16 ----- ------------------
strParag="-----";
oSel.TypeText(strParag);
oSel.TypeParagraph();
oParas.AttachDispatch(Doc.get_Paragraphs());
//
oPara=oParas.Item(1);
oPara.put_Alignment(1);
//
oRange = oPara.get_Range();
oFont.AttachDispatch(oRange.get_Font());
oFont.put_Size(14);
//oFont.put_Color(250);
oFont.put_Bold(1);
oRange.put_Font(oFont);
// 15 ,
oPara=oParas.Item(15);
oPara.put_Alignment(1);
// 16
oPara=oParas.Item(16);
oPara.put_Alignment(0);
oRange = oPara.get_Range();
oFont.AttachDispatch(oRange.get_Font());
oFont.put_Size(12);
//
//
oParas.AttachDispatch(Doc.get_Paragraphs());
oPara=oParas.Item(16);
oRange = oPara.get_Range();
//
COleVariant covTrue((short)TRUE);
COleVariant covFalse((short)FALSE);
CTables0 tabs;
tabs.AttachDispatch(Doc.get_Tables());
CTable0 tab;
tab.AttachDispatch(tabs.Add(oRange,9,4,&covFalse,&covTrue));
CBorders oborders;
oborders.AttachDispatch(tab.get_Borders());
oborders.put_Enable(1);
oRange = tab.get_Range();
CCells oCells;
oCells.AttachDispatch(oRange.get_Cells());
oCells.put_VerticalAlignment(1);
//
CCell oCell;
// -------------------------------
oCell = tab.Cell(1,1);
oRange.AttachDispatch(oCell.get_Range());
oRange.InsertAfter(" \\ ");
for(int i=2;i<=9;i++)
{
oCell = tab.Cell(i,1);
oRange.AttachDispatch(oCell.get_Range());
str.Format("X%d",i-1);
oRange.InsertAfter(str);
}
// -------------------------------
oCell = tab.Cell(1,2);
oRange.AttachDispatch(oCell.get_Range());
oRange.InsertAfter(" ");
for(int i=2;i<=9;i++)
{
oCell = tab.Cell(i,2);
oRange.AttachDispatch(oCell.get_Range());
str.Format("%d",i-1);
oRange.InsertAfter(str);
}
// -------------------------------
oCell = tab.Cell(1,3);
oRange.AttachDispatch(oCell.get_Range());
oRange.InsertAfter(" ");
for(int i=2;i<=9;i++)
{
oCell = tab.Cell(i,3);
oRange.AttachDispatch(oCell.get_Range());
str.Format("%d",i-1);
oRange.InsertAfter(str);
}
// -------------------------------
oCell = tab.Cell(1,4);
oRange.AttachDispatch(oCell.get_Range());
oRange.InsertAfter(" ");
for(int i=2;i<=9;i++)
{
oCell = tab.Cell(i,4);
oRange.AttachDispatch(oCell.get_Range());
str.Format("%d",i-1);
oRange.InsertAfter(str);
}
//
Docs.ReleaseDispatch();
Doc.ReleaseDispatch();
// CComVariant SaveChanges(false), OriginalFormat, RouteDocument;
//WordApp.Quit(&SaveChanges,&OriginalFormat,&RouteDocument );
WordApp.ReleaseDispatch();