none
how to get paragraph's list number using VBA? RRS feed

  • Question

  • for example. If the paragraph is "3.1. sdasdasdasdasd". I need to retrieve the "3.1". How can I do that with VBA. Of course, that paragraph had taken a list style.

    Friday, January 21, 2011 6:56 AM

Answers

  • Hi Sefler,

    Try:

    Sub ReportThisListPara()
    Application.ScreenUpdating = False
    Dim i As Integer, j As Integer, xRefs As Variant, TmpRef
    'Get the CrossReferenceItems collection
    xRefs = ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem)
    With Selection
      If .Range.ListParagraphs.Count = 0 Then Exit Sub
      .Collapse wdCollapseEnd
      On Error Resume Next
      'Loop through the CrossReferenceItems collection to find the current paragraph
      For i = 1 To UBound(xRefs)
        If ActiveDocument.ListParagraphs(i).Range.End = .Paragraphs.First.Range.End Then
          MsgBox ActiveDocument.ListParagraphs(i).Range.ListFormat.ListString
          Exit For
        End If
      Next
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by Sefler Zhou Tuesday, January 25, 2011 10:48 AM
    Friday, January 21, 2011 8:38 AM

All replies

  • Hi Sefler,

    Try:

    Sub ReportThisListPara()
    Application.ScreenUpdating = False
    Dim i As Integer, j As Integer, xRefs As Variant, TmpRef
    'Get the CrossReferenceItems collection
    xRefs = ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem)
    With Selection
      If .Range.ListParagraphs.Count = 0 Then Exit Sub
      .Collapse wdCollapseEnd
      On Error Resume Next
      'Loop through the CrossReferenceItems collection to find the current paragraph
      For i = 1 To UBound(xRefs)
        If ActiveDocument.ListParagraphs(i).Range.End = .Paragraphs.First.Range.End Then
          MsgBox ActiveDocument.ListParagraphs(i).Range.ListFormat.ListString
          Exit For
        End If
      Next
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by Sefler Zhou Tuesday, January 25, 2011 10:48 AM
    Friday, January 21, 2011 8:38 AM
  • The following will get the number of the paragraph in which the selection is located:

    Dim rng As Range
    Dim strnum As String
    ActiveDocument.ConvertNumbersToText
    Set rng = Selection.Paragraphs(1).Range
    strnum = Left(rng, InStr(rng, vbTab))
    ActiveDocument.Undo
    MsgBox strnum
     -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Sefler Zhou" wrote in message news:ab54a94d-f535-43da-ab01-35c5238047c2@communitybridge.codeplex.com...

    for example. If the paragraph is "3.1. sdasdasdasdasd". I need to retrieve the "3.1". How can I do that with VBA. Of course, that paragraph had taken a list style.


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Friday, January 21, 2011 10:23 AM