Launching Office applications from Sharepoint
As the title suggests, my last challenge was to launch Office applications from Sharepoint. This new customer wanted to use Sharepoint not only as a portal to find information but also to launch it's applications. I knew that Sharepoint was capable to launch the default Office applications like Word, Excel and Powerpoint. But the question was, how to launch Outlook? I tried everything from dirty javascript code to creating a custom webpart which launches the outlook process, nothing worked. Fortunatly Daniel McPherson was in my msn list and he knew the answer. He said 'try using the outlook:// protocol', i was flabbergasted because it worked immediatly ;) Copy and paste this code into a CEWP webpart and voila, you can launch Office apps from Sharepoint
<a href="javascript:OpenWord()">Word</a> <a href="javascript:OpenExcel()">Excel</a> <a href="javascript:OpenPowerPoint()">Powerpoint</a> <a href="outlook://inbox">Outlook</a> <script> function OpenWord() { var strTemplate = "Word.Document.8"; var strSaveLocation = "http://intranet"; var strProgID = "SharePoint.OpenDocuments"; createNewDocumentWithProgID(strTemplate, makeAbsUrl(strSaveLocation), strProgID, false); } function OpenExcel() { var strTemplate = "Excel.Sheet.8"; var strSaveLocation = "http://intranet"; var strProgID = "SharePoint.OpenDocuments"; createNewDocumentWithProgID(strTemplate, makeAbsUrl(strSaveLocation), strProgID, false); } function OpenPowerPoint() { var strTemplate = "PowerPoint.Show.8"; var strSaveLocation = "http://intranet"; var strProgID = "SharePoint.OpenDocuments"; createNewDocumentWithProgID(strTemplate, makeAbsUrl(strSaveLocation), strProgID, false); } </script>
又看到几篇有关的文章
Office文档在线编辑的实现之一
Office文档在线编辑的实现之二
在安装Office2003以后,有一个ActiveX控件被安装到了系统中,这个控件位于“Program Files\\Microsoft Office\\OFFICE11\\owssupp.dll”。通过这个控件,客户端页面上的JavaScript就可以激活本地的Office软件,来实现打开、编辑Office文档。(另,Office XP应该就已经包含这个ActiveX控件了。)
首先,用Script创建一个本地的对象:
openDocObj = new ActiveXObject(\"SharePoint.OpenDocuments.2\"); // 为了兼容Office XP,可以创建“SharePoint.OpenDocuments.1”
然后,调用openDocObj的相应的方法。比如打开服务器上的一个Office文档:
openDocObj.ViewDocument(\"http://www.abc.com/documents/sample.doc\");
openDocObj对象会根据参数中不同的Office文档类型(.doc、.xls、.ppt)来打开不同的程序(Word、Excel、PowerPoint)。ViewDocument()方法还有一个重载签名,可以让我们手工指定激活哪个程序来打开文档,中国自学编程网,www.zxbc.cn:
openDocObj.ViewDocument(\"http://www.abc.com/documents/sample.doc\", 要激活的程序的ProgID);
那么要打开Office程序在线编辑文件又如何?
openDocObj.EditDocument(\"http://www.abc.com/documents/sample.doc\");
就可以直接激活Word,在Word里面编辑文档,然后直接点击Word里面的保存功能,就可以将文件保存会服务器上了。注意:为了让Word能将编辑后的文档直接保存会服务器,访问Web站点的当前上下文的Windows Identity必须对服务器的相应目录(即“http://www.abc.com/documents”这个虚拟目录所对应的服务器上的物理路径)有相应的写权限,否则保存动作会失败。编辑完成后,EditDocument()会返回一个bool值,来反映编辑操作是否成功。
我们还可以通过打开服务器上的一个文档模版,来创建一个新的文档:
openDocObj.CreateNewDocument(\"http://www.abc.com/documents/sampleTemplate.dot\", \"http://www.abc.com/documents/\");
就可以使用“http://www.abc.com/documents/sampleTemplate.dot”这个模版来创建一个新的文档,默认新文档的保存地点是“http://www.abc.com/documents/”。创建新文档时使用的程序取决于模版文件的类型(比如.dot模版会对应Word)。新文档的保存同样需要注意权限问题。CreateNewDocument()方法同样会返回一个bool值来反映操作是否成功。
CreateNewDocument()方法的第一个参数,除了可以使用一个模版的地址外,还可以直接指定为希望用来创建新文档的客户端程序的ProgID。