RedditPP Mascot

RedditPP

r/vba • … • u/Feisty_Donkey_2785

VBA for word document to create 4 independent nested boarders

Hello all. I'm having trouble with this, I got this code from AI as i'm not a programmer. I get a compile error: Method or data member not found. It is in the apply precise styling parameters area under With .Line The .Color is what shows the error. Here is the code, and thank you in advanced!!!

Sub CreateFourNestedEdgeBorders() Dim doc As Document Dim headerRange As Range Dim borderShape As Shape Dim i As Integer

Set doc = ActiveDocument

' ========================================================================= ' USER CONFIGURATION: ADJUST YOUR 4 NESTED BORDERS HERE ' (Border 1 is the outermost; Border 4 is the innermost) ' =========================================================================

' 1. ACTIVE BORDERS (True = Draw this border, False = Skip/Disable this border) Dim BorderActive(1 To 4) As Boolean BorderActive(1) = True ' Outermost Layer BorderActive(2) = True ' Second Layer BorderActive(3) = True ' Third Layer BorderActive(4) = True ' Innermost Layer

' 2. COLORS FOR EACH RING (Using standard RGB values) Dim Colors(1 To 4) As Long Colors(1) = RGB(255, 0, 85) ' Border 1: Vivid Magenta/Red Colors(2) = RGB(0, 180, 216) ' Border 2: Electric Cyan Colors(3) = RGB(114, 9, 183) ' Border 3: Deep Purple Colors(4) = RGB(255, 162, 0) ' Border 4: Golden Orange

' 3. THICKNESS FOR EACH RING (In points - can use decimals like 1.5, 3.5, 6) Dim Thickness(1 To 4) As Single Thickness(1) = 5.0 ' Bold outer line Thickness(2) = 2.0 Thickness(3) = 3.5 Thickness(4) = 1.5 ' Fine inner accent line

' 4. SPACING / DISTANCE FROM THE ABSOLUTE EDGE OF THE PAGE (In points) ' To start EXACTLY at the edge of the page, set Spacing(1) to 0. ' Ensure these numbers increase progressively so they nest inside each other properly. Dim Spacing(1 To 4) As Single Spacing(1) = 0 ' 0 means flush against the physical edge of the sheet Spacing(2) = 10 ' 10 points inward from the edge Spacing(3) = 22 ' 22 points inward from the edge Spacing(4) = 32 ' 32 points inward from the edge

' =========================================================================

' Clear out any previous macro-generated borders to prevent duplicates Set headerRange = doc.Sections(1).Headers(wdHeaderFooterPrimary).Range For Each borderShape In headerRange.ShapeRange If borderShape.Name Like "NestedBorder*" Then borderShape.Delete End If Next borderShape

' Get total page setup dimensions Dim pageWidth As Single, pageHeight As Single pageWidth = doc.PageSetup.PageWidth pageHeight = doc.PageSetup.PageHeight

' Set page margins out to 0 so standard text won't artificially constrain background drawing With doc.PageSetup .TopMargin = InchesToPoints(0) .BottomMargin = InchesToPoints(0) .LeftMargin = InchesToPoints(0) .RightMargin = InchesToPoints(0) End With

' Programmatically calculate and draw active nested rectangles Dim maxActiveSpacing As Single maxActiveSpacing = 0

For i = 1 To 4 If BorderActive(i) Then Dim bLeft As Single, bTop As Single, bWidth As Single, bHeight As Single

' Map out the box sizing relative to the page dimensions bLeft = Spacing(i) bTop = Spacing(i) bWidth = pageWidth - (Spacing(i) * 2) bHeight = pageHeight - (Spacing(i) * 2)

' Draw the framing shape inside the header container layer Set borderShape = doc.Sections(1).Headers(wdHeaderFooterPrimary).Shapes.AddShape( _ msoShapeRectangle, bLeft, bTop, bWidth, bHeight, headerRange)

' Apply precise styling parameters With borderShape .Name = "NestedBorder_" & i .Fill.Visible = msoFalse ' Makes inner area transparent so text shows through .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage .RelativeVerticalPosition = wdRelativeVerticalPositionPage .WrapFormat.Type = wdWrapNone .ZOrder msoSendToBack ' Forces lines behind any text layer

With .Line .Visible = msoTrue .Color.RGB = Colors(i) .Weight = Thickness(i) .DashStyle = msoLineSolid End With End With

' Track the innermost active spacing to adjust final text padding If Spacing(i) > maxActiveSpacing Then maxActiveSpacing = Spacing(i) End If End If Next i

' Automatically protect your typing layout by pushing document text clear of the inner border With doc.PageSetup .TopMargin = maxActiveSpacing + 20 .BottomMargin = maxActiveSpacing + 20 .LeftMargin = maxActiveSpacing + 20 .RightMargin = maxActiveSpacing + 20 End With

MsgBox "Successfully generated your nested edge borders!", vbInformation, "Borders Configured" End Sub

— 0