2 Replies Latest reply on Sep 1, 2015 7:02 PM by Tim Lewis

    How can I dynamically assign variables in SolidWorks VBA?

    Tim Lewis

      I am trying to create a macro to perform several tasks with custom properties assigned to parts (e.g. download image from a URL, create a local shortcut to a different URL, etc.).  I understand how to access these properties in VBA (as a result of having a previous question answered), but now I would to assign those values to VBA variables matching the name of the corresponding custom property.


      Even if dynamic assignment isn't possible, what is the best way of doing this?  Is there a "dictionary" data structure?  I'd prefer to avoid just throwing them all in an array and having to remember which index corresponds to which property (and trust that they don't get rearranged at some point down the road).

        • Re: How can I dynamically assign variables in SolidWorks VBA?
          Keith Rice

          Not sure it is relevant to your problem, but how are "[downloading] an image of a URL" and "[creating] a local shortcut to a different URL" examples of using SolidWorks custom properties?


          Anyway, if you want a list of key-value pairs (for example, a custom property name and its value) in VBA then you can use a Dictionary object. I personally have not taken advantage of this data structure enough; I tend to use the Collection object, which is very similar to a List<T> in .NET or Java. For example, in a Collection I might have all of the odd items be the custom property names and the even items be the custom property values.


          There's a Collection example in my Macro Library under the VBA section. The Dictionary object works similarly, but you can easily google examples if necessary.



          SolidWorks API Training