Word VBA, End of File
In this article I will explain how you can check if you’ve reached the end of a word document using VBA. In the articles below I’ve explained how to get the current page and line number the cursor is on:
Using the code below you can get the last line and page number of the document:
Sub main()
Dim intLastLine As Integer
Dim intLastPage As Integer
'go to the last line
Selection.EndKey unit:=wdStory
'get last line
intLastLine = _
Selection.Range.Information(wdFirstCharacterLineNumber)
'get last page
intLastPage = _
Selection.Range.Information(wdActiveEndPageNumber)
End Sub
By comparing the current position of the cursor with last line and last page we can determine whether we have reached the last line or not.
Note: The line numbers obtained are relative to the page. That means that the line number starts from “1” at the start of each page. That is why we need to have both the page number and the line number to determine if we have reached the end of the document or not.
A complete example of using this method has been used in the article below:
See also:
- Word VBA, Get Current Page Number
- Word VBA, Get Current Line Number
- Word VBA, Move Cursor to End of Document
If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website www.software-solutions-online.com