01.
02.
03.
04.
#include "stdafx.h"
05.
#include "MSWordConsole.h"
06.
#include "Capplication.h"
07.
#include "CDocument0.h"
08.
#include "CDocuments.h"
09.
#include "CSelection.h"
10.
#include "CFind.h"
11.
#include "CWords.h"
12.
#ifdef _DEBUG
13.
#define new DEBUG_NEW
14.
#endif
15.
16.
17.
18.
CWinApp theApp;
19.
20.
using
namespace
std;
21.
22.
int
_tmain(
int
argc, TCHAR* argv[], TCHAR* envp[])
23.
{
24.
int
nRetCode = 0;
25.
COleVariant m_True((
short
)TRUE), m_False((
short
)FALSE),
26.
m_Long((
long
)DISP_E_PARAMNOTFOUND, VT_ERROR);
27.
28.
if
(!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
29.
{
30.
31.
_tprintf(_T(
"Fatal Error: MFC initialization failed\n"
));
32.
nRetCode = 1;
33.
}
34.
else
35.
{
36.
CApplication oWordApp;
37.
CoInitialize(0);
38.
if
(!oWordApp.CreateDispatch (
"Word.Application"
, NULL))
39.
{
40.
AfxMessageBox(
"CreateDispatch failed."
, MB_OK | MB_SETFOREGROUND);
41.
}
42.
43.
44.
45.
46.
47.
48.
49.
50.
CDocuments oDocs;
51.
CDocument0 oDoc;
52.
oDocs = oWordApp.get_Documents();
53.
oDoc = oDocs.Add(m_Long, m_Long, m_Long, m_Long);
54.
55.
56.
57.
58.
59.
60.
CSelection oSel;
61.
oSel = oWordApp.get_Selection();
62.
oSel.TypeText(
"one"
);
63.
oSel.TypeParagraph();
64.
oSel.TypeText(
"two"
);
65.
oSel.TypeParagraph();
66.
oSel.TypeText(
"three"
);
67.
68.
69.
CFind oFind;
70.
oFind = oWordApp.get_Selection();
71.
int
intFound = 0;
72.
73.
intFound++;
74.
oFind.Execute(COleVariant(
"one"
), m_False, m_True, m_False,
75.
m_False, m_False, m_True, COleVariant((
short
)1), m_False,
76.
COleVariant(
"aaaa"
), COleVariant((
short
)2),
77.
m_False, m_False, m_False, m_False);
78.
79.
80.
81.
CDocument0 oActiveDoc;
82.
oActiveDoc = oWordApp.get_ActiveDocument();
83.
oActiveDoc.SaveAs(COleVariant(
"c:\\SampleDoc.doc"
),
84.
COleVariant((
short
)0),
85.
m_False, COleVariant(
""
), m_True, COleVariant(
""
),
86.
m_False, m_False, m_False, m_False, m_False,
87.
m_False, m_False, m_False, m_False, m_False);
88.
89.
oWordApp.Quit(m_Long,m_Long,m_Long);
90.
}
91.
92.
return
nRetCode;
93.
}