×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

layer names in AutoCAD 2005 & Regular Expressions in VBA

layer names in AutoCAD 2005 & Regular Expressions in VBA

layer names in AutoCAD 2005 & Regular Expressions in VBA

(OP)
I am trying to write a VBA program that will rewrite over some layer names in AutoCAD 2005 that have a $ or &. The default layer manager seems to have a problem with these symbols. So I kind of new to the VBA system I written a couple programs, but I still rely heavily on books and google (love the google). I read that you can use Regular Expressions in VBA, but you need to add some extensions. How do I do that? Is it code like to a library? Can I do that in Microsoft Visual Basic Editor?

Any help would be appreciated. The code below is the first half it should look throu the layers and MsgBox out the ones that have the symbols in them.


Sub Layer_Management()
    ' Improves layer management response time by removing text that lengthen query time
    ' Macro created 02/15/2005 by David Powers
    On Error Resume Next
    
    Dim ABCLayer As AcadLayer
    Dim oReg As RegExp
    
    Set oReg = New RegExp
        With oReg
            .IgnoreCase = False
            .Pattern = ".*?($|&)"
        End With
        
    Set ABCLayer = ThisDrawing.Layers(oReg)
    If Err <> 0 Then
        MsgBox "No layers with $, & exist in drawing."
    End If

RE: layer names in AutoCAD 2005 & Regular Expressions in VBA

First of all, you need to set a reference to the "Microsoft VBScript Regualr Expressions 5.5."  Do this by going to the Tools-References... menu.  Then, this should, at least, get you started...

CODE

Sub AuditLayerNames()
Dim i As AcadLayer
Dim NewLayerName As String
Dim Continue As Integer

    For Each i In ThisDrawing.Layers
        If Not LCase(Left(i.Name, 1)) = "x" Then
        NewLayerName = TestRegExp("\$|\&", i.Name)
        If Not NewLayerName = i.Name Then
            Continue = MsgBox("The Layer " & Chr(34) & i.Name & _
            Chr(34) & " contains inappropriate characters." & _
            Chr(13) & Chr(13) & _
            "Would you like to rename the layer to " & _
            Chr(34) & NewLayerName & Chr(34) & "?" _
            , vbYesNoCancel + vbQuestion, "Rename Layer?")
            If Continue = vbCancel Then
                Exit Sub
            ElseIf Continue = vbYes Then
                i.Name = NewLayerName
            End If
        End If
        End If
    Next i
    
    MsgBox "Finished Layer Name Audit.", vbInformation, "Finished Audit"
End Sub

Function TestRegExp(myPattern As String, myString As String)
Dim objRegExp As RegExp
Dim RetStr As String

    Set objRegExp = New RegExp
    objRegExp.Pattern = myPattern
    objRegExp.IgnoreCase = True
    objRegExp.Global = True

    If (objRegExp.Test(myString) = True) Then
        RetStr = objRegExp.Replace(myString, "-")
    Else
        RetStr = myString
    End If
   
    TestRegExp = RetStr
End Function

********************
What's the best way to get the answers you need??  See FAQ222-2244 for details!

RE: layer names in AutoCAD 2005 & Regular Expressions in VBA

(OP)
First I want to thank you, this helps my understanding out a lot. It looks like the “Function TestRegEXp” is a static inclusive search based on you strings (), you replace the symbol in the TestReg if statement. But I don’t understand “If Not LCase(Left(i.Name, 1)) = "x" Then”. Is there some were I can find what RegExp functions are available (Replace, ect...) and is there a good place / book to read up on VBA functions?

Again Thanks so much

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close