<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: in VBA Forum</title>
    <link>https://forums.autodesk.com/t5/vba-forum/layer-object-behavior/m-p/303272#M65278</link>
    <description>&amp;gt; Shouldn't the "Set objLayer = ThisDrawing..." lines just overwrite without&lt;BR /&gt;
having to&lt;BR /&gt;
&amp;gt; first clear the variable or is this expected behavior?&lt;BR /&gt;
&lt;BR /&gt;
It would overwrite if the right hand side (ThisDrawing...) did not produce&lt;BR /&gt;
an error.  When the named layer does not exist, an eror is produced when&lt;BR /&gt;
evaluating the RHS, and error handling skips down to the next line without&lt;BR /&gt;
resetting objLayer.  I think you were expecting&lt;BR /&gt;
ThisDrawing.Layers("NonExistentLayer") to return Nothing... it doesn't, it&lt;BR /&gt;
just returns an error.&lt;BR /&gt;
&lt;BR /&gt;
Joe's code gets around this by Joe's knowing that if you .Add a layer that&lt;BR /&gt;
already exists, it returns the existing layer.  I didn't know VBA would do&lt;BR /&gt;
this, and it seems like a useful feature... but to me, intuitively, it seems&lt;BR /&gt;
most "proper" for VBA to throw an error if you try to Add something that&lt;BR /&gt;
already exists.  I'm not complaining, I just wouldn't have guessed it would&lt;BR /&gt;
work like that.&lt;BR /&gt;
&lt;BR /&gt;
James</description>
    <pubDate>Fri, 07 Mar 2003 06:46:00 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2003-03-07T06:46:00Z</dc:date>
    <item>
      <title>Layer object behavior</title>
      <link>https://forums.autodesk.com/t5/vba-forum/layer-object-behavior/m-p/303269#M65275</link>
      <description>In the code snippet below, I am wondering why I have to set the objLayer&lt;BR /&gt;
object to Nothing before I can create the second and third layers, when I&lt;BR /&gt;
take "Set objLayer = Nothing" out the code never changes the objLayer&lt;BR /&gt;
variable besides the first time.  Shouldn't the "Set objLayer =&lt;BR /&gt;
ThisDrawing..." lines just overwrite without having to first clear the&lt;BR /&gt;
variable or is this expected behavior?  I am using VBA 6 w/ Acad2002.&lt;BR /&gt;
Thanks:&lt;BR /&gt;
&lt;BR /&gt;
On Error Resume Next&lt;BR /&gt;
    Set objLayer = ThisDrawing.Layers("S-GRID")&lt;BR /&gt;
&lt;BR /&gt;
    If objLayer Is Nothing Then&lt;BR /&gt;
        Set objLayer = ThisDrawing.Layers.Add("S-GRID")&lt;BR /&gt;
        If objLayer Is Nothing Then&lt;BR /&gt;
            MsgBox "Unable to Add S-GRID Layer " &amp;amp; vbCr &amp;amp; Err.Description&lt;BR /&gt;
        End If&lt;BR /&gt;
&lt;BR /&gt;
        On Error Resume Next&lt;BR /&gt;
        Set objLinetype = ThisDrawing.Linetypes("Center2")&lt;BR /&gt;
&lt;BR /&gt;
        If objLinetype Is Nothing Then&lt;BR /&gt;
            ThisDrawing.Linetypes.Load "Center2", "C:\Program Files\AutoCAD&lt;BR /&gt;
2002\Support\acad.lin"&lt;BR /&gt;
            If Err Then&lt;BR /&gt;
                MsgBox "Error loading CENTER2 linetype" &amp;amp; vbCr &amp;amp;&lt;BR /&gt;
Err.Description&lt;BR /&gt;
            End If&lt;BR /&gt;
            Set objLinetype = ThisDrawing.Linetypes("Center2")&lt;BR /&gt;
        End If&lt;BR /&gt;
&lt;BR /&gt;
        objLayer.Color = 9&lt;BR /&gt;
        objLayer.Linetype = objLinetype&lt;BR /&gt;
    End If&lt;BR /&gt;
&lt;BR /&gt;
    Set objLayer = Nothing&lt;BR /&gt;
    Set objLayer = ThisDrawing.Layers("S-GRID-IDEN")&lt;BR /&gt;
    Debug.Print Err.Description  ' take this out later&lt;BR /&gt;
    If objLayer Is Nothing Then&lt;BR /&gt;
        Set objLayer = ThisDrawing.Layers.Add("S-GRID-IDEN")&lt;BR /&gt;
        If objLayer Is Nothing Then&lt;BR /&gt;
            MsgBox "Unable to Add S-GRID-IDEN Layer " &amp;amp; Err.Description&lt;BR /&gt;
        End If&lt;BR /&gt;
        objLayer.Color = acGreen&lt;BR /&gt;
    End If&lt;BR /&gt;
&lt;BR /&gt;
    Set objLayer = Nothing&lt;BR /&gt;
    Set objLayer = ThisDrawing.Layers("S-GRID-IDEN-TEXT")&lt;BR /&gt;
    Debug.Print Err.Description  ' take this out later too&lt;BR /&gt;
    If objLayer Is Nothing Then&lt;BR /&gt;
        Set objLayer = ThisDrawing.Layers.Add("S-GRID-IDEN-TEXT")&lt;BR /&gt;
        If objLayer Is Nothing Then&lt;BR /&gt;
            MsgBox "Unable to Add S-GRID-IDEN-TEXT Layer " &amp;amp; Err.Description&lt;BR /&gt;
        End If&lt;BR /&gt;
        objLayer.Color = acWhite&lt;BR /&gt;
    End If&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
--&lt;BR /&gt;
matthew g.</description>
      <pubDate>Thu, 06 Mar 2003 11:32:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/layer-object-behavior/m-p/303269#M65275</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2003-03-06T11:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: Layer object behavior</title>
      <link>https://forums.autodesk.com/t5/vba-forum/layer-object-behavior/m-p/303270#M65276</link>
      <description>Matthew,&lt;BR /&gt;
&lt;BR /&gt;
Here's your answer.&lt;BR /&gt;
&lt;BR /&gt;
Joe&lt;BR /&gt;
--&lt;BR /&gt;
&lt;PRE&gt;&lt;BR /&gt;
Dim objLayer As AcadLayer&lt;BR /&gt;
&lt;BR /&gt;
  On Error Resume Next&lt;BR /&gt;
  'load linetype upfront&lt;BR /&gt;
  ThisDrawing.Linetypes.Load "Center2", "C:\Program Files\AutoCAD 2002\Support\acad.lin"&lt;BR /&gt;
  &lt;BR /&gt;
  If Err.Description &amp;lt;&amp;gt; "Duplicate record name" Then&lt;BR /&gt;
    MsgBox "Unknown error loading linetype", vbCritical, "Error loading linetype"&lt;BR /&gt;
    &lt;BR /&gt;
    Else&lt;BR /&gt;
      Err.Clear&lt;BR /&gt;
  End If&lt;BR /&gt;
    &lt;BR /&gt;
  Set objLayer = ThisDrawing.Layers.Add("S-GRID")&lt;BR /&gt;
  &lt;BR /&gt;
  With objLayer&lt;BR /&gt;
    .Color = 9&lt;BR /&gt;
    If Err = 0 Then&lt;BR /&gt;
      .Linetype = "Center2"         'Center2 loaded&lt;BR /&gt;
      &lt;BR /&gt;
      Else&lt;BR /&gt;
        .Linetype = "Continuous"    'no, default to standard linetype&lt;BR /&gt;
    End If&lt;BR /&gt;
  End With&lt;BR /&gt;
  &lt;BR /&gt;
  Set objLayer = ThisDrawing.Layers.Add("S-GRID-IDEN")&lt;BR /&gt;
  &lt;BR /&gt;
  With objLayer&lt;BR /&gt;
    .Color = acGreen&lt;BR /&gt;
  End With&lt;BR /&gt;
  &lt;BR /&gt;
  Set objLayer = ThisDrawing.Layers.Add("S-GRID-IDEN-TEXT")&lt;BR /&gt;
  &lt;BR /&gt;
  With objLayer&lt;BR /&gt;
    .Color = acWhite&lt;BR /&gt;
  End With&lt;BR /&gt;
&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Mar 2003 13:55:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/layer-object-behavior/m-p/303270#M65276</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2003-03-06T13:55:42Z</dc:date>
    </item>
    <item>
      <title>Re:</title>
      <link>https://forums.autodesk.com/t5/vba-forum/layer-object-behavior/m-p/303271#M65277</link>
      <description>&amp;gt; Matthew,&lt;BR /&gt;
&amp;gt; Here's your answer.&lt;BR /&gt;
&amp;gt;&lt;BR /&gt;
&lt;BR /&gt;
Thanks Joe.  So the difference was in loading the linetype up front or in&lt;BR /&gt;
the way that you handled the error trapping?  Perhaps both?  And more&lt;BR /&gt;
importantly, what is the logic behind that?&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
--&lt;BR /&gt;
matthew g.</description>
      <pubDate>Thu, 06 Mar 2003 15:47:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/layer-object-behavior/m-p/303271#M65277</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2003-03-06T15:47:41Z</dc:date>
    </item>
    <item>
      <title>Re:</title>
      <link>https://forums.autodesk.com/t5/vba-forum/layer-object-behavior/m-p/303272#M65278</link>
      <description>&amp;gt; Shouldn't the "Set objLayer = ThisDrawing..." lines just overwrite without&lt;BR /&gt;
having to&lt;BR /&gt;
&amp;gt; first clear the variable or is this expected behavior?&lt;BR /&gt;
&lt;BR /&gt;
It would overwrite if the right hand side (ThisDrawing...) did not produce&lt;BR /&gt;
an error.  When the named layer does not exist, an eror is produced when&lt;BR /&gt;
evaluating the RHS, and error handling skips down to the next line without&lt;BR /&gt;
resetting objLayer.  I think you were expecting&lt;BR /&gt;
ThisDrawing.Layers("NonExistentLayer") to return Nothing... it doesn't, it&lt;BR /&gt;
just returns an error.&lt;BR /&gt;
&lt;BR /&gt;
Joe's code gets around this by Joe's knowing that if you .Add a layer that&lt;BR /&gt;
already exists, it returns the existing layer.  I didn't know VBA would do&lt;BR /&gt;
this, and it seems like a useful feature... but to me, intuitively, it seems&lt;BR /&gt;
most "proper" for VBA to throw an error if you try to Add something that&lt;BR /&gt;
already exists.  I'm not complaining, I just wouldn't have guessed it would&lt;BR /&gt;
work like that.&lt;BR /&gt;
&lt;BR /&gt;
James</description>
      <pubDate>Fri, 07 Mar 2003 06:46:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/layer-object-behavior/m-p/303272#M65278</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2003-03-07T06:46:00Z</dc:date>
    </item>
    <item>
      <title>Re:</title>
      <link>https://forums.autodesk.com/t5/vba-forum/layer-object-behavior/m-p/303273#M65279</link>
      <description>&amp;gt; It would overwrite if the right hand side (ThisDrawing...) did not produce&lt;BR /&gt;
&amp;gt; an error.  &lt;BR /&gt;
(snip)&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Thank you extremely for the explanation, it makes a lot more sense now.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
--&lt;BR /&gt;
matthew g.</description>
      <pubDate>Fri, 07 Mar 2003 10:50:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/layer-object-behavior/m-p/303273#M65279</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2003-03-07T10:50:16Z</dc:date>
    </item>
  </channel>
</rss>

