Results 1 to 9 of 9
  1. #1
    New Lounger
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word: apply protection to Table captions

    Hi

    I need to avoid the table captions to be modified.
    The main reason for that is to run macro on tables : each table will be filled with data coming from different sources.

    I have seen that the code to protect selections of a document is kind of:

    ActiveDocument.Protect Password:="xxxxxxxxx", NoReset:=False, Type:= _
    wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False

    But in order to apply it, I should select in the document everything except the tables captions ...

    Any hint?
    Thanks for your help

    BR
    Bruno

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,379
    Thanks
    207
    Thanked 829 Times in 762 Posts
    Bruno,

    Welcome to the Lounge as a new poster.

    There is no need to select anything as it will protect the entire document not just the captions.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  3. #3
    New Lounger
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot but ... I need the caption ONLY to be protected ... the rest of the document may be modified

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,379
    Thanks
    207
    Thanked 829 Times in 762 Posts
    Bruno,

    To the best of my knowledge you can't protect part of a document with the exception of forms.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,534
    Thanks
    3
    Thanked 147 Times in 140 Posts
    There are two possible ways to protect the captions from edits or deletions.
    1. Use section breaks either side of them and protect the section - Absolute nightmare, don't do it
    2. Put each caption inside a Content Control and lock the Content Control for edits and deletions. This method just might work but you would need to verify whether the captions can be cross-referenced and appear in a Table of Figures. You would also be constrained to saving the document in docx or docm format (not doc).
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    New Lounger
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In fact,

    I manage to do a sequence of actions (keyboard + mouse) which lead me to my goal ...
    But if I record the action, and I play it again it does not work.

    The not working part is the selection of Paragraphs whose style is "Caption" (in fact I work on a word french version and the style name is not this one):
    What I do is to do a find with style format = "Caption" ...
    By hand, sometime it works sometimes not ... and the macro never work:
    ... sounds like a bug ...

    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Légende") 'replace Legend with caption to make it work
    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,534
    Thanks
    3
    Thanked 147 Times in 140 Posts
    It becomes apparent that we don't actually know what you are trying to do...

    The Find/Replace code you posted won't do anything without including the .Execute command somewhere near the end. It may be on a line after the End With and you just didn't post it. That aside, the code you did paste here searches for an instance of a style and then doesn't replace it with anything.

    Perhaps you should explain again what the aim is. You said in the original post that you wanted to protect the captions from change - yet your code looks like you want to change the captions in some way.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. #8
    New Lounger
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First of all thank for your reply.

    You have highlighted that the macro recorder do not record the selection.find.execute
    I hadn't noticed (kind of rookie error)
    So ... the question is how to perform a find all in VBA?

    I have just dicovered that it does not exist in VBA ....
    Si O need in fact to implement my own "find all";

    Please correct me, if I'm wrong








    =============================
    Then wrt, to the global vision:
    As mentioned in the initial post, i need to protect the Tables caption.
    The user will not be able to modify tables captions

    This is feasible (not in VBA, but via keyboard&mouse) if I do the following:

    select the full document. protect it, but the selection will be an exception and everybody will be allowed to modify.
    Protect the document.

    unprotect the document
    Search for anything whose style is "Caption" (in order to select it)
    Protect it (no exception)

    The result is that the document can be modified except the Table captions....


    Then if I record the macro ... It doesn't work: most of the time the pb come from the step "search anything whose style is 'caption'" ....
    I wil l

    Please help me ...

  9. #9
    New Lounger
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Solved

    So finally thanks to the missing code higlighted by Andrew...

    Here's the final code

    Sub ProtectCaptions()
    'Locates each instances of "Caption" style in a document
    'and protect it

    ' In order to make it work, the protection takes place in 2 step
    'First step, protect the document excepted the selection of the full document; this exception being valid for everybody
    'Second step, remove the protection
    'Third step, remove the exception from the sections with style="Caption"

    'First Step: Protect doc except ALL for everybody
    Selection.HomeKey Unit:=wdStory
    Selection.WholeStory
    Selection.Editors.Add wdEditorEveryone
    ActiveDocument.Protect Password:="FST", NoReset:=False, Type:= _
    wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False

    'Second step
    ActiveDocument.Unprotect "FST"

    'Third step
    'scroll through all sections having style="Caption

    With ActiveDocument.Content.Find
    .ClearFormatting
    .Style = wdStyleCaption


    Do While (.Execute(FindText:="", Forward:=True, Format:=True) = True) And (counter < 10)
    counter = counter + 1
    With .Parent
    .StartOf Unit:=wdParagraph, Extend:=wdMove
    .EndOf Unit:=wdParagraph, Extend:=wdExtend
    .Select
    .Copy
    End With

    'remove the exception of step 1, on the selected text
    Selection.Editors(wdEditorEveryone).Delete



    Loop
    End With

    'Apply protection
    ActiveDocument.Protect Password:="FST", NoReset:=False, Type:= _
    wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False

    'result=> you can modify the full document excepted the captions
    End Sub

    THANKS A LOT TO EVERYBODY
    Last edited by theBruuu; 2012-08-23 at 11:03. Reason: SOLVED

Posting Permissions

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