Variables in VBA for Word 2010

107 Views Asked by At

I have this code in VBA for Word 2010:

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

When the vardel procedure is run, the first message box displays "53" and the second time it displays "50".

Why am I getting different values??

2

There are 2 best solutions below

0
SlowLearner On

You're looking at two different things:

The first message box displays something called Variables.item (your code does not compile, what is Variables.item?).

Anyway, the second message box displays something completely different, it displays LastBodyPage which you presumably set using tryit.

0
Darren Bartrup-Cook On

It looks like Variables.Items is part of the document variables that you can set up and assign values to.

LastBodyPage in your tryit procedure looks at the bookmark AppendixStart which I assume you also set up.

So...

  • AppendixStart is bookmarked on page 50
  • The code ThisDocument.Variables.Add ("LastBodyPage"), 53 is used to add the document variable somewhere in your code. This persists between opening & closing the document - so may have been added ages ago.
  • Your code uses ThisDocument.Variables.... rather than just Variables....

With those condition I got the same result as you.