Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Opening word document from a template with fields that can be populated and retrieved

rinrosglo
Explorer

Hi,

We have a requirement where the user should be able to open a word document from an SAP transaction with a pre defined template. Some fields should be pre-populated from the ABAP program and the others the user will enter. Now I have an issue with defining these fields in the template and retrieving the data back to the ABAP program.

Below are the steps that I followed.

  • Uploaded a template word document with placeholders (eg: soldto_num) in the BDN using tcode OAER.
  • I have used I_OI_DOCUMENT_PROXY for office integration and CL_BDS_DOCUMENT_SET for retrieving the template. (SAP demo programs SAPRDEMO_DOI_BDS and SAPRDEMO_FORM_INTERFACE used as reference)
  • Now using the word processor interface I am able replace the text 'soldto_num' with the value in lv_soldto.
  • The word document is successfully displayed in place with the template and runtime values from the program.

The problem is when I try to retrieve data from the document to the program. Need help in understanding how we define fields in the template and read it in our programs. I have tried using the form interface (I_OI_FORM) and link server (I_OI_LINK_SERVER) but in either case I am not able to define the fields and transfer data.

Any pointers on this will be really helpful.

Thanks in Advance.

Rincy

6 REPLIES 6

Sandra_Rossi
Active Contributor
0 Kudos

I don't know much about I_OI_FORM, but for information the templates are used in the Business Workflow component ("document from template") and maybe you can find some hints in the function module SWUO_OPEN_DOCUMENT_API which calls OPEN > FILL_LINKITEMS_FROM_WF > GET_LINK_ITEMS > P_GET_FORM_DATA, that gets the fields from the template. I guess it should use GET_DATA from I_OI_FORM, I don't know why it doesn't work for you (did you flush?)

Concerning I_OI_LINK_SERVER, if I have understood well, it's only to improve performance, by downloading something once to the laptop, and reuse it several times with DOI.

EDIT: SAP Library: The Form Interface saying "The data table is passed separately from the link server and table collection using the SAP Data Provider."

rinrosglo
Explorer
0 Kudos

Hi Sandra,

Thank you for your reply.

Regarding the GET_DATA method of I_OI_FORM, in my program if I use the sample template provided in SAPRDEMO_FORM_INTERFACE 'FORMEXAMPLE', the GET_DATA method returns the fields. But if I use my custom template uploaded similarly in BDS, the GET_DATA method does not return the fields.

So I am assuming I am missing something while building the template. (note: the display of the custom template works fine though). Any idea how to define the fields in the word template like context control or something. Is there any other step that I am missing?

Thanks.

Rincy.

Sandra_Rossi
Active Contributor

To insert your fields manually in MS Word > Developer tab > Controls > Legacy form > Text field, Check box, Drop down list.

Right-click on a given field > Properties, to change the default value.

The property fields are to be defined first in File > Info > Properties > Advanced properties > Custom tab, then to be inserted via menu Insert > Text > Quick parts > Fields > DocProperty.

For information, I have just tested ADD_FIELD of I_OI_FORM for properties, it works only if the property is already set in the document (see above), and impossible to set an initial value for all types of fields. The other commands demonstrated in SAPRDEMO_FORM_INTERFACE work, but it seems only in this demo configuration, because using SET_DATA before the document is displayed seems to not work (*).

Test with Office 365.

(*) EDIT right after Rincy comment Nov 1st : my comment about SET_DATA not working is that I tried to set a value for a text field/checkbox I have added using ADD_FIELD (i.e. the MS Word document was created from scratch, i.e. not loaded from an existing document with a field added manually, i.e. probably the reason is because of ADD_FIELD not working well).

rinrosglo
Explorer

Hi Sandra,

Thank you very much for your response. It really helped.

The property fields are to be defined first in File > Info > Properties > Advanced properties > Custom tab, then to be inserted via menu Insert > Text > Quick parts > Fields > DocProperty.

This is what I was missing. Few other things that I noticed are:

  • Set_data works to update the runtime values even before the document is displayed the first time.
  • The property fields cannot be updated in the document and data cannot be fetched using get_data
  • When using text field, checkbox etc., the content control fields have to be created, then while creating the property in file->info->advanced properties->custom tab, the link to content checkbox should be selected and the corresponding content field (bookmark) should be selected. Now these fields can be updated in the displayed doc and the data can be retrieved in the program using get_data.
  • The protect method with the protect parameter allows the document to be editable or non-editable. In protected mode, only the content control fields are available for editing. This also prevents the user from deleting the fields.

Once again thanks for your valuable inputs.

rinrosglo
Explorer
0 Kudos

Hi,

I have a couple of questions in continuation to the above.

  1. Under which Application class and Class type is it best to store the default template? Currently I am using SOFFICEINTEGRATION OT for the default template (as in the SAP demo programs) and once changes are done the document is saved in the sales order business object (VBAK BO).
  2. Later on if the template is to be changed is it possible to export, make changes and import the document directly in production or should it be changed in Dev and then transported?

I'm trying to explore the best ways of maintaining the template.

Thanks in advance,

Rincy

Sandra_Rossi
Active Contributor
0 Kudos

Hi. 1. "best" depends on context, what is important for your company, etc. If BDS (your current solution) works, then it's fine.

2. Depends on the strategy of your company. I can't answer for them.