PDA

View Full Version : Solved: create bookmarks in following order



white_flag
06-10-2011, 02:24 AM
Hello I like to do something like this via VBA:

so I like an code that will do this (create bookmarks in the following order):

AAbmk11, BBbmk11, CCbmk11 CCbmk12 CCbmk13 CCbmk14 CCbmk15 CCbmk16
AAbmk12, BBbmk12, CCbmk17 CCbmk18 CCbmk19 CCbmk20 CCbmk21 CCbmk22
AAbmk13, BBbmk13, CCbmk23 CCbmk24 CCbmk25 CCbmk26 CCbmk27 CCbmk28 etc

my not working and not logical code is like this:


For i = 11 To 20
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="AAbmk" & i
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.TypeText Text:=" "
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="BBbmk" & i
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.TypeText Text:=" "
For j = 1 To 16
Selection.TypeText Text:=" "
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="CCbmk" & i + j - 1
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Next
Next

white_flag
06-10-2011, 03:23 AM
Dim k As Integer, i, j
k = 11
For i = 11 To 20
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="AAbmk" & i
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.TypeText Text:=" "
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="BBbmk" & i
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
For j = 1 To 16
Selection.TypeText Text:=" "
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="AAbmk" & k
k = k + 1
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Next

Frosty
06-10-2011, 12:54 PM
I assume that's just sample code and you've solved (although you're missing your last next).

I did this just as a quick and dirty proof-of-concept, since what you're really looking for is the "Mod" function as a way of changing a base counter.

Sub UsingMod()
Dim x As Integer
Dim y As Integer
Dim iStartAt As Integer
Dim iResetEvery As Integer
Dim iGoUntil As IFind

iStartAt = 11
iResetEvery = 6
iGoUntil = 28

y = iStartAt
For x = iStartAt To iGoUntil
If (x - iStartAt) Mod iResetEvery = 0 Then
Debug.Print "AAbmk" & y
Debug.Print "BBbmk" & y
y = y + 1
End If
Debug.Print "CCbmk" & x
Next
End Sub

white_flag
06-15-2011, 11:11 AM
Hi Frosty ...nice done.thx . I will change my code with this one.