2014-03-26 79 views
2

我有一個文件名和用於顯示Word文件的組合列表。 下面的代碼將打開所選的文件在屏幕上, 但是,我想以只讀方式打開文件, 請人幫我如何以只讀模式打開SWT shell的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

看一看下面的代碼。 既然Application.ActiveDocument.ReadOnly是不可寫的,我使用Application.ActiveDocument.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(); 
}