Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Dec 2001
    Location
    Reston, Virginia, USA
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Identifying Outline Section containing a comment (Word 2000 SR1)

    I am writing a macro to copy all document comments into a separate table. The source document contains paragraphs with heading styles with autonumbering of the section number enabled. I need to identify the number of the section containing the comment mark. For example, below represents a portion of the source document:

    ...
    2.1.2 This is Section Title

    This is section paragraph one.

    This is section paragraph two. The second sentence contains a comment mark[MWS1]. This is the third sentence.

    This is section paragraph three.

    2.1.3 This is Another Section Title
    ...

    I am looking for code that will return "2.1.2" as being the section containing comment [MWS1].

    <img src=/S/help.gif border=0 alt=help width=23 height=15> Thanks in advance for your help.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Identifying Outline Section containing a comment (Word 2000 SR1)

    Try this. I just kind of explored around until I found some relevant properties and then used a backwards search toward the beginning of the document. If you find any great tweaks, or a more efficient way to do it, please share.
    <pre>Sub testItOut()
    MsgBox strLastListNumByStyle("Heading 3")
    End Sub

    Function strLastListNumByStyle(strStyleName As String) As String
    'Set a range from the end of the Selection to the start of doc
    Dim rngTemp As Range
    Set rngTemp = ActiveDocument.Range(ActiveDocument.Range.Start, _
    Selection.Range.End)
    'Search upwards for first instance of the specified style name
    With rngTemp.Find
    .Format = True
    .Style = strStyleName
    .Forward = False
    .Wrap = wdFindStop
    If .Execute = True Then
    'Return the "full" number for the found paragraph
    strLastListNumByStyle = rngTemp.ListFormat.ListString
    Else
    'Return an empty string
    strLastListNumByStyle = vbNullString
    End If
    End With
    Set rngTemp = Nothing
    End Function</pre>

    (This problem vaguely reminds me of a lengthy thread started by Guy Gallo about looping upwards through a document looking at headings, but I didn't go searching to see whether his method, or any suggested alternative, was applicable here.)

  3. #3
    Lounger
    Join Date
    Dec 2001
    Location
    Reston, Virginia, USA
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Identifying Outline Section containing a comment (Word 2000 SR1)

    Jeff,
    Thanks for your effort, however as is, it is limited to finding just Heading3 styles. I studied this further last night, and came up with using the Goto previous Heading approach included in the below code. The approach moves the selection point around, which I would prefer to avoid, but it works correctly. The document being examined did not have any comments inserted into headings, so there was no need to do an initial check before doing the GoToPrevious Heading. To modify the code for general use, this check should be added, plus the output document and table created as described at the beginning of the procedure.
    The CleanStringCompletely function was primarily needed to remove the character/symbol indicating the inserted comment and the ending paragraph mark from the document paragraph text.

    Public Sub GetCommentData()
    ' For each comment in the src document, get the comment identifier, the comment text,
    ' the para containing the comment, and the heading number of the section
    ' containing the para. Put these values into a table in the out document.
    ' The out document must have been previously prepared with a four column table
    ' (Section Number, Paragraph Text, Comment ID, Comment Text), with
    ' the cursor located in the last column of the last row of the table. The src document must be active at the start of the
    ' macro. The out document must be the Next Document from the src document.
    Dim src As Document, out As Document
    Dim r As Range
    Dim cmtStr As String, secStr As String, paraStr As String, cmtId As String
    Dim cmt As Comment
    Set src = ActiveDocument
    ActiveWindow.Next.Activate
    Set out = ActiveDocument
    For Each cmt In src.Comments
    src.Activate
    With cmt
    Set r = .Reference
    r.Select
    Selection.GoToPrevious wdGoToHeading
    secStr = Selection.Range.ListFormat.ListString
    r.Select
    paraStr = CleanStringCompletely(.Reference.Paragraphs(1).Ran ge.Text)
    cmtId = .Initial & .Index
    cmtStr = CleanStringCompletely(.Range.Text)
    End With
    out.Activate
    Selection.Tables(1).Rows.Add
    Selection.Move wdCell, 1
    Selection.TypeText secStr
    Selection.Move wdCell, 1
    Selection.TypeText paraStr
    Selection.Move wdCell, 1
    Selection.TypeText cmtId
    Selection.Move wdCell, 1
    Selection.TypeText cmtStr
    Next cmt
    End Sub

    Public Function CleanStringCompletely(ByVal st As String) As String
    ' Removes spaces and control characters from beginning and end of supplied string.
    ' Converts control characters within string to space char.
    Dim t As Long, l As Long

    st = st & "^"
    t = 1
    Do While Asc(Mid(st, t, 1)) <= 32
    t = t + 1
    Loop
    st = Mid(st, t)
    t = Len(st)
    If t = 1 Then
    CleanStringCompletely = ""
    Else
    t = t - 1
    Do While Asc(Mid(st, t, 1)) <= 32
    t = t - 1
    Loop
    st = Mid(st, 1, t)
    End If
    l = Len(st)
    For t = 2 To l - 1
    If Asc(Mid(st, t, 1)) < 32 Then
    st = Mid(st, 1, t - 1) & " " & Mid(st, t + 1, l - t)
    End If
    Next t
    CleanStringCompletely = st
    End Function

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Identifying Outline Section containing a comment (Word 2000 SR1)

    Marty

    I would suggest the coding the following process to work out the previous number. If you need code to do this let me know.
    1. Assign current style to a variable
    2. Make the para Normal (or at least a non-outline numbered style)
    3. On the keyboard Shift-Alt-Left Arrow (this gives you a heading at the same level as the previous heading.)
    4. Now you know the heading level (style) you are looking for so you can insert a field to pull in the previous number eg{STYLEREF "Heading 2" n}
    5. Use the outcome of this in your comment table
    6. Remove the field
    7. Put the paragraph style back to whatever it was in step 1
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Identifying Outline Section containing a comment (Word 2000 SR1)

    Chief, I was not aware of GoToPrevious and it does resolve that issue which I glossed over in my haze.

    But luckily for you, you don't need a selection at all. Try this:

    <pre>Sub LastHeaderListStringTest()
    Dim cmt As Comment
    For Each cmt In ActiveDocument.Comments
    MsgBox cmt.Initial & cmt.Index & " falls under numbered paragraph " & _
    cmt.Range.GoToPrevious(wdGoToHeading).ListFormat.L istString
    Next
    End Sub</pre>

    This technique of "stacking" properties on top of methods to bypass the explicit creation of new objects seems sometimes to work and other times not to work. When it works, as it does here, it saves a lot of steps!

Posting Permissions

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