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
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
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
which works when you add it to a message box like this
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
I then replaced the reference as mypage in the main part of the code so instead of
I get...
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.
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
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
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
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)
Code:
r.Bookmarks.Add "AIName & Selection.Information(wdActiveEndPageNumber)"
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
Im sure its just a little bit i've got wrong.
Rob