MSDN > Home page del forum > Visual Basic for Applications (VBA) > Collapse Word paragraph using macro
Formula una domandaFormula una domanda
 

DomandaCollapse Word paragraph using macro

  • mercoledì 8 luglio 2009 13.15123Heather Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    I'm trying to write a macro that will expand/collapse a paragraph in Word 2007.  I have posted part of the code below, which works fine until the paragraph is more than 50 lines.  When the paragraph is more than 50 lines, the Boolean value changes to 99999 and the macro doesn't work anymore.  Am I hitting a known limitation that I just don't know about?

    If bkmk.Name = "Grow" Then
            If bkmk.Range.Font.Hidden = True Then
                bkmk.Range.Font.Hidden = False
            Else
                bkmk.Range.Font.Hidden = True
            End If
        End If

Tutte le risposte

  • giovedì 9 luglio 2009 14.39ShasurMVPMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Contiene codice

    Mostly this '99999' occurs when there are mixed results - if some lines in the range are hidden and some are not. But this is a strange behavior, might be a bug in Office.

    If that is the case then you need to have some workaround like this

    Sub asasa()
    Dim bkmk As Bookmark
    Dim line1 As Line
    If bkmk.Name = "Grow" Then
            If bkmk.Range.Font.Hidden = True Then
                bkmk.Range.Font.Hidden = False
            ElseIf bkmk.Range.Font.Hidden = True Then
                bkmk.Range.Font.Hidden = True
            Else
                For Each p1 In bkmk.Range.Paragraphs
                    If p1.Range.Font.Hidden = True Then
                        p1.Range.Font.Hidden = False
                    End If
                Next p1
            End If
        End If
    End Sub
    
    
    

    Cheers
    Shasur
    http://www.vbadud.blogspot.com
  • mercoledì 15 luglio 2009 17.21123Heather Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Shasur, I tried using the example you provided.....is p1 meant to be line1 as you defined earlier?

    Thanks,
    Heather
  • sabato 18 luglio 2009 4.21ShasurMVPMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    Hi Heather

    P1 refers to paragraph (each paragraph in the bookmark range)

    Cheers
    Shasur


    http://www.vbadud.blogspot.com
  • lunedì 20 luglio 2009 14.32123Heather Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Ok, I have modified my code to this:

    Dim bkmk As Bookmark
    Dim line1 As Line
    Dim p1 As Paragraph
     
    For Each bkmk In ActiveDocument.Bookmarks
        If bkmk.Name = "Grow" Then
                If bkmk.Range.Font.Hidden = True Then
               
                For Each p1 In bkmk.Range.Paragraphs
                    p1.Range.Font.Hidden = False

                Next p1
              
            Else
                
                For Each p1 In bkmk.Range.Paragraphs
                    p1.Range.Font.Hidden = True
                Next p1
                
            End If
        End If
    Next bkmk


    But it still doesn't work over 50 lines.  Has anyone been able to change the font to hidden for more than 50 lines?

    Thanks
    Heather