Consulting

Results 1 to 5 of 5

Thread: Solved: Hiding Text and formfields using Styles

  1. #1
    VBAX Regular
    Joined
    Apr 2005
    Posts
    86
    Location

    Solved: Hiding Text and formfields using Styles

    Alright guys!

    Got a form with the following macro. As you can see it searches for all text in the style "HiddenRed" and then hides it.
    Well....that's not entirely true, it runs up to a certain point and then stops with the error "Object variable or With block variable not set"

    It runs through a protected section of the document fine without having to do anything, then goes into a unprotected section and hides some text in there. Once that is done, it throws up the above error.
    The only thing left for it to do is to hide a few formfields(in a protected area) in the "HiddenRed" style.

    [VBA]Sub WorkInProtectedDoc()

    If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then ActiveDocument.Unprotect Password:=""

    Dim par As Paragraph

    For Each par In ActiveDocument.Paragraphs
    If par.Range.Style = "HiddenRed" Then
    par.Range.Font.Hidden = True
    End If
    Next par

    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""

    End Sub[/VBA]

    Cheers

    Pete

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Pete,

    By setting the para range to hidden you change the index of the paragraps collection and that's why you error out!

    If you change a collection in your code you must always loop the collection backwards to front so you that you avoid those problems.

    This will work:[vba]
    Sub WorkInProtectedDoc()
    Dim lPara As Long
    With ActiveDocument
    If .ProtectionType = wdAllowOnlyFormFields Then .Unprotect Password:=""

    For lPara = .Paragraphs.Count To 1 Step -1
    With .Paragraphs(lPara)
    If .Range.Style = "hiddenred" Then
    .Range.Font.Hidden = True
    End If
    End With
    Next

    .Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
    End With
    End Sub
    [/vba]

    HTH,
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Ah, and not only that, but this kind of operation also changes the page numbers....just a note.

  4. #4
    VBAX Regular
    Joined
    Apr 2005
    Posts
    86
    Location
    Wahey!!

    Once again i bow down to you Joost.
    Thanks for the input Gerry.

    Pete

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Wahey..right back at yah Pete! You're welcome!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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