0
votes

I am trying to generate a bookmark on the line below a previous bookmark in a bullet list such that the word document starts as

  • [Bookmark1]

and then is transformed into

  • [Bookmark1]
  • [Bookmark2]
  • Etc.

so that I can adjust the formatting of the list line by line and also input text in the list. The 1D array Level() controls which type of bullet point it will have. The bookmarks are dynamically created based on a dynamically allocated 1D array (called clar()) that contains text from various points on the sheet. The program does not know how many lines of text the array will contain until it runs. The code below will run but the output of the program is

  • [Bookmark1]

will turn into (after six iterations)

  • [Boomark6]

So I only had one bookmark now called Bookmark6 rather than six in a list. I attempted to use the range at the end of the previous bookmark but this does not set the range at the next bullet point or outside the current bookmark. I also tried to expand the range using wbNewLine and then set the range at the end of that but this is not valid.

   Dim Count As Integer
Dim CountM As Integer
Count = 1
Do While Clar(Count) <> ""
CountM = Count - 1
PrevBmarkName = "Clar" & CountM
BmarkName = "Clar" & Count
BmarkText = Clar(Count)

If Count <> 1 Then
    Set prevRange = wrdDoc.Bookmarks(PrevBmarkName).Range
    Set newRange = prevRange
    newRange.SetRange prevRange.End, prevRange.End
    ActiveDocument.Bookmarks.Add "BmarkName", newRange
    If DesClar(Count + 1) = "" Then    'Stops the program from generating a blank bullet
        wrdRange.Text = BmarkText
        wrdDoc.Bookmarks.Add Name:=BmarkName, Range:=wrdRange
        Level = Check1(Count)         'Used to adjust the bullet level/type
            If Level = 1 Then wrdRange.Style = wrdDoc.Styles("SD Bullets")
            If Level = 2 Then wrdRange.Style = wrdDoc.Styles("SD Bullets 2")
    Else
        wrdRange.Text = BmarkText & vbNewLine
        wrdDoc.Bookmarks.Add Name:=BmarkName, Range:=wrdRange
        Level = Check1(Count)
            If Level = 1 Then wrdRange.Style = wrdDoc.Styles("SD Bullets")
            If Level = 2 Then wrdRange.Style = wrdDoc.Styles("SD Bullets 2")
     End If
Else                         'Only used on the first iteration because it uses Bookmark1
    Set wrdRange = wrdDoc.Bookmarks(BmarkName).Range
    wrdRange.Text = BmarkText & vbNewLine
    wrdDoc.Bookmarks.Add Name:=BmarkName, Range:=wrdRange
    Level = Check1(Count)
        If Level = 1 Then wrdRange.Style = wrdDoc.Styles("SD Bullets")
        If Level = 2 Then wrdRange.Style = wrdDoc.Styles("SD Bullets 2")
End If

Count = Count + 1
CountM = CountM + 1
Loop
1

1 Answers

1
votes

2 issues:

First, wrdRange is only set the first time through the loop, and can't change after that due to the If Count <> 1 test. Check all of the references to wrdRange for If Count <> 1.

Second, when you are setting newRange = prevRange, you aren't getting a new Range - you get two variables with the same reference. Change the line Set newRange = prevRange to Set newRange = prevRange.Duplicate.