Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-25-2024, 09:13 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

Hey, the script works well in terms of pulling the data before it. I am not sure if its beacuse the document is so large it starts to not respond and I have to hard restart Word. This only happens if the when the script has to find many cases of the value. it does about 80% of the document then it repeats the same data for pages on end. As for pulling lists in front of our search value. Not getting any data. It could be that immeditly after our search value there isnt a list but another one of the "tags" I use.

Example: [Red] [Blue] and/or [Red] #ABC
  • apple
  • orange

that may be an issue not to sure. Again help on this has been more than enough so many thanks for input.
Reply With Quote
  #2  
Old 01-29-2024, 08:27 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

Code:
Sub GatherRoundUpdated()
    Dim aRng As Range, aRngHead As Range, aDoc As Document, aDocNew As Document, aTbl As Table, aRow As Row
    Dim sNum As String, lastPosition As Long
    
    Set aDoc = ActiveDocument
    Set aDocNew = Documents.Add
    Set aTbl = aDocNew.Tables.Add(aDocNew.Range, 1, 2)
    aTbl.Cell(1, 1).Range.Text = "Heading"
    aTbl.Cell(1, 2).Range.Text = "Text"
    
    Set aRng = aDoc.Range
    aRng.Find.ClearFormatting
    aRng.Find.Replacement.ClearFormatting
    aRng.Find.Text = "[Red]"
    aRng.Find.Forward = True
    
    Do While aRng.Find.Execute
        If aRng.Start = lastPosition Then Exit Do ' Exit loop if stuck at the same position
        
        lastPosition = aRng.Start
        
        Set aRngHead = aRng.GoToPrevious(wdGoToHeading)
        If Not aRngHead Is Nothing Then
            aRngHead.End = aRngHead.Paragraphs(1).Range.End - 1
            Set aRow = aTbl.Rows.Add
            
            If aRng.ListFormat.ListType = wdListNoNumbering Then
                aRow.Cells(2).Range.FormattedText = aRng.FormattedText
            Else
                sNum = aRng.ListFormat.ListString
                aRow.Cells(2).Range.Text = sNum & vbTab & aRng.Text
            End If
            
            aRow.Cells(1).Range.Text = aRngHead.ListFormat.ListString & vbTab & aRngHead.Text
        End If
        
        aRng.Collapse Direction:=wdCollapseEnd
        aRng.End = aDoc.Range.End
    Loop
    
    ' Clean up
    Set aRng = Nothing
    Set aDoc = Nothing
    Set aDocNew = Nothing
    Set aTbl = Nothing
    Set aRow = Nothing
End Sub
This was my attempt
Reply With Quote
Reply



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 04:22 PM.


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