Word VBA, Delete Page
In this article I will explain how you can use VBA for Word to delete a page. To be clear, there isn’t a “Page” collection where you could just call the delete or remove method of its members. The way to accomplish this is to manually select all the content in a page and delete it.
The function below receives as input a page number and deletes it:
Public Sub DeletePage(ByVal intPage As Integer)
Dim flag As Boolean
'the line number before moving to the left
Dim intLineBefore As Integer
'the line number after moving to the left
Dim intLineAfter As Integer
'the page number before moving to the left
Dim intPageNumberBefore As Integer
'the page number after moving to the left
Dim intPageNumberAfter As Integer
'the last page number
Dim intLastPage As Integer
'the last line numnber
Dim intLastLine As Integer
Selection.EndKey unit:=wdStory
intLastPage = Selection.Range.Information(wdActiveEndPageNumber)
intLastLine = Selection.Range.Information(wdFirstCharacterLineNumber)
'moves the cursor to the start of the document
Selection.HomeKey unit:=wdStory
'gets the currrent page number
If Selection.Range.Information(wdActiveEndPageNumber) _
<> intPage Then
flag = True
'keeps going until the page number is found
While flag = True
Selection.EndKey unit:=wdLine
intPageNumberBefore = _
Selection.Range.Information(wdActiveEndPageNumber)
intLineBefore = _
Selection.Range.Information(wdFirstCharacterLineNumber)
Selection.MoveLeft unit:=wdCharacter, Count:=1
intPageNumberAfter = _
Selection.Range.Information(wdActiveEndPageNumber)
intLineAfter = _
Selection.Range.Information(wdFirstCharacterLineNumber)
Selection.MoveRight unit:=wdCharacter, Count:=1
If intLineAfter = intLineBefore And _
intPageNumberBefore = intPageNumberAfter Then
Selection.MoveRight unit:=wdCharacter, Count:=1
End If
'checks if page number has been reached
If Selection.Range.Information(wdActiveEndPageNumber) _
= intPage Then
flag = False
End If
Wend
End If
'move to the start of the page
Selection.HomeKey
flag = True
'keep selecting until the end of the page
While flag = True
Selection.EndKey unit:=wdLine, Extend:=wdExtend
intPageNumberBefore = _
Selection.Range.Information(wdActiveEndPageNumber)
intLineBefore = _
Selection.Range.Information(wdFirstCharacterLineNumber)
Selection.MoveLeft unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
intPageNumberAfter = _
Selection.Range.Information(wdActiveEndPageNumber)
intLineAfter = _
Selection.Range.Information(wdFirstCharacterLineNumber)
Selection.MoveRight unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
If intLineAfter = intLineBefore And intPageNumberBefore _
= intPageNumberAfter Then
Selection.MoveRight unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
End If
'if the end of the page is reached delete
If (Selection.Range.Information(wdActiveEndPageNumber) _
<> intPage) Then
Selection.HomeKey unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft unit:=wdCharacter, Extend:=wdExtend
Selection.Delete
flag = False
ElseIf (Selection.Range.Information(wdActiveEndPageNumber) _
= intLastPage) Then
Selection.EndKey unit:=wdStory, Extend:=wdExtend
Selection.Delete
flag = False
End If
Wend
End Sub
Assume we call the function with the parameter 2:
Sub main()
Call DeletePage(2)
End Sub
The second page will be selected by the function:
Code Description:
The first while loop, moves the cursor down line by line until the target page is reached:
While flag = True
Selection.EndKey unit:=wdLine
intPageNumberBefore = _
Selection.Range.Information(wdActiveEndPageNumber)
intLineBefore = _
Selection.Range.Information(wdFirstCharacterLineNumber)
Selection.MoveLeft unit:=wdCharacter, Count:=1
intPageNumberAfter = _
Selection.Range.Information(wdActiveEndPageNumber)
intLineAfter = _
Selection.Range.Information(wdFirstCharacterLineNumber)
Selection.MoveRight unit:=wdCharacter, Count:=1
If intLineAfter = intLineBefore And _
intPageNumberBefore = intPageNumberAfter Then
Selection.MoveRight unit:=wdCharacter, Count:=1
End If
'checks if page number has been reached
If Selection.Range.Information(wdActiveEndPageNumber) _
= intPage Then
flag = False
End If
Wend
You might be wondering why I used the
instead of the Selection.EndKey
function? The object of the first while loop is to loop through all the lines of the document, page by page. If the user scrolls out and we use the Selection.MoveDown
function the cursor will skip pages and move directly down:Selection.MoveDown
Once the target page number has been found the second while loop selects all the lines in the page. Once the end of the page is reached it deletes the content.
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