sequential names in document

R

Rob Hargreaves

Please could someone help me with a word problem I have when making book
marks

I want to use some code generated by the macro recorder to put some
sequentially numbered bookmarks in a document template.

I need the code to move down x lines from the cursor then enter a new
bookmark with a pre defined name although the number of the page following
the bookmark name.

So page 1 will be bookmark1
page 2 bookmark 2 etc

the code i have and I have it successfully entering test value as text are

Selection.MoveDown Unit:=wdLine, Count:=39
Selection.TypeText Text:="Test Value"

So in the place of testvalue I need to add a bookmark and some code to name
it namepage.

I have found some code to add a bookmark but dont know how to adapt it and
to add a loop.

If someone can help me that would be great.

Thanks

Rob
 
D

Doug Robbins

Sounds like what you are doing is adding bookmarks named as pagenumbers.
What is the purpose of them? There are other ways of going to pages.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
Joined
Aug 6, 2005
Messages
2
Reaction score
0
Hi Doug,

Thanks for your reply,

I was beginning to think that noone would answer and the vba section of the ms public doesnt really recieve posts so I doubt it would get looked at there. (my newsreader only shows 11 posts for the vba one!)

I have tackled the code in a different way and I have nearly completed the whole thing.

This is my approach -

First add the pages changing the number in the loop to the amount of pages.

************************************
Sub AddPages()

Dim x As Integer

Selection.WholeStory
Selection.copy

x = 1
Do Until x = 10

Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeParagraph
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeBackspace

x = x + 1

Loop

End Sub
********************************

then using the code below to enter the bookmarks.

********************************
Sub PagesBookmarks()

Dim r As Range
Dim x As Integer
Dim mypage As Variant

Set r = ActiveDocument.Range

x = 0

Do Until x = 10

mypage = Selection.Information(wdActiveEndSectionNumber) 'wdActiveEndPageNumber 'wdActiveEndAdjustedPageNumber

Set r = r.GoTo(what:=WdGoToItem.wdGoToLine, which:=WdGoToDirection.wdGoToRelative, Count:=2)
r.MoveStart WdUnits.wdCharacter, 21
r.Bookmarks.Add "AIName" & mypage

Set r = r.GoTo(what:=WdGoToItem.wdGoToLine, which:=WdGoToDirection.wdGoToRelative, Count:=1)
r.MoveStart WdUnits.wdCharacter, 14
r.Bookmarks.Add "AIRef" & mypage

Set r = r.GoTo(what:=WdGoToItem.wdGoToLine, which:=WdGoToDirection.wdGoToRelative, Count:=2)
r.MoveStart WdUnits.wdCharacter, 16
r.Bookmarks.Add "Address" & mypage

Set r = r.GoTo(what:=WdGoToItem.wdGoToLine, which:=WdGoToDirection.wdGoToRelative, Count:=3)
r.MoveStart WdUnits.wdCharacter, 23
r.Bookmarks.Add "Operator" & mypage

Set r = r.GoTo(what:=WdGoToItem.wdGoToLine, which:=WdGoToDirection.wdGoToRelative, Count:=1)
r.MoveStart WdUnits.wdCharacter, 15
r.Bookmarks.Add "Manager" & mypage

Set r = r.GoTo(what:=WdGoToItem.wdGoToLine, which:=WdGoToDirection.wdGoToRelative, Count:=2)
r.MoveStart WdUnits.wdCharacter, 5
r.Bookmarks.Add "MeterNo" & mypage

Set r = r.GoTo(what:=WdGoToItem.wdGoToLine, which:=WdGoToDirection.wdGoToRelative, Count:=25)
r.MoveStart WdUnits.wdCharacter, 1

x = x + 1

Loop

End Sub
************************

The problem is that the variable mypage which collects its page number value from the line

mypage = Selection.Information(wdActiveEndSectionNumber)

does not enter more than the first page number. I am guessing that this is because the selection.information gets its information from before the code starts.

As you can see by the commented out code following the line in the main code above I have tried out
wdActiveEndPageNumber and wdActiveEndAdjustedPageNumber in place of wdActiveEndSectionNumber to no avail.

They all create the first pages bookmarks but excel is seeing the wdActiveEndSectionNumber as alsways page 1 so only adds the first set of bookmarks.

I know an alternative would be to specify that the value of x from the loop be used in the bookmark name. Can you tell me how I would alter my code to reflect that?

Thanks again,

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top