PDA

View Full Version : VBA code equivalent with lock button (Word 2010)



Romulus
11-22-2012, 03:14 AM
Hello all,

I need your help. I am using Windows XP Professional with SP3 + Word 2010 version 14.0.6123.5005 SP1 MSO version 14.0.6123.5001, licensed (file is with DOC extension), and I am struggling to find a VBA code equivalent to lock button, the one which looks like a closed, orange lock.

To add it to quick access toolbar, I chose File -> Options -> Customize Ribbon -> All Commands -> scroll down -> Lock button -> Add -> I put it on first position in the list (to be able to use it with ALT + 1 shortcut) -> OK.

I have tried to record the utilization of this button, it did not work, it recorded nothing, because the recording was stopped as soon as I hit lock button. I have partially succeeded to make it work with SendKeys “%{1}” (or VBA.SendKeys “%{1}”), it does work at the end of the code, but it does NOT work on ErrHandling section. I am soooo close to make it work completely, but because of this I am blocked.



ErrorHandler:
If Err.Number = 4605 Then ‘This is the error number, the code it tries to delete something on a locked file, therefore the error pops up automatically;
VBA.SendKeys "%{1}" ‘This does NOT want to work LLL
Resume
End If


Actually, in the code I am writing, I need a kind of verification whether the file is locked, before actually processing the data. If yes, then the Word file it should be un-locked automatically by the macro routine and then to have the rest of the code run. Else (if the file is not locked), the routine should work naturally.

As am I on hold because of this, I would kindly ask for your help, either to solve my approach above, or to have an equivalent VBA coding for lock button, as mentioned hereinabove. :dunno

Thank you in advance,
Romulus.

fumei
11-22-2012, 02:47 PM
I do not use 2010, but in principle the lock button status is likely boolean. And therefore testable. Try and find the lock button object in the Object Browser. It will show the properties.

IF LockButton.Locked = True Then
....do this
ELSE
...do that

macropod
11-22-2012, 06:15 PM
In a quick test, it seems the lock button merely toggles formfield protection. In that case, the test is:
If ActiveDocument.ProtectionType = wdNoProtection Then
'Do something
Else
'Do something different
End If