Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-15-2021, 01:25 PM
JingleBelle JingleBelle is offline Insert Text at Start of Each Paragraph Windows 10 Insert Text at Start of Each Paragraph Office 2016
Novice
Insert Text at Start of Each Paragraph
 
Join Date: Nov 2020
Posts: 18
JingleBelle is on a distinguished road
Default Insert Text at Start of Each Paragraph

I discovered the following code (from a March 31, 2015 thread) while exploring this forum, and it has been very helpful to say the least. I was wondering, though, how to modify it to include multiple styles (if that is even possible). Currently, I have one macro calling on another, calling on another, and on and on.

Thank you in advance for considering my request for help.



Code:
Sub MarkBodyText()


' Author:    A. Lockton (a/k/a Guessed) via msofficeforums.com
' Date:      April 2015
' Notes:     Adds "(XX)" at start of every para styled as specified


Application.ScreenUpdating = False
  WithActiveDocument.Content.Find
   .ClearFormatting
   .Style = "Body Text"           'Changestyle, here
    DoWhile .Execute(Forward:=True, Format:=True) = True
     With .Parent
       If Left(.Text, 1) = vbCr Or Left(.Text, 1) = " " Or Left(.Text, 1) = chr(12) Then  
         'do nothing
       Else
         .InsertBefore "(XX) "
       End If

       If .End = ActiveDocument.Content.End Then
         Exit Do
       Else
         .Move Unit:=wdParagraph, Count:=1
       End If
     End With
   Loop
  EndWith
Application.ScreenUpdating = True
End Sub

Last edited by macropod; 04-15-2021 at 02:53 PM. Reason: Cleaned up post formatting and added code tags
Reply With Quote
  #2  
Old 04-15-2021, 03:34 PM
Charles Kenyon Charles Kenyon is offline Insert Text at Start of Each Paragraph Windows 10 Insert Text at Start of Each Paragraph Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,081
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

When and where do you want to be changing the style? How will Word know which style you want where?
Reply With Quote
  #3  
Old 04-15-2021, 04:05 PM
Guessed's Avatar
Guessed Guessed is offline Insert Text at Start of Each Paragraph Windows 10 Insert Text at Start of Each Paragraph Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

Assuming each additional style is being treated exactly the same way, I would add an array of the target style names and loop it like this
Code:
Sub MarkBodyText()
  ' Author:    A. Lockton (a/k/a Guessed) via msofficeforums.com
  ' Date:      April 2021
  ' Notes:     Adds "(XX)" at start of everypara styled as specified
  Dim arrStyles() As String, i As Integer
  arrStyles = Split("Body Text|Heading 1|Heading 2", "|")  'list of style names
  Application.ScreenUpdating = False
  For i = LBound(arrStyles) To UBound(arrStyles)
    With ActiveDocument.Content.Find
     .ClearFormatting
     .Style = arrStyles(i)      
      Do While .Execute(Forward:=True, Format:=True) = True
       With .Parent
         If Left(.Text, 1) = vbCr Or Left(.Text, 1) = " " Or Left(.Text, 1) = Chr(12) Then
           'do nothing
         Else
           .InsertBefore "(XX) "
         End If
  
         If .End = ActiveDocument.Content.End Then
           Exit Do
         Else
           .Move Unit:=wdParagraph, Count:=1
         End If
       End With
     Loop
    End With
  Next i
  Application.ScreenUpdating = True
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Charles Kenyon; 04-16-2021 at 01:21 AM.
Reply With Quote
  #4  
Old 04-16-2021, 03:14 AM
JingleBelle JingleBelle is offline Insert Text at Start of Each Paragraph Windows 10 Insert Text at Start of Each Paragraph Office 2016
Novice
Insert Text at Start of Each Paragraph
 
Join Date: Nov 2020
Posts: 18
JingleBelle is on a distinguished road
Default

Hey, Charles.

Thank you for getting back to me. The styles (a standard set used repeatedly) are in the document, itself (not in the normal.dot file).
Reply With Quote
  #5  
Old 04-16-2021, 03:17 AM
JingleBelle JingleBelle is offline Insert Text at Start of Each Paragraph Windows 10 Insert Text at Start of Each Paragraph Office 2016
Novice
Insert Text at Start of Each Paragraph
 
Join Date: Nov 2020
Posts: 18
JingleBelle is on a distinguished road
Default

Andrew,


Thank you for adjusting/rewriting the code for me. I will give it a whirl and let you know. Thank you, again!
Reply With Quote
  #6  
Old 04-16-2021, 07:36 AM
JingleBelle JingleBelle is offline Insert Text at Start of Each Paragraph Windows 10 Insert Text at Start of Each Paragraph Office 2016
Novice
Insert Text at Start of Each Paragraph
 
Join Date: Nov 2020
Posts: 18
JingleBelle is on a distinguished road
Default

Andrew,


The code modification works beautifully. It also reduces about a dozen separate macros to one. I appreciate it very much.


Thank you, again!


JB
Reply With Quote
Reply

Tags
add text, all paragraphs, search styles

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Text at Start of Each Paragraph Word macro to insert text at the beginning of paragraph but skip tables ashalles135 Word VBA 5 09-26-2018 09:49 AM
Insert Text at Start of Each Paragraph word macro To insert text at the beginning and at end of paragraph ArieH Word VBA 20 09-10-2017 04:23 PM
Insert Text at Start of Each Paragraph Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank caboy Word VBA 2 04-01-2015 07:00 AM
How to find all numbers that start with a paragraph? csongi12xme Word 7 07-22-2014 07:55 AM
Justify Paragraph Start position with VBA jeff_kaufman Word VBA 2 11-09-2013 12:15 PM

Other Forums: Access Forums

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