Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-24-2020, 03:14 AM
stormrider2230 stormrider2230 is offline Macro to add paragraph borders Windows 7 64bit Macro to add paragraph borders Office 2010 64bit
Novice
Macro to add paragraph borders
 
Join Date: Jan 2016
Posts: 3
stormrider2230 is on a distinguished road
Default Macro to add paragraph borders

Hello,

I am new to VBA and struggling to combine code from different sources.
I have a sort of a report that is comprised of several lines of text, that can be considered as parapgraphs.

I need to add a box line, or single cell line arround each paragraph, takes much time if there are many.



I have managed to do something with this code, but this applies border around each line, instead of paragraph.


Dim rng As Range
Dim brd As Border

Set rng = ActiveDocument.Range
With rng.Find
.Text = "Paragraph_starting_word*^13"
.MatchWildcards = True
Do While .Execute
For Each brd In rng.Paragraphs.Borders
With brd
.LineStyle = wdLineStyleSingle
End With
Next brd

rng.Collapse wdCollapseEnd
rng.End = ActiveDocument.Range.End
Loop
End With

I would appreciate help.
Reply With Quote
  #2  
Old 07-24-2020, 04:14 AM
eduzs eduzs is offline Macro to add paragraph borders Windows 10 Macro to add paragraph borders Office 2019
Competent Performer
 
Join Date: May 2017
Posts: 208
eduzs is on a distinguished road
Default

Try this

Code:
Dim rng As range
Dim brd As Border
Dim oPara as Paragraph

Set rng = ActiveDocument.range
With rng.Find
.Text = "Para*^13"
.MatchWildcards = True
Do While .Execute
For Each brd In rng.Paragraphs.Borders
With brd
.LineStyle = wdLineStyleSingle
End With
Next brd
rng.Collapse wdCollapseEnd
rng.End = ActiveDocument.range.End
Loop
End With
For Each oPara In ActiveDocument.Paragraphs
    With oPara
        If .Borders(wdBorderLeft).LineStyle Then
            .range.Select
            Selection.MoveDown unit:=wdParagraph, Count:=1, Extend:=wdExtend
            On Error Resume Next
            Selection.range.Borders(wdhorizontal).LineStyle = wdLineStyleSingle
            On Error GoTo 0
        End If
    End With
Next oPara
__________________
Backup your original file before doing any modification.
Reply With Quote
  #3  
Old 07-24-2020, 04:50 AM
stormrider2230 stormrider2230 is offline Macro to add paragraph borders Windows 10 Macro to add paragraph borders Office 2016
Novice
Macro to add paragraph borders
 
Join Date: Jan 2016
Posts: 3
stormrider2230 is on a distinguished road
Default

Thank you.
This is working on a new (manually created) document, but not on my template.
It must be something with the formatting of my report.

I will have to tweak the template, play with formatting so that code executes corretly.

Thank you once again.
Reply With Quote
  #4  
Old 07-24-2020, 05:20 AM
Guessed's Avatar
Guessed Guessed is offline Macro to add paragraph borders Windows 10 Macro to add paragraph borders Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,732
Guessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of light
Default

It would make a lot more sense to create a style with the border attributes you want and include that style in the Replace formatting.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 07-24-2020, 09:34 AM
eduzs eduzs is offline Macro to add paragraph borders Windows 10 Macro to add paragraph borders Office 2019
Competent Performer
 
Join Date: May 2017
Posts: 208
eduzs is on a distinguished road
Default

Using style as Guessed suggested:
You should first create "MyStyle" style.

Code:
Dim rng As range, brd As Border

Set rng = ActiveDocument.range
With rng.Find
.Text = "Para*^13"
.MatchWildcards = True
Do While .Execute
    rng.Style = "MyStyle"
Loop
End With
__________________
Backup your original file before doing any modification.
Reply With Quote
Reply

Tags
microsoft word, paragraph formatting, vba macro

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to insert paragraph Hockey Word VBA 5 01-28-2020 06:21 AM
Macro to add paragraph borders VBA / macro to set paragraph spacing paulkaye Outlook 4 01-23-2017 06:21 AM
Get paragraph number from macro brice Word VBA 4 06-30-2015 03:29 AM
Macro to add paragraph borders Macro to set page borders Jennifer Murphy Word VBA 1 11-04-2012 02:45 AM
Macro to add paragraph borders Page-crossing borders in a table with hidden between-cell borders tenpaiman Word 2 08-08-2012 07:20 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 12:49 PM.


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