![]() |
|
#1
|
|||
|
|||
|
I'm trying to create a Word macro to remove all columns and their content, but I find that the Column object works only for tables. Mine are normal columns in the main story range. Can someone help? Thanks! |
|
#2
|
|||
|
|||
|
|
|
#3
|
|||
|
|||
|
Thanks a lot, Italophile! But this doesn't work.
Code:
ActiveDocument.Sections(3).PageSetup.TextColumns(2).Delete |
|
#4
|
|||
|
|||
|
Does Intellisense show that TextCoumns has a Delete method? Does the Help text suggest that columns can be deleted?
No. But the documentation I referred you to does state, very clearly, that there must be a minimum of 1 column, and it shows you an example of how to set it back to a single column Code:
myDoc.PageSetup.TextColumns.SetCount NumColumns:=1 |
|
#5
|
|||
|
|||
|
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.
|
|
#6
|
|||
|
|||
|
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
|
|
#7
|
|||
|
|||
|
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
|
|
#8
|
|||
|
|||
|
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.
|
|
#9
|
|||
|
|||
|
Thank you, RobiNew!
|
|
#10
|
|||
|
|||
|
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
|
|
#11
|
|||
|
|||
|
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 |
|
#12
|
|||
|
|||
|
Hi, Vivka! Thank you very much for your code variant. Except for "wdGoToPage, PgNum" (which should be "wdGoToPage, Name:=PgNum") it works all right. However, like mine it deletes all the pages of the docx when inserted into a larger (very large) macro.
If I use a Selection version of the code, it works also when inserted into the larger macro. Surely there must be a way to make the Range version work irrespective of the code context! |
|
#13
|
|||
|
|||
|
Hi, RobiNew! It's difficult to find a problem without seeing the whole code. I'd recommend inserting
Code:
rng.Select Code:
rng.Delete |
|
#14
|
|||
|
|||
|
Hi, Vivka! Solved. All I had to do was to insert 'ActiveWindow.View.Type = wdPrintView' before the code in question (even if the the View was not on Normal). Thanks for being always ready to help!
|
|
#15
|
|||
|
|||
|
Nice, you did it!
|
|
|
|
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 |