Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making an olecontrol 'read only'

Status
Not open for further replies.

fxsage

Programmer
Sep 9, 2002
21
US
I'm using olecontrols (not bound) on a form to link
to active word .docs and .xls's.

Works fine, except I don't want the user to be able to edit.
Can I make this link a 'read only' link?

Thanks
 
In general, No. Even if you set the document as read-only in Word, that wouldn't prevent the user from editing it.

Is there any special reason for using an OLE control? Maybe you should think about using something like the Word document viewer instead. You could ShellExecute() to it from your application.

Mike


Mike Lewis
Edinburgh, Scotland
 
OK. Thought not but wanted to take advantage of the knowledge base. I'll check on the ShellExecute() idea.

Thanks
 
fxSage

Another option would be to change the file attribute before load it into the OLE control:
Code:
LPARAMETERS  lpFileName 

#DEFINE FILE_ATTRIBUTE_READONLY       1   
#DEFINE FILE_ATTRIBUTE_HIDDEN         2   
#DEFINE FILE_ATTRIBUTE_SYSTEM         4   
#DEFINE FILE_ATTRIBUTE_DIRECTORY     16   
#DEFINE FILE_ATTRIBUTE_ARCHIVE       32   
#DEFINE FILE_ATTRIBUTE_NORMAL       128   
#DEFINE FILE_ATTRIBUTE_TEMPORARY    512   
#DEFINE FILE_ATTRIBUTE_COMPRESSED  2048   
       
DECLARE SHORT SetFileAttributes IN kernel32; 
    STRING lpFileName, INTEGER dwFileAttributes 

DECLARE INTEGER GetFileAttributes IN kernel32 STRING lpFileName 

* read current attributes for this file 
dwFileAttributes = GetFileAttributes (lpFileName) 

IF dwFileAttributes = -1 
* the file does not exist 
    RETURN 
ENDIF 

IF dwFileAttributes > 0 
    * read-only attribute to be set 
    dwFileAttributes = BitOr(dwFileAttributes, FILE_ATTRIBUTE_READONLY) 
         
    * archive attribute to be removed 
    IF BitAnd(dwFileAttributes, FILE_ATTRIBUTE_ARCHIVE) = FILE_ATTRIBUTE_ARCHIVE 
        dwFileAttributes = dwFileAttributes - FILE_ATTRIBUTE_ARCHIVE 
    ENDIF 

    * setting selected attributes 
    = SetFileAttributes (lpFileName, dwFileAttributes) 
ENDIF
This code is available at

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike,

I'm not sure about this, but I don't think your idea of changing the attribute to R/O would necessarily work.

For example, if you set a Word doc to R/O, the OLE control would still let the user edit it. It is only when you come to save the file that you are prevented from doing so.

Of course, that might be what Fxsage wants. And it might work differently with other doc types.

Mike




Mike Lewis
Edinburgh, Scotland
 
Thanks all. Just didn't want users editing the .doc or .xls.
Thakns for the good advice.
 
Dear Fxsage,

I have the same problem before, what i did is just protected the document. So the user even can not copy the contents to other documents.

wrd = CreateObject("Word.Application")
WRD.Selection.TypeText("This document will be protected")
WRD.Selection.TypeText("Try to edit it if you can.. Ajaz")
WRD.selection.homekey(6)
WRD.ActiveDocument.Protect(2,.T.,'0123450')
FN = 'C:\Protected.Doc'
wrd.ActiveDocument.SaveAs(FN)
WRD.ActiveDocument.WritePassword = '*psword*'

I hope this will help you.

Regards,

Ajaz.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top