check by VBA if cursor resides in a table on the last page of the current document

Dear Experts:

the following snippet is taken from a macro I regulary run.

I would like to change this snippet to accommodate the following requirement.

Not only is the cursor to reside in a table, but the table is to be located on the last page of the current document.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas

If Not Selection.Information(wdWithInTable) Then
MsgBox ("The cursor does not reside in a a table!" & vbCrLf & _
"Please make sure that the cursor resides in a table!"), vbCritical, "Cursor does not reside in a table!"
Exit Sub
End If
Andreas HermleTeam leaderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi Andreas,
The Selection might include more than one table and the last table might start on an earlier page, so
this code checks that some part of the last table in the Selection is on the last page.
Function IsInTableAtEndOfDoc() As Boolean
    Dim rng As Range
    If Selection.Tables.Count > 0 Then
        Set rng = Selection.Tables(Selection.Tables.Count).Range
        rng.Collapse wdCollapseEnd
        If rng.Information(wdActiveEndPageNumber) = rng.Information(wdNumberOfPagesInDocument) Then
            IsInTableAtEndOfDoc = True
        End If
    End If
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sorry. Forget to put it in a code box.
Andreas HermleTeam leaderAuthor Commented:
Hi Graham,

very nice coding, I am impressed. Thank you very much for it. As usual works like a charm :-)

Regards, Andreasa
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.