Log in

View Full Version : I'm getting two different values for the same variable



msnyder701
05-14-2018, 09:59 PM
I'm using Word 2010. I have the following code:

Public LastBodyPage
Sub tryit()
Selection.GoTo What:=wdGoToBookmark, Name:="AppendixStart"
LastBodyPage = Selection.Information(wdActiveEndPageNumber)


End Sub


Sub vardel()
MsgBox Variables.Item("LastBodyPage").Value
MsgBox LastBodyPage
End Sub

The first procedure puts a particular page number into LastBodyPage, equal to the page number where the bookmark "AppendixStart" is located.

When I run the second procedure, the first MessageBox statement returns a value of "53" (the page number where the bookmark is located. But the second MessageBox statement prints out "50".

Why are the values different???

gmayor
05-15-2018, 12:55 AM
I am surprised that
MsgBox Variables.Item("LastBodyPage").valueproduces anything but an error message as the macro does not know from the code you have posted what 'Variables' refers to.
You could have used
MsgBox Activedocument.Variables.Item("LastBodyPage").valueif there was a document variable of that name to refer to, but again there isn't from your code.
Try the following instead

Public LastBodyPage As Integer
Sub tryit()
Selection.GoTo What:=wdGoToBookmark, Name:="AppendixStart"
LastBodyPage = Selection.Information(wdActiveEndPageNumber)
ActiveDocument.Variables.Item("LastBodyPage").value = LastBodyPage
End Sub


Sub vardel()
MsgBox ActiveDocument.Variables.Item("LastBodyPage").value
MsgBox LastBodyPage
End Sub


or better still

Public iLastBodyPage As Integer
Sub tryit()
iLastBodyPage = ActiveDocument.Bookmarks("AppendixStart").Range.Information(wdActiveEndPageNumber)
ActiveDocument.Variables.Item("LastBodyPage").value = iLastBodyPage
End Sub


Sub vardel()
MsgBox ActiveDocument.Variables.Item("LastBodyPage").value
MsgBox iLastBodyPage 'This line seems superfluous
End Sub