Search code examples
vbacoreldraw

CorelDraw using IF function based on text


I'm trying to create an IF function based off text on the page.

I am working with print merges and populating dozens of merge fields that are hidden and separated into layers with there objects based on which style they go with.

I then manually set visibility = True for that style

I know this syntax is wrong, but for the sake of explaining, what I'm trying to do is;

If Layer("Style") contains text "MyStyleName"

 Layer("MyStyleName").Visible = True

I'm currently using this code with MyStyleLayer1 to MyStyleLayer13 or so

If ActivePage.Layers("MyStyleLayer1").Visible = True Then
ActivePage.Layers("MyStyleLayer1").Visible = False
Else: ActivePage.Layers("MyStyleLayer1").Visible = True
End If

Each style layer is assigned to a keyboard shortcut and manually shown or hidden

Im trying to automate this process


Solution

  • To hide layers with name contian 'Layer' you can use this code

    Sub HideLayer()
    ' Recorded 20.12.2015
    Dim Mylayer As Layer
    Dim searchstring As String
    searchstring = "Layer"
     For Each Mylayer In ActivePage.Layers
     If InStr(1, Mylayer.Name, searchstring) > 0 Then
      Mylayer.Visible = False
     End If
     Next
    End Sub
    

    To show layer change

    Mylayer.Visible = False
    

    to

    Mylayer.Visible = true
    

    To search string in text on page use this code

    Public Sub TextFind()
    Dim s As Shape
    Dim WhatFind as String
    Dim CountFind as integer
    CountFind = 0
    WhatFind = "I"
    For Each s In ActiveDocument.ActivePage.Shapes
        If s.Type = cdrTextShape Then
             If InStr(1, s.Text.Story, WhatFind) > 0 Then
             CountFind=CountFind+1
             End If
       End If 
     Next
    If CountFind > 0 Then ' do what you want when WhatFind had searched in text
    End If
    End Sub