Consulting

Results 1 to 2 of 2

Thread: I'm getting two different values for the same variable

  1. #1

    I'm getting two different values for the same variable

    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???

  2. #2
    I am surprised that
    MsgBox Variables.Item("LastBodyPage").value
    produces 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").value
    if 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
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •