Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    Feb 2016
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Skipping or Excluding Ranges in a Document

    Hi to all,

    I wanted to know what the best way was to skip ranges in a document.

    For example i have ranges that I need to exclude from being touched.

    Is it best to use placeholders such as

    RangeStart

    Formatted Text

    RangeEnd

    Here is a simple example


    Code:
    Sub SkipRanges()
    
    
    Dim oPara As Word.Paragraph
    
    
    For Each oPara In ActiveDocument.Paragraphs
    
    
      If oPara.Range.Words.First = RangeStart And oPara.Range.Words.Last = RangeEnd Then
      
      ' Exclude  -Skip Over this section
      
      End If
      
      Next
      
      For Each oPara In ActiveDocument.Paragraphs
      
      oPara.Range.Bold = True
    
      
    Next
    End Sub
    its bolded everything.

    Any kind suggestions appreciated

    ac

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    AC,

    Welcome to the Lounge as a new poster!

    You might want to take a look at this MS Article on restricted editing.

    It's another possibility would be to bookmark the portions of the document where wish to restrict editing with a base name and number, e.g. Restricted1, Restricted2, etc. Then the macro could look for these bookmarks based on the base "Restricted" and skip over them when processing.

    My point with both of the suggestions is that I'd avoid methods that require actually modifying the document content to accomplish your task as that can easily lead to missed cleanup and the production of a document with the tags still in place.

    Sorry I'm not more up to snuff on the Word Object Model. Hopefully someone w/more experience in this area will pipe in.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    AC,

    I ran a little test using the Restricted Document method and I think it does what you want.

    See this document for the setup.

    I allowed changes to paragraphs 2 & 4 of my randomly generated {=rnd(5,3)} document.

    I then changed your Macro to:
    Code:
    Option Explicit
    
    Sub TestProtection()
    
       Dim oPara As Word.Paragraph
    
      For Each oPara In ActiveDocument.Paragraphs
      
         oPara.Range.Bold = True
         
      Next 'oPara
      
    End Sub  'TestProtection
    Results:
    WordProtected.PNG

    Test Word File w/Macro: Word - VBA - Restricted Document Editing Sample.docm

    HTH
    Last edited by RetiredGeek; 2016-02-21 at 17:21.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    In addition to the editing restrictions described in the MS article RG linked to, you can use 'Filling in Forms' editing restrictions in conjunction with Section breaks to constrain user edits (by leaving the editable Sections unprotected). These are even more restrictive than the defaults for 'No changes (Read only)' restrictions.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    New Lounger
    Join Date
    Feb 2016
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello RG & Paul,

    thank you very much for advising and the kind welcome.

    Also for the document sample.


    I can't use bookmarks - as I may have the same bookmark - with different enclosed content, that becomes complicated

    Restricting editing may work for a few documents - but there are hundreds of documents, and then i have a lot of styles to restrict.

    to make it easy I thought I could use generic placeholders enclosing the range of formatted text.

    image.png



    Then what ever vba script I run I will ask it to skip enclosed ranges.

    Does the logic make sense?

    I am a newbie but keen to learn the VBA way - as I have so many documents to deal with now.

    What do you gentlemen think ?

    I hope its not as complicated as I am making it out to be

    ac

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    To have hundreds of existing documents that now need protection is some parts and others would be rather strange. Manually tagging such documents with what you call generic placeholders so that a macro could be run to restrict editing to the tagged ranges (which is what you'd have to do) would require as much effort as manually applying the restrictions.

    If the requirement is for new documents, the process is simple - design a template with the restrictions already set up, then create the new documents from that template. No code required.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #7
    New Lounger
    Join Date
    Feb 2016
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Paul,

    aww just seems, really fiddly now.

    I was looking to update documents , and apply new formatting and styles to them to jazz them up.

    However certain sections - are fine as is - I just wanted to skip those sections, portions or ranges as you call them.

    VBA does not have a filter to say this named range - just skip.

    The other alternative is to create a conditional table - with all the styles or formatted text I want to skip or ignore.

    but that's a complex process.

    Bookmark Option

    Create bookmarks of all the sections to not touch - yikeess very complicated it seems now.

    Paul, let me get some ideas on what I can do to change my strategy

    thank you

    ac

  8. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Word doesn't have named ranges - Word's equivalent is bookmarked ranges. And the effort in manually bookmarking all the ranges (with unique bookmarks) would be as great as applying the restrictions, so there'd be no net benefit from the macro. The same applies to anything else you might want to do with defining ranges by Style, etc.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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