Home > Blockchain >  Type mismatch trying to iterate ShapeRange objects to change their line colors in a document
Type mismatch trying to iterate ShapeRange objects to change their line colors in a document

Time:11-11

I am trying to change the line color of all ShapeRanges in my Word document.

This is what I thought I should do:

Private Sub pMakeAllShapesWhite(ByRef uDoc As Word.document)

Dim mycolor As WdColor
mycolor = wdColorWhite

Dim iShapeCount&
iShapeCount = uDoc.Shapes.Count

Dim l&
For l = 1 To uDoc.Shapes.Count

    Dim s As Shape
    Set s = uDoc.Shapes(l) 'Type mismatch
    s.BorderColor = mycolor

Next

iShapeCount returns 144 shapes, but this throws an error:

  Set s = uDoc.Shapes(l) 'Type mismatch

I was told that I get the type mismatch because I am using Shape methods on a ShapeRange.

Ok, so I try the following:

Dim mycolor As WdColor
mycolor = wdColorWhite

Dim iShapeCount&
iShapeCount = uDoc.Shapes.Count

Dim l&
For l = 1 To uDoc.Shapes.Count

    Dim s As ShapeRange
    Set s = uDoc.Shapes(l) 'this throws a type mismatch again. Why?
    
    s.line.BackColor = mycolor

Next

My doc looks like this:

enter image description here

Thank you!

ps: This is the macro that I have recorded:

Sub Makro2()
'
' Makro2 Makro
'
'
    Selection.ShapeRange.Fill.Visible = msoFalse
    Selection.ShapeRange.Fill.Solid
    Selection.ShapeRange.Fill.Transparency = 0#
    Selection.ShapeRange.Line.Weight = 0.25
    Selection.ShapeRange.Line.DashStyle = msoLineSolid
    Selection.ShapeRange.Line.Style = msoLineSingle
    Selection.ShapeRange.Line.Transparency = 0#
    Selection.ShapeRange.Line.Visible = msoTrue
    Selection.ShapeRange.Line.ForeColor.ObjectThemeColor = _
        wdThemeColorBackground1
    Selection.ShapeRange.Line.ForeColor.TintAndShade = 0#
    Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
    Selection.ShapeRange.LockAspectRatio = msoFalse
    Selection.ShapeRange.Rotation = 0#
    Selection.ShapeRange.Left = 27.75
    Selection.ShapeRange.Top = 121#
    Selection.ShapeRange.RelativeHorizontalPosition = _
        wdRelativeHorizontalPositionPage
    Selection.ShapeRange.RelativeVerticalPosition = _
        wdRelativeVerticalPositionPage
    Selection.ShapeRange.RelativeHorizontalSize = wdRelativeHorizontalSizePage
    Selection.ShapeRange.RelativeVerticalSize = wdRelativeVerticalSizePage
    Selection.ShapeRange.Left = CentimetersToPoints(0.98)
    Selection.ShapeRange.LeftRelative = wdShapePositionRelativeNone
    Selection.ShapeRange.Top = CentimetersToPoints(4.27)
    Selection.ShapeRange.TopRelative = wdShapePositionRelativeNone
    Selection.ShapeRange.WidthRelative = wdShapeSizeRelativeNone
    Selection.ShapeRange.HeightRelative = wdShapeSizeRelativeNone
    Selection.ShapeRange.LockAnchor = True
    Selection.ShapeRange.LayoutInCell = False
    Selection.ShapeRange.WrapFormat.AllowOverlap = True
    Selection.ShapeRange.WrapFormat.Side = wdWrapBoth
    Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0)
    Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0)
    Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0.32)
    Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0.32)
    Selection.ShapeRange.WrapFormat.Type = 3
    Selection.ShapeRange.ZOrder 4
End Sub

CodePudding user response:

Dim l&
For l = 1 To uDoc.Shapes.Count

    Dim nShape As Word.Shape
    Set nShape = uDoc.Shapes.item(l)
    
    nShape.line.ForeColor = mycolor
    nShape.line.BackColor = mycolor
    
Next

Thanks for the help everybody!

  • Related