Hi Sachin,
You can manage sheet protection how you want from
VB very easily. You
should, however, include a check to see if the target worksheet belongs to
your
VB project before applying the protection. This will prevent disturbing
other worksheets that may not use protection. <just a safeguard
I assume you already have established a variable reference to the running
instance of Excel via the appropriate APIs (FindWindowA and SetWindowLongA),
in your
VB project.
Following is some standard code that I use for applying sheet protection,
modifying it as required. ***It should be noted that the protection
parameters you are looking to manage are only available in Excel v10 and
later***
How I use it is, I cut&paste the parameters I require so they run
concurrent, then comment out the ones I don't want (as shown). The entire
list of available parameters is always there to pick & choose the order. It
doesn't matter what the order is.
Here's the code:
Sub wksProtect()
With ActiveSheet
If val(Application.Version) = 10 Then
.Protect Password:=" ", _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True, _
Userinterfaceonly:=True, _
AllowFiltering:=True, _
AllowFormattingColumns:=True, _
AllowFormattingRows:=True ', _
AllowDeletingColumns:=True, _
AllowDeletingRows:=True, _
AllowEditRanges:=True, _
AllowFormattingCells:=True, _
AllowInsertingColumns:=True, _
AllowInsertingHyperlinks:=True, _
AllowInsertingRows:=True, _
AllowUsingPivotTables:=True
Else
.Protect Password:=" ", _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True, _
Userinterfaceonly:=True
End If
' .EnableSelection = xlUnlockedCells
' .EnableSelection = xlNoSelection
End With
End Sub
Note that the current parameters being used stop after
AllowFormattingRows:=True and the ' comments out the remaining ones. It's
important that the ' is placed before the comma after the last one being
used, and the very last one can never have a comma after it.
Note also the xlEnableSelection constants are listed. These are also
commented out to leave only the one I require. You can have only one, or none
(which, as in the example above, defaults to xlNoRestrictions). These are
Worksheet level constants, and not part of the protection. They are, however,
only effective if a worksheet is protected.
Be sure you qualify your references. This code resides in an Excel add-in
file, so all references are (internal) to the Excel Application, whether
implicit or explicit. If, for example, your variable for the running instance
of the Excel.Application is xlApp, then to use this code in
VB you need to
include it as follows:
In the line With ActiveSheet you would use something like:
With xlApp.ActiveSheet
In the line If Val(Application.Version)... you would use something like:
If Val(xlApp.Version)...
Alternatively, (not tested with this code) you could try something like this
in a sub:
With xlApp
wksProtect
End With
where the reference carries, so you can just run the code "as is" by calling
it after establishing the reference. I know this works with other code I've
used with VB6 DLLs, but I can't say that for sure about this specific code.
HTH
Regards,
Garry