Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-19-2019, 06:57 AM
mattbrad mattbrad is offline Macro to wrap each section in a document with some text Mac OS X Macro to wrap each section in a document with some text Office 2019
Novice
Macro to wrap each section in a document with some text
 
Join Date: Jul 2019
Posts: 2
mattbrad is on a distinguished road
Default Macro to wrap each section in a document with some text

I am trying to create a macro that runs through a document section by section and then inserts some text at the start and end of the section.

e.g. Before

Heading 1
Some text here
Heading 2
Some text here
SubHeading2.1
Table here
Sub Heading 2.2


Table Here
Some Text Here

After
Heading 1
STARTSome text hereEND
Heading 2
STARTSome text hereEND
SubHeading2.1
STARTTable hereEND
Sub Heading 2.2
STARTTable Here
Some Text HereEND

When I run ActiveDocuments.Sections.Count, it returns 1 so I can seem to loop over it?

Is this something to do with the way the sections have been added?
Reply With Quote
  #2  
Old 07-19-2019, 08:15 AM
gmaxey gmaxey is online now Macro to wrap each section in a document with some text Windows 10 Macro to wrap each section in a document with some text Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Because those aren't sections. They are paragraphs with Heading Styles applied (or that's what they should be).


Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPar As Paragraph
Dim oRng As Range
  For Each oPar In ActiveDocument.Paragraphs
    Select Case oPar.Style
      Case "Heading 1", "Heading 2", "Heading 3" 'etc.
        Set oRng = oPar.Range
        oRng.End = oRng.End - 1
        oRng.Text = "Start " & oRng.Text & " End"
    End Select
  Next oPar
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 07-22-2019, 02:29 AM
mattbrad mattbrad is offline Macro to wrap each section in a document with some text Mac OS X Macro to wrap each section in a document with some text Office 2019
Novice
Macro to wrap each section in a document with some text
 
Join Date: Jul 2019
Posts: 2
mattbrad is on a distinguished road
Default

Thanks for the clarification and help. The macro you suggested seems to just wrap the title of the section with the "Start" and "End" text. I would like the paragraph under the heading to have the text wrapped.
Reply With Quote
  #4  
Old 07-22-2019, 03:13 AM
gmaxey gmaxey is online now Macro to wrap each section in a document with some text Windows 10 Macro to wrap each section in a document with some text Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

So that would be the "next" paragraph:


Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPar As Paragraph
Dim oRng As Range
  For Each oPar In ActiveDocument.Paragraphs
    Select Case oPar.Style
      Case "Heading 1", "Heading 2", "Heading 3" 'etc.
        Set oRng = oPar.Next.Range
        oRng.End = oRng.End - 1
        oRng.Text = "Start " & oRng.Text & " End"
    End Select
  Next oPar
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to wrap each section in a document with some text Automatic locking section of document AFTER text typed lou1990lou Word VBA 2 01-20-2019 02:39 PM
Macro to wrap each section in a document with some text Auto section break macro - want to have no header on first page of document cspeid03 Word 1 05-17-2017 12:18 PM
Macro to wrap each section in a document with some text Wrap text in text box around image with transparent background pstidsen Word 4 02-08-2016 03:30 PM
Macro to wrap each section in a document with some text Fix a section on first page and wrap around it? Bpilgrim Word 2 09-04-2015 09:50 PM
Macro to wrap each section in a document with some text Macro to insert multiple pictures to word to a specific size and text wrap mescaL Word VBA 3 11-03-2014 10:51 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:39 PM.


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