Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Locking Fields

  1. #1
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Tallahassee, Florida, USA
    Posts
    901
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Locking Fields

    I know that I can search for, say, TOA Fields (^d TOA) and then lock them (Ctrl+F11), and loop through a document doing individually. Is there a way to do this programatically through VBA?
    Karen

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Locking Fields

    I don't ever lock fields; I just unlink them, so I'm not sure if this actually works.

    Sub TALock()
    Dim aField As Field
    For Each aField In ActiveDocument.Fields
    If aField.Type = wdFieldTOAEntry Then
    aField.Locked = True
    End If
    Next
    End Sub

    Hope this helps.

  3. #3
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Locking Fields

    For the specific purpose of TOC or TOA fields, that should work fine. However, for any fields in the headers and footers, you would have to loop through the storyRanges.
    Charles Kyle Kenyon
    Madison, Wisconsin

  4. #4
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Tallahassee, Florida, USA
    Posts
    901
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Locking Fields

    Thanks to both of you. Yes, Charles -- I know that it only works for TOA -- which is what I want. I can't get the users to stop editing the TOA result <img src=/S/dragon.gif border=0 alt=dragon width=17 height=15>, but need to update fields on print to update the tagline... so what Jscher2000 provided will do the trick nicely! Thanks for the input.
    Karen

  5. #5
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Tallahassee, Florida, USA
    Posts
    901
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Locking Fields

    Thanks! That'll do it.

    Edited 01/03/23 by KB MacKenzie
    Instead of wdFieldTOAEntry, it's wdFieldTOA. But you gave me what I needed to get the job done. Thanks again!
    Karen

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Locking Fields

    A very nitpicky followup<g>:

    For many if not most uses of the For Each/Next construct to loop through a collection, it will 'visit' all of the story ranges without needing to specify the separate story ranges.

    In the case of the code Jefferson posted, it will work fine for locking fields in the header, footer, textboxes etc.

    I think you mainly need to worry about visiting each of the story ranges if you are applying a selection to each object.

  7. #7
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Locking Fields

    Sorry Gary,

    It was nitpicky in that the code posted answered the problem. Not so nitpicky though if anyone tried to adapt it to handle fields in headers/footers.

    I tried the following code to update fields. Those in the body of the document are updated, those in headers/footers are not.

    Sub AllFieldsUpdate()
    Dim aField As Field
    For Each aField In ActiveDocument.Fields
    aField.Update
    Next aField
    End Sub

    You were correct that in talking about "looping" through the story ranges I meant a For/Next loop. <img src=/S/yadda.gif border=0 alt=yadda width=15 height=15> The following code does update all fields:

    Sub UpdateAllStories()
    Dim rStory As Range
    For Each rStory In ActiveDocument.StoryRanges
    rStory.Fields.Update
    Next rStory
    End Sub

    It can be modified to only update certain fields using an IF construction.
    Charles Kyle Kenyon
    Madison, Wisconsin

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Locking Fields

    Hi Charles,

    No need to be sorry... this is one of those 'twilight zone' experiences for me: when I tested the following code yesterday:

    <pre>Sub AllFieldsLocked()
    Dim aField As Field
    For Each aField In ActiveDocument.Fields
    aField.Locked = True
    Next aField
    End Sub</pre>

    ... I thought I observed that it locked all fields in the document; now when I try this again tonight, as you say, it only locks them in the main document story <img src=/S/crazy.gif border=0 alt=crazy width=15 height=15> - don'tcha hate it when stuff like that happens?<g>

    I'll try to make sense out of that tomorrow...

    So until proven otherwise I'll definitely go along with your take on this.<g>

    Gary

  9. #9
    NewWordUser
    Guest

    Re: Locking Fields

    Charles,

    Can you give us an example of a macro that will only lock the fields in the Header and Footer?

    I'm new to VBA and I could really use this.

    Thanks!

  10. #10
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Locking Fields

    I don't have one handy but will try to come up with one. If I haven't gotten back to you by the end of the week, please send me an email or a private message.
    Charles Kyle Kenyon
    Madison, Wisconsin

  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Locking Fields

    Hi NWU,

    The following macro should do this:

    <pre>Sub LockAllFieldsInHeadersFooters()
    Dim aStory As Range
    For Each aStory In ActiveDocument.StoryRanges
    If aStory.StoryType > 5 Then
    aStory.Fields.Locked = True
    End If
    Next aStory
    End Sub
    </pre>

    Gary

  12. #12
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Locking Fields

    Gary,

    I didn't understand quite straight away what you were trying to do.

    I had to dig a bit to work it out. This code does exactlky the same- but might be easier for somebody else coming in later to maintain:
    <pre>Sub LockAllFieldsInHeadersFooters()
    Dim aStory As Range
    For Each aStory In ActiveDocument.StoryRanges
    Select Case aStory.StoryType
    Case wdCommentsStory, wdEndnotesStory, _
    wdFootnotesStory, wdMainTextStory, wdTextFrameStory

    Case wdEvenPagesFooterStory, wdEvenPagesHeaderStory, _
    wdFirstPageFooterStory, wdFirstPageHeaderStory, _
    wdPrimaryFooterStory, wdPrimaryHeaderStory
    aStory.Fields.Locked = True
    End Select
    Next
    End Sub
    </pre>

    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  13. #13
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Locking Fields

    Hi Geoff,

    I agree with you, thanks for posting your version.
    What I posted is more streamlined, but yours is easier to understand and maintain (and more instructive<g>).

    Gary

  14. #14
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Locking Fields

    Hi Geoff & Gary:

    What is it about a macro that requires maintenance? Thanks.

  15. #15
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Locking Fields

    Phil,

    From my point of view, somebody else will probably be looking after my system in 6 months time.

    If, for instance, we upgraded to Word e[bold]XP[/bold]ensive, there might be more constants not in the list. So Gary's code might not work.

    If my successor came across code which said "if aStory.StoryType > 5" (s)he might have trouble knowing what was going on. It might take that person some time to work out what exactly that meant (as it did for me).

    If the person saw things which meant something real, the job of fixing up would be a lot easier.

    I have been in the unfortunate situation a few times where I wrote code which I assumed to be throwaway, so I didn't pay much attention to readability. Unfortunately, the code was used later (even one-off conversion code was used as a basis for another conversion project). So I try to make new coede just that little bit better now.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Page 1 of 2 12 LastLast

Posting Permissions

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