Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #7  
Old 01-22-2024, 03:31 PM
Guessed's Avatar
Guessed Guessed is offline Finding,Sorting and creating a new Section VBA Script help Windows 10 Finding,Sorting and creating a new Section VBA Script help Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
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

Try this macro. It harvests any text in the paragraph which is in front of the trigger word - in this case "[Red]" and includes a reference to the nearest preceding Heading.
Code:
Sub GatherRound()
  Dim aRng As Range, aRngHead As Range, aDoc As Document, aDocNew As Document, aTbl As Table, aRow As Row
  Set aDoc = ActiveDocument
  Set aRng = aDoc.Range
  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"
  With aRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[Red]"
    .Forward = True
    Do While .Execute
      aRng.Start = aRng.Paragraphs(1).Range.Start
      Set aRow = aTbl.Rows.Add
      aRow.Cells(2).Range.FormattedText = aRng.FormattedText
      Set aRngHead = aRng.GoToPrevious(wdGoToHeading)
      aRngHead.End = aRngHead.Paragraphs(1).Range.End - 1
      aRow.Cells(1).Range.Text = aRngHead.ListFormat.ListString & vbTab & aRngHead.Text
      aRng.Collapse Direction:=wdCollapseEnd
      aRng.End = aDoc.Range.End
    Loop
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
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