Enterprise Resource Planning Portal ERPGenie.COM Enterprise Resource Planning Portal

   Advertise | BLOG

Web ERPGenie.COM

Home | Vote for us |

ERPGenie.COM -> SAP Technical -> ABAP -> Example code -> Desktop Office Integration DOI

1 program z_office_demo message-id demoofficeintegratio. 
2 * this program is able to run desktop applications like Excel, Word, 
3 * PowerPoint, WordPad, Word Pro, Lotus 1-2-3, and Visio either in-place 
4 * (in R/3 screen) or in their own windows. To determine the desktop 
5 * application you have to enter the associated "ProgID" in the entry 
6 * field "Desktop application" (check the Registry Editor to confirm ProgIDs) 
7 * for Word 97: WORD.DOCUMENT.8 
8 * for Excel: EXCEL.SHEET.8 
9 * for PowerPoint: POWERPOINT.SHOW.8
10 * for Word Pro: WORDPRO.DOCUMENT
11 * for Lotus 1-2-3: LOTUS123.WORKBOOK
12 * for Visio: VISIO.DRAWING.5
13 * for WordPad: WORDPAD.DOCUMENT 
14 
15 * start with the only screen 100 
16 set screen 100. 
17 
18 * Control Framework definitions. These definitions have to be included 
19 * for use of controls and SAP DOI makes use of control technology 
20 type-pools: cndp. 
21 include <ctldef>. 
22 
23 * this is the very important INCLUDE. It contains the whole ABAP 
24 * class and interface definitions and implementations of 
25 * SAP Desktop Office Integration. These definitions are global from 
26 * 4.6A on, so you won't need this INCLUDE there. 
27 include officeintegrationinclude. 
28 
29 data: control type ref to i_oi_ole_container_control, 
30 factory type ref to i_oi_document_factory, 
31 document type ref to i_oi_document_proxy. 
32 * the dynpro entry fields 
33 data: application(25) type c value 'Word.Document.8', 
34 inplace(1) type c. 
35 
36 data: stored_application like application, 
37 initialized(1), retcode type t_oi_ret_string, 
38 is_released type i, has_changed type i. 
39 
40 types:row(1024) type c. 
41 * ABAP internal table to store documents of desktop applications 
42 data: doc_table type standard table of row, 
43 doc_size type i, doc_url(256). 
44 
45 * in ABAP you have to write an event handler class to catch events. 
46 * This is ABAP OO technology. SAP DOI just makes use of it to 
47 * catch the event: "user closes desktop application by closing window" 
48 class c_event_handler definition. 
49 public section. 
50 * catch the event: "user closes desktop application by closing window" 
51 class-methods: close_event_handler 
52 for event on_close_document of i_oi_document_proxy 
53 importing document_proxy has_changed. 
54 
55 * for trigger this event you have to write a VB statement 
56 * like ActiveDocument.Container.SendCustomEvent("param1", ...) 
57 * but for now we don't support this (empty implementation) 
58 class-methods: custom_event_handler 
59 for event on_custom_event of i_oi_document_proxy 
60 importing document_proxy event_name param_count 
61 param1 param2 param3. 
62 endclass. 
63 
64 class c_event_handler implementation. 
65 method close_event_handler. 
66 data: answer. 
67 * ask user if document is to be stored 
68 call function 'POPUP_TO_CONFIRM' 
69 exporting 
70 titlebar = 'Office Integration Demo' 
71 text_question = 'Save Document?' 
72 display_cancel_button = ' ' 
73 importing 
74 answer = answer 
75 exceptions 
76 text_not_found = 1 
77 others = 2. 
78 if answer = '1'. 
79 * user said store it ! 
80 perform: store_document. 
81 endif. 
82 perform close_document. 
83 endmethod. 
84 
85 method custom_event_handler. 
86 * for now we don't support this method (empty implementation) 
87 endmethod. 
88 endclass. 
89 
90 *----------------------------------* 
91 * MODULE BEFORE_0100 OUTPUT * 
92 *----------------------------------* 
93 * initializes Control Framework just once * 
94 *----------------------------------* 
95 module before_0100 output. 
96 set pf-status 'MAIN0100'. 
97 set titlebar '001'. 
98 if initialized is initial. 
99 call function 'CONTROL_INIT' 
100 exceptions 
101 control_init_error = 1 
102 others = 2. 
103 if sy-subrc ne 0. message e007. endif. 
104 initialized = 'X'. 
105 endif. 
106 endmodule. 
107 
108 *----------------------------------* 
109 * MODULE USER_COMMAND_0100 INPUT * 
110 *----------------------------------* 
111 * handles all R/3 commands like create, store, reopen * 
112 * as well as the standard exit, return and abort icons * 
113 *----------------------------------* 
114 module user_command_0100 input. 
115 * CONTROL_DISPATCH is in 4.0 and 4.5 necessary to 
116 * dispatch control events to the controls. 
117 * Control events are okcodes, which start with '%' 
118 if sy-ucomm ca '%'. 
119 call function 'CONTROL_DISPATCH' 
120 exporting 
121 fcode = sy-ucomm. 
122 * exceptions 
123 * cb_not_found = 1 
124 * others = 2. 
125 exit. 
126 endif. 
127 
128 case sy-ucomm. 
129 when 'CREATE'. 
130 * start an application and create a new document 
131 * first close current application, if one exists 
132 perform close_document. 
133 * application in its window - factory points to i_oi_document_factory 
134 if factory is initial. 
135 perform create_factory. 
136 endif. 
137 * in-place activation - control pointing to i_oi_ole_container_control 
138 if control is initial. 
139 perform create_container. 
140 endif. 
141 * document pointing to i_oi_document:proxy interface 
142 perform get_document_proxy using application. 
143 * register the event handler method for closing the document window 
144 set handler c_event_handler=>close_event_handler for document. 
145 
146 call method document->create_document 
147 exporting open_inplace = inplace. 
148 call method c_oi_errors=>show_message exporting type = 'E'. 
149 
150 when 'STORE'. 
151 * close document and save it in ABAP table doc_table 
152 perform store_document. 
153 stored_application = application. 
154 perform close_document. 
155 
156 * reopen the document stored in doc_table 
157 when 'REOPEN'. 
158 check doc_size > 0. 
159 perform close_document. 
160 perform get_document_proxy using stored_application. 
161 * register the event handler method for closing the document window 
162 set handler c_event_handler=>close_event_handler for document. 
163 perform open_document_from_table. 
164 
165 when 'EXIT'. 
166 perform close_document. 
167 if not control is initial. 
168 call method control->destroy_control. 
169 free control. 
170 endif. 
171 if not factory is initial. 
172 call method factory->stop_factory. 
173 free factory. 
174 endif. 
175 call function 'CONTROL_EXIT'. 
176 leave program. 
177 endcase. 
178 endmodule. " USER_COMMAND_0100 INPUT 
179 
180 *----------------------------------* 
181 * FORM CREATE_CONTAINER * 
182 *----------------------------------* 
183 form create_container. 
184 * create an i_oi_ole_container_control interface instance 
185 * in case of in-place activation, referenced by CONTROL 
186 call method c_oi_ole_control_creator=>get_ole_container_control 
187 importing control = control 
188 retcode = retcode. 
189 call method c_oi_errors=>show_message exporting type = 'E'. 
190 
191 data: shell_style type i, align type i, inplace_mode type i. 
192 shell_style = ws_visible + ws_child + ws_border + ws_clipchildren. 
193 inplace_mode = control->inplace_mode_enabled + 
194 control->inplace_mode_scroll. 
195 
196 call method control->init_control 
197 exporting r3_application_name = 'R/3 Basis' 
198 inplace_mode = inplace_mode 
199 shell_style = shell_style 
200 register_on_close_event = 'X' 
201 receiving retcode = retcode. 
202 call method c_oi_errors=>show_message exporting type = 'E'. 
203 
204 align = align_at_right + align_at_bottom. 
205 call method control->set_window_properties 
206 exporting top = 5 
207 align = align. 
208 call method c_oi_errors=>show_message exporting type = 'E'. 
209 endform. 
210 
211 *----------------------------------* 
212 * FORM CREATE_FACTORY * 
213 *----------------------------------* 
214 form create_factory. 
215 * create an i_oi_document_factory interface instance 
216 * in case of own application window activation referenced 
217 * FACTORY 
218 call method c_oi_factory_creator=>get_document_factory 
219 exporting factory_type = 'OLE' 
220 importing factory = factory 
221 retcode = retcode. 
222 call method c_oi_errors=>show_message exporting type = 'E'. 
223 call method factory->start_factory 
224 exporting r3_application_name = 'SAP DOI' 
225 register_on_close_event = 'X' 
226 receiving retcode = retcode. 
227 endform. 
228 
229 *----------------------------------* 
230 * FORM STORE_DOCUMENT * 
231 *----------------------------------* 
232 * store document in internal table doc_table 
233 * doc_size has length of table in charackters 
234 *----------------------------------* 
235 form store_document. 
236 check not document is initial. 
237 call method document->is_destroyed 
238 receiving ret_value = is_released. 
239 if is_released is initial. 
240 call method document->close_document 
241 exporting do_save = 'X' 
242 importing has_changed = has_changed. 
243 call method c_oi_errors=>show_message exporting type = 'E'. 
244 if not has_changed is initial. 
245 * document has changed, so save it into ABAP internal table 
246 call function 'SAP_OI_GET_UNIQUE_URL' 
247 importing 
248 unique_url = doc_url 
249 exceptions 
250 others = 0. 
251 call method document->save_document_to_url 
252 exporting url = doc_url 
253 receiving retcode = retcode. 
254 call method c_oi_errors=>show_message exporting type = 'E'. 
255 call function 'DP_GET_STREAM_FROM_URL' 
256 exporting 
257 url = doc_url 
258 importing 
259 size = doc_size 
260 tables 
261 data = doc_table. 
262 * exceptions 
263 * dp_fail = 1 
264 * dp_failed_init = 2 
265 * others = 3. 
266 call method c_oi_errors=>show_message exporting type = 'E'. 
267 endif. 
268 endif. 
269 endform. 
270 
271 *----------------------------------* 
272 * FORM CLOSE_DOCUMENT * 
273 *----------------------------------* 
274 form close_document. 
275 check not document is initial. 
276 call method document->close_document. 
277 call method document->release_document. 
278 free document. 
279 endform. 
280 
281 *----------------------------------* 
282 * FORM GET_DOCUMENT_PROXY * 
283 *----------------------------------* 
284 * will create a reference to the interface i_oi_document_proxy * 
285 * and assign it to DOCUMENT. The document type is determined * 
286 * by the APPLICATION parameter. * 
287 *----------------------------------* 
288 form get_document_proxy using application. 
289 if inplace is initial. 
290 call method factory->get_document_proxy 
291 exporting document_type = application 
292 importing document_proxy = document 
293 retcode = retcode. 
294 else. 
295 call method control->get_document_proxy 
296 exporting document_type = application 
297 importing document_proxy = document 
298 retcode = retcode. 
299 endif. 
300 call method c_oi_errors=>show_message exporting type = 'E'. 
301 endform. 
302 
303 *----------------------------------* 
304 * FORM OPEN_DOCUMENT_FROM_TABLE * 
305 *----------------------------------* 
306 * will open a document, which is stored in the ABAP internal * 
307 * table DOC_TABLE usinf the data provider (DP_CREATE_URL) * 
308 * DOCUMENT points to an i_oi_document_proxy interface. * 
309 *----------------------------------* 
310 form open_document_from_table. 
311 * transport doc_table contents to desktop and get back a key in doc_url 
312 * to reference the table contents 
313 call function 'DP_CREATE_URL' 
314 exporting 
315 type = 'application' 
316 subtype = 'x-oleobject' 
317 size = doc_size 
318 tables 
319 data = doc_table 
320 changing 
321 url = doc_url 
322 exceptions 
323 dp_invalid_parameter = 1 
324 dp_error_put_table = 2 
325 dp_error_general = 3 
326 others = 4. 
327 case sy-subrc. 
328 when 0. 
329 when 1. message e802 raising dp_invalid_parameter. 
330 when 2. message e802 raising dp_error_put_table. 
331 when 3. message e802 raising dp_error_general. 
332 when 4. message e802 raising dp_error_general. 
333 endcase. 
334 if not doc_url is initial. 
335 * now open the document referenced at the frontend by doc_url 
336 call method document->open_document 
337 exporting document_url = doc_url 
338 open_inplace = inplace 
339 receiving retcode = retcode. 
340 call method c_oi_errors=>show_message exporting type = 'E'. 
341 else. 
342 message e010. 
343 endif.
344 endform.

Contact Us | Polls | Add URL | Contribute | About | Privacy | Terms | Feedback | Help!

Message Board | Discussion Forum | BLOG | Consultants: Post your resume | Companies: Advertise on ERPGenie.COM | Post Job