MSDN > 論壇首頁 > Visual Basic for Applications (VBA) > Collapse Word paragraph using macro
發問發問
 

問題Collapse Word paragraph using macro

  • 2009年7月8日 下午 01:15123Heather 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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

所有回覆

  • 2009年7月9日 下午 02:39ShasurMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼

    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
  • 2009年7月15日 下午 05:21123Heather 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Shasur, I tried using the example you provided.....is p1 meant to be line1 as you defined earlier?

    Thanks,
    Heather
  • 2009年7月18日 上午 04:21ShasurMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    Hi Heather

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

    Cheers
    Shasur


    http://www.vbadud.blogspot.com
  • 2009年7月20日 下午 02:32123Heather 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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