?
Solved

How Do I open doc, xls, ppt as read-only using Javascript

Posted on 2008-02-04
10
Medium Priority
?
4,372 Views
Last Modified: 2012-08-13
I am creating a server side application using VB.Net.  I can succesfully open doc, xls, and ppt using the posted code below.  This is fine when the application is accessed by the author creating the doc, xls, or ppt.  How do I add in a read-only attribute for other users?  Note I already have the appropriate code to catch the author/other users, I just need to know if it is possible to open the files in read-only through javascript.

If dr("T_Path").ToString.EndsWith(".xls") Then
                Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
                Response.Write("var w = new ActiveXObject('Excel.Application');" & vbCrLf)
                Response.Write("var obj;" & vbCrLf)
                Response.Write("w.Visible=true;" & vbCrLf)
                Response.Write("obj=w.Workbooks.Open(""" & dr("T_Path").ToString & """)" & vbCrLf)
                Response.Write("</script>")
            End If
            If dr("T_Path").ToString.EndsWith(".doc") Then
                Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
                Response.Write("var w = new ActiveXObject('Word.Application');" & vbCrLf)
                Response.Write("var docText;" & vbCrLf)
                Response.Write("var obj;" & vbCrLf)
                Response.Write("w.Visible=true;" & vbCrLf)
                Response.Write("obj=w.Documents.Open(""" & dr("T_Path").ToString & """)" & vbCrLf)
                Response.Write("</script>")
            End If
            If dr("T_Path").ToString.EndsWith(".ppt") Then
                Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
                Response.Write("var w = new ActiveXObject('PowerPoint.Application');" & vbCrLf)
                Response.Write("w.Visible=true;" & vbCrLf)
                Response.Write("obj=w.Presentations.Open(""" & dr("T_Path").ToString & """)" & vbCrLf)
                Response.Write("</script>")

Open in new window

0
Comment
Question by:Black_Flag
  • 6
  • 3
10 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20821224
These say no

http://p2p.wrox.com/topic.asp?TOPIC_ID=43487

These say yes:

http://www.codeprof.com/dev-archive/209/10-32-2092996.shtm

Open(FileName:="C:\Test.doc", ReadOnly:=True)
0
 
LVL 7

Accepted Solution

by:
-sg- earned 336 total points
ID: 20821266
First try changing this line:

Response.Write("obj=w.Workbooks.Open(""" & dr("T_Path").ToString & """)" & vbCrLf)

to

Response.Write("obj=w.Workbooks.Open(""" & dr("T_Path").ToString & """, null, true);" & vbCrLf)

If that doesnt work then try changing it to:

Response.Write("obj=w.Workbooks.Open(""" & dr("T_Path").ToString & """, null, false, null, null, null, true);" & vbCrLf)

Those extra parameters of the Open method control things like read-only mode.

If they dont work then I'm out of ideas for now.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20821273
so try

<script type="text/javascript">
<%
Response.Write("var path =  '"& dr("T_Path").ToString & "';" & vbCrLf)
%>
var appls = { xls: "Excel.Application", doc: "Word.Application", ppt: "PowerPoint.Application"}
var colls = { xls: "Workbooks", doc: "Documents", ppt: "Presentations"}
var ext = (path)?path.substring(path.lastIndexOf('.')+1):"";
if (ext && appls[ext]) {
  var obj;
  var w = new ActiveXObject(appls[ext]);
  w.Visible=true;
  obj=w[colls[ext]].Open(path,"ReadOnly=true");
}
</script>

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20821316
or if -sg- has the correct syntax,

change
 obj=w[colls[ext]].Open(path,"ReadOnly=true");
to
 obj=w[colls[ext]].Open(path,null,true);
0
 
LVL 1

Author Comment

by:Black_Flag
ID: 20823115
-sq- The following opened .doc and .xls in read only:

Response.Write("obj=w.Workbooks.Open(""" & dr("T_Path").ToString & """, null, true);" & vbCrLf)

However it didn't work for the .ppt files.  I also tried your other example and that didn't work either.
Microsoft Power Point opens, but the actual presentation does not open in read only:

Response.Write("obj=w.Presentations.Open(""" & dr("T_Path").ToString & """,null,true);" & vbCrLf)

When I take out the [,null, true] it opens but with read/write permissions.

Any ideas for .ppt files?

mplungjan - I am going to try your code example for the ppt files to see if it works.  I will respond back with what I get.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20823232
If it does not work using -sg-'s suggestions I am not sure my suggestion would be any better...

I see here that the frist parameter is readonly flag, so perhaps

 obj=w[colls[ext]].Open(path,true);

In any case here it is:
<script type="text/javascript">
<%
Response.Write("var path =  '"& dr("T_Path").ToString & "';" & vbCrLf)
%>
var appls = { xls: "Excel.Application", doc: "Word.Application", ppt: "PowerPoint.Application"}
var colls = { xls: "Workbooks", doc: "Documents", ppt: "Presentations"}
var ext = (path)?path.substring(path.lastIndexOf('.')+1):"";
if (ext && appls[ext]) {
  var obj;
  var w = new ActiveXObject(appls[ext]);
  w.Visible=true;
  obj=w[colls[ext]].Open(path,true);
}
</script>

Open in new window

0
 
LVL 1

Author Comment

by:Black_Flag
ID: 20823726
mplungjan - i tried that and it didnt work for my .ppt files.

Any other ideas?
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 164 total points
ID: 20823781
I really looked. It is impossible to find the proper syntax for the Presentations.open but I saw false, false, true and such so perhaps you can just try adding parms
0
 
LVL 1

Author Comment

by:Black_Flag
ID: 20823899
mplungjan - The following worked for .ppt files - used false instead of null like you said-

Response.Write("obj=w.Presentations.Open(""" & dr("T_Path").ToString & """,false,true);" & vbCrLf)

Thank you both for your help.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20825919
So you did not feel for using the smaller script I made for you?
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question