Section 6.8.Program with Permissions


6.8. Program with Permissions

Microsoft provides the permissions objects through the Office object library since permissions can be applied to Excel, Word, and PowerPoint documents. Figure 6-20 illustrates the hierarchy of the permission objects.

Figure 6-20. Office permisson object model


6.8.1. How to do it

Once credentials are installed, you can restrict access to workbooks by setting the Permission collection's Enabled property to True, as shown here:

    Dim irm As Office.Permission    Set irm = ThisWorkbook.Permission    irm.Enabled = True

The preceding code sets the workbook as Do Not Distribute. You are given full control, but no other users have permissions. Use the Add method to add permissions for other users. You must add each user individually, even if they have the same permissions, as shown here:

    Set irm = ThisWorkbook.Permission    irm.Add "ExcelDemo@hotmail.com", MsoPermission.msoPermissionView    irm.Add "someone@microsoft.com", MsoPermission.msoPermissionView

Use Or to combine permissions for a user. For example, the following code allows ExcelDemo@hotmail.com to read, print, and copy a workbook:

    irm.Add "someone@microsoft.com", MsoPermission.msoPermissionView Or & _      MsoPermission.msoPermissionPrint Or MsoPermission.msoPermissionExtract

When you combine permissions, they may not display in the Excel Permissions options dialog box. Instead, the user may appear as having Custom permissions as shown in Figure 6-21.

Figure 6-21. User options added in code don't always appear in the Excel dialogs


You can set a date at which the user's permissions to the document expire using an argument in the Add method or by setting the Expiration property as shown here:

    Set irm = ThisWorkbook.Permission    Set usr = irm("ExcelDemo@hotmail.com")    usr.ExpirationDate = Date + 1

The preceding code sets the expiration date for the user one day from the current date. Expiration dates are always calendar datesyou can't set permissions to expire at a certain time.

You may also notice from the preceding code that there is no "Users" collection. Instead, you use the Permission collection to get UserPermission objects. For example, the following code displays the permissions for each user in the Immediate window:

    Dim irm As Office.Permission, usr As Office.UserPermission    Set irm = ThisWorkbook.Permission    For Each usr In irm     Debug.Print usr.UserId, usr.Permission, usr.ExpirationDate    Next

The simplest way to remove permissions from a workbook is to set the Permission collection's Enabled property to False:

    ThisWorkbook.Permission.Enabled = False

Disabling the Permission collection removes all users and their permissions. Use the UserPermission object's Remove method to selectively remove users.

6.8.2. What about...

To learn aboutLook here
Permission and UserPermission objects C:\Program Files\Microsoft Office\OFFICE11\1033\VBAOF11.CHM
Deploying and programming with IRMSubscribe to the newsgroup: microsoft.public.rights_mgmt_svcs




    Excel 2003 Programming. A Developer's Notebook
    Excel 2003 Programming: A Developers Notebook (Developers Notebook)
    ISBN: 0596007671
    EAN: 2147483647
    Year: 2004
    Pages: 133
    Authors: Jeff Webb

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net