Question

I have a list of file names and a composite for displaying Word file. the below code will open the selected file in the screen, but, I want to open the file in read-only mode, please anyone help me

public class openDatafile
{

public void open_file(OleClientSite clientSite, OleFrame frame,String fname,String fpath)
    {
        String fileName=fname;
        String filePath=fpath;
        String fullpath=filePath+"/"+fileName;

            if (fullpath != null) {

                clientSite.dispose();
                clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document",new File(fullpath));
                clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
            }
            frame.redraw();
    }
}

please help me to do following:- 1. to open the file in read only mode 2. to close an opened file

any one please help me......

Was it helpful?

Solution

Have a look at the following code. Since Application.ActiveDocument.ReadOnly is not writable, I use Application.ActiveDocument.Final which works great.

"Returns or sets a Boolean that indicates whether a document is final. Read/write." http://msdn.microsoft.com/en-us/library/office/ff838930(v=office.15).aspx

It is very IMPORTANT that you call this after OleClientSite.doVerb(), otherwise Application.ActiveDocument is not initialized and nothing happens.

/**
 * Sets a boolean that indicates whether a document is final (read only)<br/>
 * http://msdn.microsoft.com/en-us/library/office/ff838930(v=office.15).aspx<br/>
 * <br/>
 * IMPORTANT: Call after OleClientSite.doVerb(), otherwise Application.ActiveDocument is not initialized
 *
 * @param clientSite
 * @param readOnly
 */
public static void setFinal(OleClientSite clientSite, boolean readOnly) {
    OleAutomation oleAutomation = new OleAutomation(clientSite);
    int[] ids = oleAutomation.getIDsOfNames(new String[] { "Application" }); //$NON-NLS-1$
    if (ids != null) {
        Variant variant = oleAutomation.getProperty(ids[0]);
        if (variant != null) {
            OleAutomation application = variant.getAutomation();
            ids = application.getIDsOfNames(new String[] { "ActiveDocument" }); //$NON-NLS-1$
            if (ids != null) {
                variant = application.getProperty(ids[0]);
                if (variant != null) {
                    OleAutomation activeDocument = variant.getAutomation();
                    ids = activeDocument.getIDsOfNames(new String[] { "Final" }); //$NON-NLS-1$
                    if (ids != null) {
                        activeDocument.setProperty(ids[0], new Variant(readOnly));
                    }
                    activeDocument.dispose();
                }
            }
            application.dispose();
        }
    }
    oleAutomation.dispose();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top