View Single Post
 
Old 08-19-2017, 04:39 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

From what I understand from your dilemma, you wish to insert an incremented bookmark name each time the macro is run. You can do that with a couple of additional functions e.g. as shown below. I have not tested the concept of the macro itself, only the bookmark issue. The first time the macro is run, the bookmark added is called ABC, the next time ABC1 then ABC2 etc.

You can test the process with
Code:
Sub Macro1()
Dim strName As String
    strName = "ABC"
    strName = BookmarkUnique(strName, ActiveDocument)
            ActiveDocument.Bookmarks.Add Name:=strName, Range:=Selection.Range
End Sub

Code:
Option Explicit

Sub ProofOfConcept()
'macro by Andrew Lockton
'Graham Mayor - http://www.gmayor.com - Last updated - 19 Aug 2017
Dim aRng As Range, iLev As Integer
Dim strName As String
    strName = "ABC"
    Set aRng = ActiveDocument.Bookmarks("\page").Range
    aRng.InsertParagraphBefore
    With aRng.Paragraphs(1)
        .Style = "Normal"
        .OutlinePromote
        iLev = .OutlineLevel
        If iLev > 3 Then
            .Style = "Normal"
            aRng.Collapse direction:=wdCollapseStart
            aRng.Select
            Selection.GoTo What:=wdGoToHeading, which:=wdGoToPrevious, Count:=1
            While Selection.Paragraphs(1).OutlineLevel <> iLev - 1
                Selection.GoTo What:=wdGoToHeading, which:=wdGoToPrevious, Count:=1
            Wend
            strName = BookmarkUnique(strName, ActiveDocument)
            ActiveDocument.Bookmarks.Add Name:=strName, Range:=Selection.Range
            aRng.InsertAfter " continued"
            aRng.Collapse direction:=wdCollapseStart
            ActiveDocument.Fields.Add Range:=aRng, Text:="Ref " & strName & " \w \h"
            aRng.Select
        Else
            aRng.Paragraphs(1).Range.Delete
        End If
    End With
End Sub

Private Function BookmarkUnique(strBookmark As String, oDoc As Document) As String
'Graham Mayor - http://www.gmayor.com - Last updated - 19 Aug 2017
Dim lngB As Long: lngB = 1
Dim lngName As Long
    lngName = Len(strBookmark)
    Do While BMExists(strBookmark, oDoc) = True
        strBookmark = Left(strBookmark, lngName) & lngB
        lngB = lngB + 1
    Loop
    'Reassemble the filename
    BookmarkUnique = strBookmark
lbl_Exit:
    Exit Function
End Function

Private Function BMExists(strbmName As String, oDoc As Document) As Boolean
'Graham Mayor - http://www.gmayor.com - Last updated - 19 Aug 2017
Dim oBm As Bookmark
    For Each oBm In oDoc.Bookmarks
        If oBm.Name = strbmName Then
            BMExists = True
            Exit For
        End If
    Next oBm
lbl_Exit:
    Exit Function
    Set oBm = Nothing
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote