EnforceStyle property

L

Larry

I need to determine in VBA whether a document has "formatting
restrictions" enabled. I would think that the value of
ActiveDocument.EnforceStyle would tell me, but its value is always
True, regardless of whether I've restricted formatting or not. How do
I check for this status?

I know that some of the MS documentation is wrong, ascribing the
ability to SET the value to EnforceStyle. But that's not what I'm
looking to do. I just need to find out whether formatting restrictions
are on or off.

TIA--
--larry
 
L

Larry

Another quick follow-up. I've just realised/discovered that
EnforceStyle only tells whether the tick-mark on the Formatting
Restrictions value in the Protect Document pane is on or off. It
doesn't tell whether protection is currently enforced, which is what I
really need to do.
 
J

Jean-Guy Marcil

Larry said:
I need to determine in VBA whether a document has "formatting
restrictions" enabled. I would think that the value of
ActiveDocument.EnforceStyle would tell me, but its value is always
True, regardless of whether I've restricted formatting or not. How do
I check for this status?

I know that some of the MS documentation is wrong, ascribing the
ability to SET the value to EnforceStyle. But that's not what I'm
looking to do. I just need to find out whether formatting restrictions
are on or off.

ActiveDocument.ProtectionType
will tell you the type of protection currently active.
 
L

Larry

ActiveDocument.ProtectionType
will tell you the type of protection currently active.

Thanks, Jean-Guy, but that will give me a value only for the type of
Editing Restriction in effect; it will not show whether Formatting
Protection is enabled. If I have not set any Editing Restriction at
all, then ActiveDocument.ProtectionType will return "-1" no matter
whether Formatting Protection is enabled.
 
K

Klaus Linke

Larry said:
Another quick follow-up. I've just realised/discovered that
EnforceStyle only tells whether the tick-mark on the Formatting
Restrictions value in the Protect Document pane is on or off. It
doesn't tell whether protection is currently enforced, which is what I
really need to do.

This area seems to be incredibly buggy!

First I thought you might go through the styles and look if any have
myStyle.Locked = True.
But that doesn't seem to be cleared when the formatting restrictions are
removed.

Then I thought you might go through the styles, and try to apply one that is
locked. If it's applied, it would show that the formatting restrictions are
not enforced, else you'd catch the error and it would be obvious that the
restrictions are enforced.
Unfortunately, often I can apply locked styles by macro
(Selection.Style=myStyle) when the same is not possible in the user
interface (Styles dropdown or Styles pane), and vice versa.

:-( Klaus
 
L

Larry

Tag, Klaus --

Here's what I ended up doing. It's awful but it works and I don't have
any more time to spend on it. It's just a snippet around my "check
whether format-protection is enabled" logic, and it relies on
recognising two different errors: #4605 when trying to unprotect a
document that is not protected; and #5485 when trying to unprotect a
document with the wrong password:

On Error GoTo ProtectOrNot: ' Only way I can figure to
check whether a document is format-protected.

ActiveDocument.Unprotect ' Note: no password here. Trying
to force an error and then detect which error it was.
ActiveDocument.Protect Type:=wdNoProtection,
EnforceStyleLock:=True, Password:=myPass ' make a password algorithm
from the project ID
GoTo Bye: ' This fall-through can only happen if the
document was initially protected with a null password.

ProtectOrNot:
If Err.Number = "4605" Then ' Document currently
unprotected; protect it
ActiveDocument.Protect Type:=wdNoProtection,
EnforceStyleLock:=True, Password:=myPass ' make a password algorithm
from the project ID
ElseIf Err.Number = "5485" Then ' Document already
protected; don't protect it.
' Do nothing.
End If
Bye:
 
L

Larry

Hi, Klaus -- this may be a duplicate post so I'll just abbreviate what
I posted a few minutes ago but which seems to have failed to post.

I ended up just doing an ActiveDocument.Unprotect, without a password.
This forces a failure -- either the document is not protected in the
first place, or it is protected and the null password is wrong. These
errors give distinct error numbers, so I can trap them and take the
appropriate action.

YASK (Yet Another Stupid Kluge!).

--larry
 
K

Klaus Linke

Larry said:
Hi, Klaus -- this may be a duplicate post so I'll just abbreviate what
I posted a few minutes ago but which seems to have failed to post.

I ended up just doing an ActiveDocument.Unprotect, without a password.
This forces a failure -- either the document is not protected in the
first place, or it is protected and the null password is wrong. These
errors give distinct error numbers, so I can trap them and take the
appropriate action.

YASK (Yet Another Stupid Kluge!).

Maybe... But one based on a very clever idea!

:) Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top