Variable problem

robhargreaves

Board Regular
Joined
Feb 6, 2005
Messages
85
I have written three sections of code for word and it all works as i want it to.

The first section is to copy the first page x times specified in the loop.

This code looks like this

Code:
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
so the above example creates 10 pages for me including the first template page of the document which all others are copied from.

I place the cursor at the top of the page before executing the next piece of code which adds a bookmark to the page with a constant name and the variable of the page name following.

I know the placement of the cursor is correct to add the bookmark as when i run the same code and add an "X" it adds to all the correct places.

Here is the code

Code:
Sub PagesBookmarks()

Dim r As Range
Dim x As Integer

Set r = ActiveDocument.Range

x = 0

Do Until x = 10

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

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

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

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

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

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

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

x = x + 1

Loop

End Sub
This is where the problem lies. Word does not automatically increment the number following the name if faced with code asking to add a duplicate name. I have tried to add the code

Code:
Selection.Information(wdActiveEndPageNumber)

which works when you add it to a message box like this

Code:
Sub DisplayPageNo()

MsgBox ("page no -" & Selection.Information(wdActiveEndPageNumber)), vbInformation

End Sub

Can someone you tell me how i would make the code work without getting the error saying that I am attempting to use an invalid bookmark name.

After a bit more thinking I decided to try and change the code for adding the bookmarks and added the line

Code:
mypage = Selection.Information(wdActiveEndPageNumber)
I then replaced the reference as mypage in the main part of the code so instead of

Code:
r.Bookmarks.Add "AIName & Selection.Information(wdActiveEndPageNumber)"
I get...

Code:
r.Bookmarks.Add "AIName & mypage"


I know it needs to be declared so I added Dim as variant and it didnt work. I then tried Dim as String as you see in the code below.

Code:
Sub PagesBookmarks()

Dim r As Range
Dim x As Integer
Dim mypage As String

mypage = Selection.Information(wdActiveEndPageNumber)

Set r = ActiveDocument.Range

x = 0

Do Until x = 10

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
It still doesnt work though!! The reason I switched from Variant to string is I thought it maybe needed to be passed to the bookmark as if it was a text string tagged onto the end of the constant in the name.

Im sure its just a little bit i've got wrong.

Rob
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,224,852
Messages
6,181,403
Members
453,036
Latest member
Koyaanisqatsi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top