Write the macro before you lock the form. Also, if you need to run it from a specific place, use your macro as an exit macro for a formfield.
The following is a generic type macro for protection/unprotection. Assumes: 4 sections, of which Sections 2 & 3 need to be protected as they have formfields, Sections 1 & 4 do not. Note that I tend to make the document I am working on a specific object. Just a habit, as it makes it easier if you are working on multiple documents. The aDoc as Document object is not, strictly speaking, necessary. ActiveDocument.Unprotect (etc.) works as well.
Sub ProtectUnptotect()
Dim aDoc As Document
Set aDoc = ActiveDocument
aDoc.Unprotect Password:=""
' * * * * * * * * * * *
' do what ever you need to do
aDoc.Protect wdAllowOnlyFormFields, Password:=""
Selection.Sections(1).ProtectedForForms = False
Selection.Sections(2).ProtectedForForms = True
Selection.Sections(3).ProtectedForForms = True
Selection.Sections(4).ProtectedForForms = False
Set aDoc = Nothing
End Sub
Hope this helps.
Gerry