Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Feb 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    114
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro to generate bookmarks for reports

    Hi there,

    I write many reports which require a set structure where there are recommendations and conclusions in a paragraph when I am describing an issue. I then need to summarise the recommendations and conclusions at the end of the report - I will do this by cross referencing the "recommendation bookmark'

    So .... I would like assistance in generating a macro which will create a recommendation bookmark "RECXXX", where XXX is automatically toggled every time I run (hit the assigned macro button) the macro (ie from 001 to 010 if I run the macro 10 times).

    Any assistance will be greatly appreciated.

    Thanks

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi CPD,

    Why not use a numbered paragraph Style, where the number is prefixed with 'Recommendation'?
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    2 Star Lounger
    Join Date
    Feb 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    114
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Paul,

    Thanks for your review. The reports that I generate follow a rather strict format style (called a seven part paragraph) so the conclusion and recommendation are inbedded in the 'one paragraph' with the other '5 parts'. So I understand your proposed solution, and I would even know how to do that myself , but it not applicable for my particular problem.

    Cheers

    Claude

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Claude,

    Perhaps the simplest would be a macro to insert a SEQ field coded as:
    {SEQ Rec \# "'Recommendation: '0 "}
    That way, if you add/delete recommendations before those already inserted, the numbering can be updated via a simple field update. If you combine that with the use of Style separators (generated via Ctrl-Shift-Enter in the previous paragraph) and numered paragraphs as previously suggested, you can retain the appearance of in-line recommendations and have the cross-referencing functionality.
    Last edited by macropod; 2011-12-08 at 02:12. Reason: Clarification
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    2 Star Lounger
    Join Date
    Feb 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    114
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Paul,

    WIth my understanding of word that sounds great. I am however having trouble implementing the solution. I now realise that I probably have not given sufficient information how the solution, in my mind, would work. After writing the paragraph, or document, I would go through the document and identify the 'Recommendation', highlight the 'recommendation' text that I want duplicated in summary section and then hit a 'whiz bang macro button' which will produce the magic that will enable the cross referencing in the summary section.

    Below is an example of a macro that was generated to do a similar function for abbreviations in the report .... but I dont have the talent to modify it and ideally i would like this functionality for a report I am writing overnight. I dont mind a SEC field or a bookmarking function....I am easily pleased Once again, apologies for my lack of macro karma.

    Code:
    Public Sub MakeREC()
    Debug.Print Now(), "CompanyGlobal.MakeABR"
    On Error GoTo MakeABR_Err:
       
    Dim rngThis As Range, rngE As Range, rngA As Range
    Dim lStart As Long, lEnd As Long, ret As Long
    Dim bmNum As Long
    Dim strText As String, strMsg As String, strTitle As String, strParen As String
    Dim bmAName As String, bmEName As String
    Dim bmA As Bookmark, bmE As Bookmark, bmThis As Bookmark
     
       
    ' Routine to make bookmarks for the abbreviation list
     
    ' takes a selection including the full text followed by the abbreviation in parentheses
    ' extracts the text before the parentheses and marks that with the next available eNN bookmark
    ' then marks the text in the partenthese with the corresponding aNN bookmark.
     
        Set rngThis = Selection.Range
       
        'First check that there are no bookmarks already in the selection
        If rngThis.Bookmarks.Count > 1 Then
            'There are already bookmarks in here - check if they are to be kept, replaced or quit
            strMsg = "There are already bookmark(s) in the selection:" & vbCr & _
                     rngThis.Text & vbCr & _
                     "Do you want to add more?" & vbCr & _
                     "Choose Yes to Keep the bookmarks and add new ones for this Explanation and Abbr." & vbCr & _
                     "Choose No to Replace the bookmarks with new ones for this Explanation and Abbr." & vbCr & _
                     "Choose Cancel to quit."
                    
            strTitle = "Abbreviation Capture"
            ret = MsgBox(strMsg, vbDefaultButton1 + vbYesNoCancel, strTitle)
            Select Case ret
            Case vbYes
                'Do Nothing
            Case vbNo
                For Each bmThis In rngThis.Bookmarks
                    bmThis.Delete
                Next bmThis
            Case vbCancel
                Exit Sub
            End Select
           
        End If
       
        'check for parentheses
        strText = rngThis.Text
        strParen = "("
        lStart = InStr(strText, strParen)
        strParen = ")"
        lEnd = InStr(strText, strParen)
        Debug.Print strText, lStart, lEnd
       
        If lStart > 0 And lEnd > 0 Then
            'We have found the abbr
           
            'Find the next available bookmark number
            bmNum = 1
           
            'Make the BM names
            bmAName = "a" & Trim(Format(bmNum, "00000"))
            bmEName = "e" & Trim(Format(bmNum, "00000"))
           
            While ActiveDocument.Bookmarks.Exists(bmAName) Or ActiveDocument.Bookmarks.Exists(bmEName)
                bmNum = bmNum + 1
                bmAName = "a" & Trim(Format(bmNum, "00000"))
                bmEName = "e" & Trim(Format(bmNum, "00000"))
            Wend
           
            'make the ranges
            If lStart > 1 Then
                'The parentheses are not at the start of the line - assume explanation is
                Set rngE = ActiveDocument.Range(Start:=rngThis.Start, End:=rngThis.Start + lStart - 1)
                Set rngA = ActiveDocument.Range(Start:=rngThis.Start + lStart, End:=rngThis.Start + lEnd - 1)
            Else
                'The parentheses are at the start of the line so make this the abbr
                Set rngA = ActiveDocument.Range(Start:=rngThis.Start + lStart, End:=rngThis.Start + lEnd - 1)
                Set rngE = ActiveDocument.Range(Start:=rngThis.Start + lEnd, End:=rngThis.End)
               
            End If
           
            'Add the bookmarks
            Set bmE = ActiveDocument.Bookmarks.Add(bmEName, rngE)
            Set bmA = ActiveDocument.Bookmarks.Add(bmAName, rngA)
           
        Else
            'No opening and closing paren - so message
            strMsg = "There is no set of parentheses () in the the selection." & vbCr & _
                         "Please make a selection including the full text, and the abbr in parentheses()"
            strTitle = "Abbreviation Capture"
            MsgBox strMsg, vbOKOnly, strTitle
            Exit Sub
           
        End If
    Exit Sub
     
    MakeABR_Err:
        Debug.Print "MakeABR_Err:", Err.Number, Err.Description, Err.Source
        Resume Next
       
    End Sub
    Last edited by macropod; 2011-12-08 at 03:35. Reason: Added code tags

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Claude,

    Anything relying on bookmarks will be problematic, as:
    1. they can easily be deleted/replaced by accident;
    2. they are easily compromised by nothing more complex than typing something else in the bookmarked range; and
    3. you'd need to test whether a found bookmark:
    (a) relates to a recommendation. It may already be in use for other reasons and deleting any cross-references to it would have adverse effects; and
    (b) matches the selected range (it may be one to which 2 above applies).

    To get the next available number, you'd also need to either:
    1. maintain a document property that holds the last-used number; or
    2. loop through all relevant bookmarks to find the highest-numbered one in use.
    3. decide whether 'missing' #s are of any consequence and whether all bookmark #s should correspond with their recommendation #s. If so, a whole new level of complexity is introduced.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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