一 简单配置
首先在VC下建立简单的对话框程序,然后打开类向导,点右边的Add Class-》Visual C++ >>TypeLib中的MFC类 》》文件
,找到word的封装类库,一般在C:\Program Files (x86)\Microsoft Office\OFFICE12\MSWORD.OLB(word2007)。
添加需要使用的类。
二 替换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")
三 可以使用的代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333//下面是可以使用的代码 VS2008+office2007 //相关头文件 #include <atlbase.h> // 为了方便操作 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();
最后
以上就是飘逸花瓣最近收集整理的关于VS2008操作WORD2007的全部内容,更多相关VS2008操作WORD2007内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复