VBA layer control

Posted on 2006-05-24
Last Modified: 2012-06-22
I have the following VBA script which I wnat to have run when I open a drawing:

This is in ThisDrawing:
Option Explicit

Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
   If UCase(CommandName) Like "OPEN" _
    Or UCase(CommandName) Like "CLOSE" Then
               Call SetLayers
   End If
End Sub

This is in Module1:
Option Explicit
Public Sub SetLayers()

Dim objLayer As AcadLayer
Dim vMatchStrings(0 To 3) As String
Dim sMatchString  'must be declared as variant

vMatchStrings(0) = "RM*"
vMatchStrings(1) = "GROS*"
vMatchStrings(3) = "XREF"
vMatchStrings(2) = "*WALL*"

    For Each objLayer In ThisDrawing.Layers
    For Each sMatchString In vMatchStrings
    If objLayer.Name Like sMatchString Then
            objLayer.LayerOn = True
        If objLayer.Freeze = True Then
           objLayer.Freeze = False
        End If
            Exit For
            objLayer.LayerOn = False
        End If
        'Debug.Print objLayer.Name
    'ThisDrawing.Regen True
End Sub

When I first open a drawing it does not run on the drawing being opened. After I open a drawing and then open another drawing it runs on the previous open drawing.
Question by:fusebox
    1 Comment
    LVL 14

    Accepted Solution

    Your code should be in the Private Sub AcadDocument_BeginClose() for the close event.
    In the open you could place it in the activate but the problem there is it will fire everytime you pick the drawing.



    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now