• Macro to generate bookmarks for reports

    Author
    Topic
    #480403

    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

    Viewing 3 reply threads
    Author
    Replies
    • #1309580

      Hi CPD,

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

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1309582

        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 :rolleyes:, but it not applicable for my particular problem.

        Cheers

        Claude

    • #1309584

      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.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1309587

      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
    • #1310233

      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
      [Fmr MS MVP - Word]

    Viewing 3 reply threads
    Reply To: Macro to generate bookmarks for reports

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: