Start of Content Area

Procedure documentation Using OfficeControl  Locate the document in its SAP Library structure

This example shows you can use OfficeControl to integrate an Office document in a Web Dynpro application.

Use

OfficeControl is used in a Web Dynpro application that is used for a defined group of users, as the relevant Office program needs to be installed on the client.

Prerequisites

A Web Dynpro project must be installed, together with the required components.

In this example, a file is read with the name msofficeexample.doc and saved to C:\ios. If you want to follow this example subsequently, you should save a file with the same name in this folder.

If you have problems displaying OfficeControl, it might help to check whether ActiveX Control Framework (ACF) has been correctly installed. For details, see SAP Note 846952.  

Procedure

Creating a Context for the File

To integrate an Office document in Web Dynpro, you need a value node and a value attribute. Create a supplyFunction for the value node. This allows you to fill the binary type value attribute with the data from the document that has been read to a byte array.

The following graphic shows the structure of the context:

This graphic is explained in the accompanying text

Source Text of the supplyFunction

In the supplyFunction, enter the following source text with which the file – in this case the Word file msofficeexample.doc – is written to the context.

ISimpleTypeModifiable mod = node.getNodeInfo().getAttribute("data_source").getModifiableSimpleType();

ModifiableBinaryType bin = (ModifiableBinaryType) mod;

bin.setMimeType(new WebResourceType("doc", "application/octet-stream", false));

IPrivateIOSExpertMode.ISrcElement element = wdContext.nodeSrc().createSrcElement();

wdContext.nodeSrc().addElement(element);

element.setData_source(getBytesFromFile("C:\\ios\\msofficeexample.doc"));

Reading the File

Method getBytesFromFile reads the file and converts the data into a byte array:

public byte[] getBytesFromFile(String fileName)

{

File actualfile = new File(fileName);

IWDMessageManager mmger = wdComponentAPI.getMessageManager();

FileInputStream fis = null;

byte[] bytes = null;

try{

   fis = new FileInputStream(actualfile);

   long length = actualfile.length();

   bytes = new byte[(int) length];

   long bytesRead = fis.read(bytes);

   if (bytesRead < length) {

      mmger.reportException("Could not completely read file " + fileName, false);

   }

}

catch (IOException ioe){

   mmger.reportException("Error during read file " + fileName, false);

}

try{

   fis.close();

}

catch (IOException ioe){

   mmger.reportException("Could not close file " + fileName, false);

}

return bytes;

}

 

Properties of UI Element OfficeControl

In the present example, the properties of OfficeControl are as follows:

This graphic is explained in the accompanying text

Using the Methods in the IWDIOSFactory

If you have set the property expertMode in OfficeControl to true, you can use the methods IWDIOSDocument via IWDIOSFactory.

The following methods are available:

     openDocument

     createDocument

     closeDocument

     saveDocument.

 

The following variables need to be declared (in others):

public IWDIOSFactory factoryInstance;

public IWDIOSDocument documentInstance;

public IWDIOSResultNo resultObject;

 

This is an example of the source text to open an existing file.

IWDMessageManager mmger = wdComponentAPI.getMessageManager();

try{

   if (factoryInstance == null){

      IWDAttributeInfo info =
         wdContext.nodeSrc().getNodeInfo().getAttribute(
"data_source");

      factoryInstance =

         WDOfficeControlMethods.getMethodHandlerInstance(info);

   }

   documentInstance = factoryInstance.getDocumentProxy();

   documentInstance.openDocument();

}

catch (WDIOSDocumentException documentException){

         mmger.reportException("could not create file: " +

            documentException.getMessage(), false);

}

catch (WDIOSEnvironmentException envException){

         mmger.reportException("could not create file: " +

            envException.getMessage(), false);

}

catch (WDIOSFactoryException facException){

         mmger.reportException("could not create file: " +

            facException.getMessage(), false);

}

You can use this source text in the same way for other methods, but need to take account of the differences in the exceptions.

 

End of Content Area