|
#1
|
|||
|
|||
Remove normal columns and their content in main story range
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
|
|||
|
|||
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! |
#11
|
|||
|
|||
Hi, RobiNew! It's difficult to find a problem without seeing the whole code. I'd recommend inserting
Code:
rng.Select Code:
rng.Delete |
#12
|
|||
|
|||
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!
|
#13
|
|||
|
|||
Nice, you did it!
|
#14
|
|||
|
|||
Hi, Vivka! Too good to be true. Inserting 'ActiveWindow.View.Type = wdPrintView' before the code in question worked only once or twice. Following your suggestion, I inserted 'rng.Select' before 'rng.Delete' and saw that the whole doc was selected. How would you reset 'rng'? Thanks a lot!
|
#15
|
||||
|
||||
I think there are two problems with your code
1. the PgNum should be an integer, not a string 2. You need to be explicit with the variables you are passing to the GoTo command as Word is not thinking you are asking for a page number. All the variables are optional and Word is expecting them in the following order. expression.GoTo( What , Which , Count , Name ) From your code, you were passing the page number in as a string which Word thought was for the optional "Which" parameter and it appears it was intended to be the "Count" parameter. If you haven't explicitly named the parameter for each variable Word tries to align the variables in the same order as the intellisense (and help file) displays the parameters Try it again with these variations... Code:
Sub DeleteFromPgToEnd() 'Delete pages from PgNum till the doc's end. Dim rng As Range Dim PgNum As Integer PgNum = CInt(InputBox("Enter the number of the page")) Set rng = ActiveDocument.Range.GoTo(What:=wdGoToPage, Count:=PgNum) rng.End = ActiveDocument.Range.End 'rng.Select 'enable for testing '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.Select 'enable for testing rng.Delete End Sub Set rng = ActiveDocument.Range.GoTo(wdGoToPage, , PgNum)
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia Last edited by Guessed; 03-03-2024 at 03:29 PM. |
|
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 |