PDA

View Full Version : How to delete the last page in word VBA which contains tex boxes



MazAb
04-02-2017, 06:38 PM
Hi everyone,

I have a multi page word document which contains a combination of text, shapes and text boxes.

I want to delete the last page using word vba. I came across the following code by Paul Edstein which works well to some extent

Dim Rng As Range, iPage As Long
iPage = 2
With ActiveDocument
Set Rng = .GoTo(What:=wdGoToPage, Name:=iPage)
Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
Rng.Delete
End With

however, this code deletes everything in the last page except the last textbox located a the bottom left corner of the page. therefore, the last page is not deleted.
I used to following code in addition to the above one but still doesn't work and the textbox and last page are not deleted.

Selection.EndKey Unit:=wdStory
Selection.TypeBackspace

can anyone help please.

BTW, I'm not expert in vba, so some explanation of your codes would be much appreciated.

regards
Maz

macropod
04-02-2017, 08:02 PM
The code as you've posted it deletes the second page, which may or may not be the last page. To delete the last page, use code like:

Sub Demo()
Dim Rng As Range
With ActiveDocument
Set Rng = .GoTo(What:=wdGoToPage, Name:=.ComputeStatistics(wdStatisticPages))
Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
Rng.Delete
End With
End Sub
PS:When posting code, please use the code tags, indicated by the # button on theposting menu. Without them, your code loses whatever structure it had.

MazAb
04-02-2017, 08:17 PM
hi Paul,

I used the code you gave me but for some reasons which I don't understand, the problem still is there.
below is the snapshot of the last page before and after running the code. I circled around the textbox which is giving me hassle. the textbox is not deleted after running the code.

before:
18837

After:
18838

also I deleted the textbox manually and run the code again. it appears that the code doesn't actually delete the page but it deletes the content. see below again the page snapshot after running the code.

18839

thnx a lot

macropod
04-02-2017, 08:38 PM
It's impossible to tell from your screenshot where on the page or what kind of content the reminder is. Can you attach a document to a post with some representative data (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.

MazAb
04-02-2017, 08:50 PM
18840
hi paul,

not sure if I did it correct, but I tried to attach a sample file, I had to delete some of the pages for privacy.
I require a code to delete a selected page entirely.

thanks

macropod
04-02-2017, 10:20 PM
Your document does not appear to have been particularly well designed. All the $$CertNo paragraphs, for example, are located in textboxes in the document body even though their physical location (and that of the table with which they're aligned) suggests they should be in the page footer. Similarly, all the 'Tank Calibration Report on Vertical Tank at $$SiteName, Tank $$TankNo' paragraphs look as if they should be in the page header. Moreover, the lack of paragraph breaks on most pages makes it difficult to write code that would delete content from only the last page - since deleting the page break preceding the last page (without which the original last page will remain) would delete the $$CertNo textbox from that page as well.

I suggest spending some time redoing the document layout so it's easier to work with.

MazAb
04-02-2017, 11:02 PM
thanks Paul,

I see how I can rearrange the file.