![]() |
|
|
|
#1
|
|||
|
|||
|
Thank you! But you can obtain the same result by deleting column breaks with Find/Replace. The code I am trying to find deletes the content of the second column before removing it.
|
|
#2
|
|||
|
|||
|
Hi, everybody! I have devised a different approach. But how do you get rid of the last page? Thanks!
Code:
Sub DeleteFromNrToEnd()
Dim rng As Range
ActiveDocument.PageSetup.TextColumns.SetCount NumColumns:=1
Set rng = ActiveDocument.Range(0, 0)
Set rng = rng.GoTo(What:=wdGoToPage, Name:=4)
Set rng = rng.GoTo(What:=wdGoToBookmark, Name:="\page")
rng.End = ActiveDocument.Range.End
rng.Delete
End Sub
Can this one be improved? Code:
Sub DeleteFromNrToEnd()
Dim rng As Range
ActiveDocument.PageSetup.TextColumns.SetCount NumColumns:=1
Set rng = ActiveDocument.Range(0, 0)
Set rng = rng.GoTo(What:=wdGoToPage, Name:=4)
Set rng = rng.GoTo(What:=wdGoToBookmark, Name:="\page")
rng.End = ActiveDocument.Range.End
rng.Delete
With ActiveDocument
LastChr = .GoTo(wdGoToPage, wdGoToLast).Start
.Range(LastChr - 1, ActiveDocument.Range.End).Delete
End With
End Sub
|
|
#3
|
|||
|
|||
|
Hi, RobiNew! I've tested your code and it does delete the last page (if I understand correctly your objective). For me the following code also deletes the doc's last page:
Code:
Sub Delete_Last_Page()
Dim rng as range
Set rng = ActiveDocument.range.GoTo(wdGoToPage, wdGoToLast)
rng.End = ActiveDocument.range.End
rng.Delete
End sub
|
|
#4
|
|||
|
|||
|
Thank you, Vivka! Nice to see you again. Your code deletes the last page with text. If it is empty, you need the code I posted.
|
|
#5
|
|||
|
|||
|
Thank you, RobiNew!
|
|
#6
|
|||
|
|||
|
I've taken up again the second macro at #6 above, but I get problems.
I have a docx of 7 pages. I need to go to page 4 and then delete pages 4 to 7. The macro works all right on its own, but it deletes all the pages of the docx when inserted into a larger (very large) macro. Can someone fancy why? Thanks! Code:
Sub DeleteFromPgToEnd()
Dim oRng As Range
Set oRng = ActiveDocument.Range
Set oRng = oRng.GoTo(What:=wdGoToPage, Name:=4)
Set oRng = oRng.GoTo(What:=wdGoToBookmark, Name:="\page")
oRng.End = ActiveDocument.Range.End
oRng.Delete 'doesn't delete the last empty page
oRng.Collapse wdCollapseEnd
oRng.Start = oRng.Start - 1
oRng.Delete 'deletes the last empty page
End Sub
|
|
#7
|
|||
|
|||
|
Hi, RobiNew! Welcome back!
Try your slightly changed code: Code:
Sub DeleteFromPgToEnd()Sub Del_From_Pg_To_End()
'Delete pages from PgNum till the doc's end.
Dim rng As range
Dim PgNum As String
PgNum = InputBox("Enter the number of the page")
Set rng = ActiveDocument.range.GoTo(wdGoToPage, PgNum)
rng.End = ActiveDocument.range.End
'If there's a page break before the PgNum page, the pages won't get
'deleted, so move the rng's start:
If rng.Characters.First.Previous = Chr(12) Then
rng.Start = rng.Start - 1
End If
rng.Delete
End Sub
Last edited by vivka; 02-28-2024 at 12:30 PM. Reason: Useful addition |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Use a table in appendix to determine content of main document? | MattMurray | Word | 3 | 07-19-2022 09:16 AM |
Run Script to remove carriage returns on certain columns
|
ryanjohnsond@gmail.com | Excel Programming | 34 | 09-03-2014 10:43 PM |
Normal sort not bringing along other columns
|
Dave Fraser | Excel | 2 | 06-06-2014 11:48 AM |
Using range object to work with multiple columns
|
kjworduser | Word VBA | 1 | 11-01-2013 03:03 AM |
How to remove blank rows from a specified range?
|
Learner7 | Excel | 1 | 04-19-2011 02:45 AM |