Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-15-2021, 12:36 PM
shistris03 shistris03 is offline Macro to select Headings of ALL styles Windows 10 Macro to select Headings of ALL styles Office 2019
Novice
Macro to select Headings of ALL styles
 
Join Date: Jun 2021
Posts: 7
shistris03 is on a distinguished road
Default Macro to select Headings of ALL styles

Hello,



i searched other threads but couldn't find the solution. no experience in vba.

i wish to select all headings with multiple styles all at once and then delete them.

any vba code on this?

i found this macro selecting only heading 1 style. How to add additional styles in the code?

Code:
Sub SelectHeadings()
'
' SelectHeadings Macro
'
'
Dim tempTable As Paragraph
Application.ScreenUpdating = False
ActiveDocument.DeleteAllEditableRanges wdEditorEveryone
For Each tempTable In ActiveDocument.Paragraphs
  'Debug.Print tempTable.Range.Style & " " & wdStyleHeading1
  If tempTable.Style = ActiveDocument.Styles(wdStyleHeading1) Then
    'Debug.Print "aaa"
    tempTable.Range.Editors.Add wdEditorEveryone
  End If
Next
ActiveDocument.SelectAllEditableRanges wdEditorEveryone
ActiveDocument.DeleteAllEditableRanges wdEditorEveryone
Application.ScreenUpdating = True
End Sub

Last edited by macropod; 06-15-2021 at 02:58 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 06-15-2021, 03:04 PM
macropod's Avatar
macropod macropod is offline Macro to select Headings of ALL styles Windows 10 Macro to select Headings of ALL styles Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Each heading can have only one paragraph Style. If you want to delete all headings, simply use Find/Replace to delete all content in each heading Style. That would be way quicker than looping through all paragraphs and testing them. For example:
Code:
Sub DeleteHeadings()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Content.Find
  .ClearFormatting
  .Format = True
  .Text = ""
  .Replacement.Text = ""
  .Wrap = wdFindContinue
  For i = 1 To 9
    .Style = "Heading " & i
    .Execute Replace:=wdReplaceAll
  Next i
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-15-2021, 10:29 PM
shistris03 shistris03 is offline Macro to select Headings of ALL styles Windows 10 Macro to select Headings of ALL styles Office 2019
Novice
Macro to select Headings of ALL styles
 
Join Date: Jun 2021
Posts: 7
shistris03 is on a distinguished road
Default

Thank you so much for your response it works as expected.

The only thing it deletes the section breaks I have to separate each page. Each page is a different letter and I wish to show them on a different page. Is there a way I can retain the section breaks? Sample of word file structure attached.

Thank you for your time.
Attached Files
File Type: docx Letters.docx (28.4 KB, 7 views)
Reply With Quote
  #4  
Old 06-15-2021, 11:58 PM
Guessed's Avatar
Guessed Guessed is offline Macro to select Headings of ALL styles Windows 10 Macro to select Headings of ALL styles Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
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

The section breaks have Heading styles applied to them - what did you think would happen?

If you change the style on the breaks to non-heading styles, you would find the macro doesn't remove them.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 06-16-2021, 12:08 AM
shistris03 shistris03 is offline Macro to select Headings of ALL styles Windows 10 Macro to select Headings of ALL styles Office 2019
Novice
Macro to select Headings of ALL styles
 
Join Date: Jun 2021
Posts: 7
shistris03 is on a distinguished road
Default

Thank you. Didn't realise that you can apply fonts/styles to section breaks.

Perfectly clear. Thank you
Reply With Quote
  #6  
Old 06-16-2021, 01:26 AM
macropod's Avatar
macropod macropod is offline Macro to select Headings of ALL styles Windows 10 Macro to select Headings of ALL styles Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For example:
Code:
Sub DeleteHeadings()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Content.Find
  .ClearFormatting
  .Format = True
  .Text = "^12"
  .Replacement.Text = "^&"
  .Replacement.Style = wdStyleNormal
  .Wrap = wdFindContinue
  .Replacement.ClearFormatting
  .Text = ""
  .Replacement.Text = ""
  For i = 1 To 9
    .Style = "Heading " & i
    .Execute Replace:=wdReplaceAll
  Next i
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
heading styles selection

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to get Styles to move headings. Balliol Word Tables 0 10-12-2019 01:24 PM
Macro to select Headings of ALL styles Applied Styles to Headings in Multi-Level List; now ALL second level headings are 1.XX NNL Word 1 08-09-2017 02:52 PM
Macro to select Headings of ALL styles Help with Styles - What is +Body and +Headings spdoffice Word 8 12-18-2016 11:15 PM
Macro to select Headings of ALL styles Creating new styles and applying numbering to headings - numbering not following from headings Fuzzyflo Word 1 07-26-2016 04:34 AM
Headings and Styles. Balliol Word 2 07-26-2013 12:41 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:04 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