Locking Master Document

D

Derek Reed

I need to be able to lock a word master document from VBA. In word from the
outline view when I position the cursor inside the master document and click
the Lock Document icon this works fine but if I record these actions the
macro produced contains the code:
Selection.Range.Subdocuments(1).Locked = Not Selection.Range.Subdocuments( _
1).Locked
If I run this macro I get a message saying the selected member of the
collection does not exist.

Anyone know how to lock a master document from VBA?
 
J

Jean-Guy Marcil

Derek Reed said:
I need to be able to lock a word master document from VBA. In word from the
outline view when I position the cursor inside the master document and click
the Lock Document icon this works fine but if I record these actions the
macro produced contains the code:
Selection.Range.Subdocuments(1).Locked = Not Selection.Range.Subdocuments( _
1).Locked
If I run this macro I get a message saying the selected member of the
collection does not exist.

These are the perils of using the Selection object. When you recorded the
code, the cursor (represented by the Selection object) was in an appropriate
location, but when you tried to run the recorded code, if the cursor was not
in an inappropriate location you got an error message.

The subdocuments in a master document form a collection. Once you understand
that, you can manipulate each subdocument as you wish. See the following code:

Dim subdocsCurrent As Subdocuments
Dim i As Long

Set subdocsCurrent = ActiveDocument.Subdocuments

For i = 1 To subdocsCurrent.Count
If Not subdocsCurrent(i).Locked Then
subdocsCurrent(i).Locked = True
End If
Next


It may seem that the bit

If Not subdocsCurrent(i).Locked Then
subdocsCurrent(i).Locked = True
End If

is overkill, but if you use only:

For i = 1 To subdocsCurrent.Count
subdocsCurrent(i).Locked = True
Next

you get undesired results.

Then the code act as a toggle, even though it says to set all subdocuments
to locked, if some are already locked, they will become unlocked.

Note that you must first save the master document at least once for the code
to work.

For more on using the macro recorder, see
http://word.mvps.org/faqs/macrosvba/UsingRecorder.htm
and
http://word.mvps.org/faqs/macrosvba/ModifyRecordedMacro.htm
 
D

Derek Reed

Thank for taking time to respond, but unfortunately it does not address my
question. What you have described is the mechanism for locking subdouments;
which I already know what to do.

My question is how to lock the Master document. You can do it by moving the
cursor to a line in the master document and clicking the lock buttom on the
toolbar; I cannot however work out how to do the same from VBA.

Can anyone help?
 
J

Jean-Guy Marcil

Derek Reed said:
Thank for taking time to respond, but unfortunately it does not address my
question. What you have described is the mechanism for locking subdouments;
which I already know what to do.

My question is how to lock the Master document. You can do it by moving the
cursor to a line in the master document and clicking the lock buttom on the
toolbar; I cannot however work out how to do the same from VBA.

I do not think that button was intend for that purpose, but it does lock the
master document itself while leaving the subdocuments unlocked, which is a
bit weird, no?

In any case, since this is what you need to do, I looked into it and the
only way I managed to make this work with VBA is as follows. The code works
as a toggle because if the master is locked, it will unlock it, and vice
versa.


Sub ToggleLockMaster()

Dim cmdbarOutline As CommandBar
Dim ctrOutline As CommandBarControl
Dim i As Long

If ActiveDocument.IsMasterDocument Then
Selection.HomeKey wdStory

If Not ActiveDocument.ReadOnly Then
ActiveDocument.Save
End If

Set cmdbarOutline = Application.CommandBars("Outlining")

For i = 1 To cmdbarOutline.Controls.Count
Set ctrOutline = cmdbarOutline.Controls(i)
If ctrOutline.ID = 236 Then ctrOutline.Execute
Next
Else
MsgBox "The currently active document is not a master document.", _
vbExclamation, "Cancelled"
End If

End Sub
 
D

Derek Reed

Thank you!

That is very helpful. I agree the it is a bit strange, but is a neat
solution for the application I am working on. It allows to to put fixed text
in the master document, and allow the users to edit the subdocuments

Cheers!
 

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