如何以只读模式将word文档打开到SWT shell中

时间:2014-03-26 07:43:59

标签: java file swt

我有一个文件名列表和一个用于显示Word文件的复合词。 以下代码将在屏幕中打开所选文件, 但是,我想以只读模式打开文件, 请任何人帮助我

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();
    }
}

请帮我做以下事项: - 1.以只读模式打开文件 2.关闭已打开的文件

任何人请帮帮我......

1 个答案:

答案 0 :(得分:1)

查看以下代码。 由于Application.ActiveDocument.ReadOnly不可写,我使用Application.ActiveDocument.Final效果很好。

“返回或设置一个布尔值,指示文档是否为final。读/写。” http://msdn.microsoft.com/en-us/library/office/ff838930(v=office.15).aspx

OleClientSite.doVerb()之后调用此内容非常重要,否则Application.ActiveDocument未初始化且没有任何反应。

/**
 * 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();
}