Consulting

Results 1 to 4 of 4

Thread: Finding and locking a style

  1. #1

    Finding and locking a style

    I have a template with a number of fields and the following code updates these:

        For Each oStory In ActiveDocument.StoryRanges
            Do
                oStory.Fields.Update
                Set oStory = oStory.NextStoryRange
            Loop Until oStory Is Nothing
        Next
    However, I've got a number of Fill-in fields populated with a style I created. The quantity of these fields can vary as the user applies them to the document.

    When I run the above procedure, I want the code to avoid updating the Fill-in fields. At the moment I can do this manually going to each one and using Ctrl+F11. When the field update has completed I go back to each Fill-in field and unlock it by using Ctrl+Shift+F11.

    What I want VBA to do is go through the document, going to each of these Fill-in fields and apply the code of:
    Selection.Fields.Locked = False
    ...and then reversing the lock at the end of the above process using the "True" command.

    At the moment, I don't know how to use the process of either finding each Fill-in field, using VBA, and lock it or use the field plus the style that goes with it and then lock it. That style is called "Appendix Title".

    Can someone please give me some ideas?

    Thanks

    Roderick

  2. #2
    Why don't you replace the fields with content controls and use your code to write to the controls by title/tag and then you won't have to worry about locking them (though you can if you want) and you can apply formatting to the control range either in advance or to the vagaries of your process?
    You may find http://www.gmayor.com/insert_content_control_addin.htm useful and it includes a process to change form fields to content controls to help you on your way.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks for your advice, Graham.

    However, the client is pushing me and the template would take quite a while to convert to Content Controls. I'm leaving it to version 2 when I've had enough time to digest the likely changes needed.

    I appreciate your directions, though

    Roderick

  4. #4
    Further to my earlier start comments, I did some deeper research and found a set of procedures from Greg Maxey which seem to satisfy what I was looking for.

    This is the code:

    Public Sub UniversalFieldMacro()
        Dim rngStory As Word.Range
        Dim lngLink As Long
        Dim oShp As Shape
        lngLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
        For Each rngStory In ActiveDocument.StoryRanges
            'Iterate through all linked stories
            Do
                On Error Resume Next
                'Call actionable procedure
                '      FieldsDeleteAll rngStory
                DeleteAllSpecificFields rngStory
                Select Case rngStory.StoryType
                    Case 6, 7, 8, 9, 10, 11
                        If rngStory.ShapeRange.Count > 0 Then
                            For Each oShp In rngStory.ShapeRange
                                If oShp.TextFrame.HasText Then
                                    'Call actionable procedure
                                    DeleteAllSpecificFields oShp.TextFrame.TextRange
                                End If
                            Next
                        End If
                    Case Else
                        'Do Nothing
                End Select
                On Error GoTo 0
                'Get next linked story (if any)
                Set rngStory = rngStory.NextStoryRange
            Loop Until rngStory Is Nothing
        Next rngStory
    lbl_Exit:
        Exit Sub
    End Sub
    
    
    Sub DeleteAllSpecificFields(ByRef oTargetRng As Range)
        Dim oFld As Word.Field
        For Each oFld In oTargetRng.Fields
            Select Case oFld.Type
                Case wdFieldFillIn
                    '        oFld.Delete
                    oFld.Locked = True
                Case Else
                    'Do nothing
            End Select
        Next oFld
    lbl_Exit:
        Exit Sub
    End Sub
    I made a small amount of change and instead of deleting the fields as shown I locked these and it works as originally wanted.

    So, thanks, Greg for your examples.

    Roderick

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •