Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-16-2023, 10:39 AM
RobiNew RobiNew is offline Remove normal columns and their content in main story range Windows 10 Remove normal columns and their content in main story range Office 2016
Competent Performer
Remove normal columns and their content in main story range
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Question 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!
Reply With Quote
  #2  
Old 11-16-2023, 10:44 AM
Italophile Italophile is online now Remove normal columns and their content in main story range Windows 11 Remove normal columns and their content in main story range Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

See PageSetup.TextColumns property (Word) | Microsoft Learn
Reply With Quote
  #3  
Old 11-16-2023, 11:16 AM
RobiNew RobiNew is offline Remove normal columns and their content in main story range Windows 10 Remove normal columns and their content in main story range Office 2016
Competent Performer
Remove normal columns and their content in main story range
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks a lot, Italophile! But this doesn't work.
Code:
ActiveDocument.Sections(3).PageSetup.TextColumns(2).Delete
Reply With Quote
  #4  
Old 11-16-2023, 12:45 PM
Italophile Italophile is online now Remove normal columns and their content in main story range Windows 11 Remove normal columns and their content in main story range Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

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
Reply With Quote
  #5  
Old 11-16-2023, 03:03 PM
RobiNew RobiNew is offline Remove normal columns and their content in main story range Windows 10 Remove normal columns and their content in main story range Office 2016
Competent Performer
Remove normal columns and their content in main story range
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 11-17-2023, 07:41 AM
RobiNew RobiNew is offline Remove normal columns and their content in main story range Windows 10 Remove normal columns and their content in main story range Office 2016
Competent Performer
Remove normal columns and their content in main story range
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 11-18-2023, 03:50 AM
vivka vivka is offline Remove normal columns and their content in main story range Windows 7 64bit Remove normal columns and their content in main story range Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 11-18-2023, 04:36 AM
RobiNew RobiNew is offline Remove normal columns and their content in main story range Windows 10 Remove normal columns and their content in main story range Office 2016
Competent Performer
Remove normal columns and their content in main story range
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 11-18-2023, 04:55 AM
vivka vivka is offline Remove normal columns and their content in main story range Windows 7 64bit Remove normal columns and their content in main story range Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Thank you, RobiNew!
Reply With Quote
  #10  
Old 02-28-2024, 08:54 AM
RobiNew RobiNew is offline Remove normal columns and their content in main story range Windows 10 Remove normal columns and their content in main story range Office 2016
Competent Performer
Remove normal columns and their content in main story range
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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
Reply With Quote
  #11  
Old 02-28-2024, 12:04 PM
vivka vivka is offline Remove normal columns and their content in main story range Windows 7 64bit Remove normal columns and their content in main story range Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

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
Reply With Quote
  #12  
Old 02-29-2024, 01:19 AM
RobiNew RobiNew is offline Remove normal columns and their content in main story range Windows 10 Remove normal columns and their content in main story range Office 2016
Competent Performer
Remove normal columns and their content in main story range
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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!
Reply With Quote
  #13  
Old 02-29-2024, 07:07 AM
vivka vivka is offline Remove normal columns and their content in main story range Windows 7 64bit Remove normal columns and their content in main story range Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! It's difficult to find a problem without seeing the whole code. I'd recommend inserting
Code:
rng.Select
before
Code:
rng.Delete
to see rng. Probably it needs resetting.
Reply With Quote
  #14  
Old 02-29-2024, 09:50 AM
RobiNew RobiNew is offline Remove normal columns and their content in main story range Windows 10 Remove normal columns and their content in main story range Office 2016
Competent Performer
Remove normal columns and their content in main story range
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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!
Reply With Quote
  #15  
Old 02-29-2024, 09:53 AM
vivka vivka is offline Remove normal columns and their content in main story range Windows 7 64bit Remove normal columns and their content in main story range Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Nice, you did it!
Reply With Quote
Reply



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
Remove normal columns and their content in main story range Run Script to remove carriage returns on certain columns ryanjohnsond@gmail.com Excel Programming 34 09-03-2014 10:43 PM
Remove normal columns and their content in main story range Normal sort not bringing along other columns Dave Fraser Excel 2 06-06-2014 11:48 AM
Remove normal columns and their content in main story range Using range object to work with multiple columns kjworduser Word VBA 1 11-01-2013 03:03 AM
Remove normal columns and their content in main story range How to remove blank rows from a specified range? Learner7 Excel 1 04-19-2011 02:45 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:52 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft