Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-12-2025, 09:11 AM
RobiNew RobiNew is offline Macro to apply border color Windows 11 Macro to apply border color Office 2016
Competent Performer
Macro to apply border color
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default Macro to apply border color


Could someone suggest a shorter code for this macro? Thanks!
Code:
Sub BorderColor()
MyColor = InputBox("Specify color code", "Border Color")
    With Selection.ParagraphFormat
        With .Borders(wdBorderLeft)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = MyColor
        End With
        With .Borders(wdBorderRight)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = MyColor
        End With
        With .Borders(wdBorderTop)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = MyColor
        End With
        With .Borders(wdBorderBottom)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = MyColor
        End With
        With .Borders
            .DistanceFromTop = 1
            .DistanceFromLeft = 4
            .DistanceFromBottom = 1
            .DistanceFromRight = 4
            .Shadow = False
        End With
    End With
    With Options
        .DefaultBorderLineStyle = wdLineStyleSingle
        .DefaultBorderLineWidth = wdLineWidth050pt
        .DefaultBorderColor = MyColor
    End With
End Sub
Reply With Quote
  #2  
Old 04-12-2025, 11:20 AM
vivka vivka is offline Macro to apply border color Windows 7 64bit Macro to apply border color Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Hi, RobiNew! Please, try this one:
Code:
Sub BorderColor()

Dim oBrdr As Variant
Dim MyColor As Long
    
    Application.ScreenUpdating = False
    MyColor = InputBox("Specify color code", "Border Color")

    For Each oBrdr In Array(wdBorderLeft, wdBorderRight, wdBorderTop, wdBorderBottom)
        With Selection.ParagraphFormat.Borders(oBrdr)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = MyColor
        End With
    Next oBrdr
    Application.ScreenUpdating = True
End Sub
It is shorter but not faster. Besides, you may face an issue entering a color code.
Reply With Quote
  #3  
Old 04-13-2025, 12:06 AM
RobiNew RobiNew is offline Macro to apply border color Windows 11 Macro to apply border color Office 2016
Competent Performer
Macro to apply border color
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default

Hi, Vivka! Thank you for your assistance.


I get this error when I enter the color code:
Run-time Error '6': Overflow (The color code is 10027008)


Any idea? All the best, RobiNew
Reply With Quote
  #4  
Old 04-13-2025, 05:32 AM
gmaxey gmaxey is offline Macro to apply border color Windows 10 Macro to apply border color Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

I didn't get your error running Vikka's code. You can also try:

Code:
Sub BorderColor()
Dim oBrdr As Variant
Dim MyColor As Long
    Application.ScreenUpdating = False
    MyColor = 10027008 'InputBox("Specify color code", "Border Color")
    For Each oBrdr In Selection.ParagraphFormat.Borders
      With oBrdr
        .LineStyle = wdLineStyleSingle
        .LineWidth = wdLineWidth050pt
        .Color = MyColor
      End With
    Next oBrdr
   Application.ScreenUpdating = True
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 04-13-2025, 07:30 AM
RobiNew RobiNew is offline Macro to apply border color Windows 11 Macro to apply border color Office 2016
Competent Performer
Macro to apply border color
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default

This one works also for me. Thanks, Greg! Thanks Vivka!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't change border color of Office windows coffent Office 0 12-17-2020 09:21 AM
Macro to apply border color Word 16/19 change font and outside border color (same color) eg is red change to pink jec1 Word VBA 2 12-04-2019 11:32 PM
Macro to apply border color VBA Word - Format ALL Tables - Apply Specific Formatting to Sets of Columns – Font, Border & Width jc491 Word VBA 10 11-04-2015 04:02 PM
Macro to apply border color How to change the color of Character Border? tinfanide Word 4 10-27-2012 06:35 AM
Macro to apply border color Apply font color programatically using VBA Word divakarganta Word VBA 3 08-08-2012 08:05 PM

Other Forums: Access Forums

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