我有一个文件名列表和一个用于显示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. 关闭已打开的文件
谁来帮帮我......
看一下下面的代码。由于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();
}