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 offline 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: 341
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 offline 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: 341
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: 228
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: 228
vivka is on a distinguished road
Default

Thank you, RobiNew!
Reply With Quote
  #10  
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
  #11  
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: 228
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
  #12  
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
  #13  
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: 228
vivka is on a distinguished road
Default

Nice, you did it!
Reply With Quote
  #14  
Old 03-03-2024, 03:00 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! 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!
Reply With Quote
  #15  
Old 03-03-2024, 03:28 AM
Guessed's Avatar
Guessed Guessed 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
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,994
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
The shorter way to specify your key line and rely ONLY on the default order of parameters is to include extra commas to skip an optional parameter
Set rng = ActiveDocument.Range.GoTo(wdGoToPage, , PgNum)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Guessed; 03-03-2024 at 03:29 PM.
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 11:22 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