Check to see if a specific layer name exists

I started this many years ago but now I am changing it up to make it flow a little better. I have a form that you can select different sizes for bleed settings both inside and outside a page space. I want to check to see if the bleed lines layer has been created already. If so you do have an option to continue and create another set or cancel and keep the one that's there. That would happen in this first portion of code before loading the bleed form. The section below that is the code for the form itself plus the form.

The code I have to call out the form for the first step.

Sub BleedLines()
'set apart from Z_Projects 2020.07.30
'added 2021 1222 rs
'check to see if a bleed layer is already created
'continue to add layers
    answer = MsgBox("Bleeds already exsist continue adding more?", vbYesNo, "Add bleeds?")
    If answer = vbNo Then Exit Sub
'load the bleed form
End Sub
Private Sub cmdCancel_Click()
    Unload frmBleeds
End Sub

Private Sub cmdOk_Click()
    ' Recorded 7/25/2016 by Rob Smith
    Dim p As PageSize
    Dim lyr As Layer
    Dim s1 As Shape
    Dim s2 As Shape
    Dim effl As Effect
    Dim eff2 As Effect
    Dim bleedout As Double
    Dim bleedin As Double
    Dim pagewidth As Double
    Dim pageheight As Double

'add new layer and set as bleed lines
    Set lyr = ActivePage.CreateLayer("Layer 2")
    ActiveDocument.ActivePage.GetSize pagewidth, pageheight
    Set s1 = lyr.CreateRectangle(0, pageheight, pagewidth, 0)

'    if typeof ckbRuleThirds is false
'        Set s2 = ActiveLayer.CreateGridBoxes(0#, 0#, 3.5, 2#, 3, 3)
'    End If
'    s2.MoveToLayer ActivePage.GuidesLayer
'    msgbox ( ,vbOKOnly, "Page Size", , "Page Size: " & pagesize.width)
'   lyr.Name = "BleedLines" 'moved towards the end of IF statements to add bleed thickness
'   changed 2021 1222 rs
'   replaced radio buttons to checkboxes
'   If cmd0625 Then bldln = 0.0625
'   If cmd125 Then bldln = 0.125
'   If cmd25 Then bldln = 0.25
    If obout1 Then bleedout = 0.0625
    If obout2 Then bleedout = 0.125
    If obout3 Then bleedout = 0.25
    If obin1 Then bleedin = 0.0625
    If obin2 Then bleedin = 0.125
    If obin3 Then bleedin = 0.25
    lyr.Name = "Bleeds In" & bleedin & " Out" & bleedout
    s1.Rectangle.CornerType = cdrCornerTypeRound
    s1.Rectangle.RelativeCornerScaling = True
    s1.Outline.SetPropertiesEx 0.006945, OutlineStyles(0), CreateCMYKColor(0, 0, 0, 100), ArrowHeads(0), ArrowHeads(0), cdrFalse, cdrFalse, cdrOutlineButtLineCaps, cdrOutlineMiterLineJoin, 0#, 100, MiterLimit:=45#, Justification:=cdrOutlineJustificationMiddle
    s1.Outline.SetProperties Color:=CreateCMYKColor(100, 100, 0, 0)
'   Set inside bleed line to green
    Set eff1 = s1.CreateContour(0, bleedin, 1, 0, CreateRGBColor(51, 204, 102), CreateCMYKColor(0, 0, 0, 100), CreateCMYKColor(0, 0, 0, 100), 0, 0, 2, 4, 15#)
'   Set outside bleed line to red
    Set eff2 = s1.CreateContour(1, bleedout, 1, 0, CreateRGBColor(51, 204, 102), CreateCMYKColor(0, 0, 0, 100), CreateCMYKColor(0, 0, 0, 100), 0, 0, 2, 4, 15#)
    eff2.Contour.OutlineColor = CreateRGBColor(255, 0, 0)
    ActiveDocument.CreateSelection eff2.Contour.ContourGroup, s1
    lyr.Printable = False
    lyr.Editable = False
    ActivePage.Layers("Bleeds In" & bleedin & " Out" & bleedout).Color = CreateRGBColor(153, 0, 0)
    ActivePage.Layers("Layer 1").Activate

    Unload Me
End Sub
  • You might look at the Layers.Find method in the CorelDRAW API documentation.

    Here's an example where I check to see if a page layer named, "foo" exists:

    Sub check_page_layer_foo_exists()
    Dim layThis As Layer
        Set layThis = ActivePage.Layers.Find("foo")
        If Not layThis Is Nothing Then
            MsgBox "The page layer ""foo"" exists."
            MsgBox "The page layer ""foo"" does not exist."
        End If
    End Sub

    You don't really need to create an object to do that, either. I did that to explicitly show that Layers.Find was expected to return a Layer.

    You could just do something like this:

    Sub check_page_layer_foo_exists_2()
        If Not ActivePage.Layers.Find("foo") Is Nothing Then
            MsgBox "The page layer ""foo"" exists."
            MsgBox "The page layer ""foo"" does not exist."
        End If
    End Sub

    Instead of using ActivePage.Layers to check just for page layers, you could use ActiveDocument.MasterPage.Layers to limit it to master layers, or ActivePage.AllLayers to check both page layers and master layers.

    You could also wrap up the capabilities you need into a function in VBA.