6 Replies Latest reply on Sep 1, 2015 9:40 AM by Leon Wurr


    Willie Roelofs

      Hi I'm trying to execute the following macro:

      2012 SOLIDWORKS API Help - Get Appearance Filename Example (VBA)


      At line:

      Debug.Print "First appearance's filename: " & swAppearance.FileName


      I get the error:

      Run-time error "91":


      Object variable or With block variable not set.


      1. File is open.

      2. File is active

      3. File is saved.

      4. File has an appearance


      What am I doing wrong?

        • Re: swAppearance
          Willie Roelofs

          When I hover over swAppearance in debug mode it's been set to "Nothing".


          Am I missing a reference library?

            • Re: swAppearance
              Leon Wurr

              Try this instead:


              ' Preconditions: Model document is open that
              '                has at least one appearance
              '                applied to it.
              ' Postconditions: None
              Option Explicit
              Dim swApp As SldWorks.SldWorks
              Dim swModel As SldWorks.ModelDoc2
              Dim swModelDocExt As SldWorks.ModelDocExtension
              Dim swAppearance As SldWorks.RenderMaterial
              Dim lNbrAppearances As Long, i As Long
              Dim varAppearances As Variant
              Sub main()
                  Set swApp = Application.SldWorks
                  Set swModel = swApp.ActiveDoc
                  Set swModelDocExt = swModel.Extension
                  lNbrAppearances = swModelDocExt.GetRenderMaterialsCount
                  varAppearances = swModelDocExt.GetRenderMaterials2(swThisDisplayState, Nothing)
                  Debug.Print "Number of appearances: " & lNbrAppearances
                  ' If 1 or more appearances where applied to the model,
                  ' then get the filename of the first appearance applied
                  If lNbrAppearances > 0 Then
                      'Set swAppearance = swModelDocExt.GetMaterial(1, "Default")
                      Set swAppearance = varAppearances(0)
                      Debug.Print "First appearance's filename: " & swAppearance.FileName
                      Debug.Print "No appearances applied to this model."
                  End If
              End Sub