Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

layer names in AutoCAD 2005 & Regular Expressions in VBA 1

Status
Not open for further replies.

Powers99

Technical User
Joined
Feb 15, 2005
Messages
2
Location
US
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
 
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!
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top