Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 01-22-2024, 08:59 AM
tonykekw tonykekw is offline Finding,Sorting and creating a new Section VBA Script help Windows 11 Finding,Sorting and creating a new Section VBA Script help Office 2021
Novice
Finding,Sorting and creating a new Section VBA Script help
 
Join Date: Jan 2024
Posts: 13
tonykekw is on a distinguished road
Default

youre right, we don't know. Thats something I will have to go in and re-structure the document for. Disregarding the sentence or paragraph. Only the section headers would work for now.

I wrote a script but it just copy pasted the entire docuemnt twice. Or I recive a runtime error.

Code:
Sub OrganizeSections()
    Dim doc As Document
    Dim sec As Section
    Dim newSection As Section
    
    ' Set the source document
    Set doc = ActiveDocument
    
    ' Disable screen updating to improve performance
    Application.ScreenUpdating = False
    
    ' Loop through each section in the source document
    For Each sec In doc.Sections
        ' Check if the section contains "[A]"
        If InStr(sec.Range.Text, "[A]") > 0 Then
            ' Create a new section after the current one
            Set newSection = doc.Sections.Add
            ' Copy the content of the section to the new section
            newSection.Range.FormattedText = sec.Range.FormattedText
            ' Add a new line after the copied section
            newSection.Range.InsertAfter vbNewLine
        End If
    Next sec
    
    ' Enable screen updating
    Application.ScreenUpdating = True
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with creating a script with VBA lenziwag93 Excel Programming 1 04-20-2022 12:57 AM
Need Help to Script to align all the tables only as of a section to end of doc? Cendrinne Word VBA 4 04-05-2021 11:37 AM
Creating a master spreadsheet for sorting information Notsonerdy Excel Programming 8 05-31-2016 04:39 AM
Creating a list and sorting alphabetically. irvsax Word 4 09-04-2013 11:47 PM
Finding,Sorting and creating a new Section VBA Script help Creating a sorting method. Balliol Word 2 07-25-2013 06:18 AM

Other Forums: Access Forums

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


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