8 Replies Latest reply on Oct 23, 2017 6:05 AM by Mr Omkar Deshpande

    Suggestion for how to Automate Macro

    Stefanie Davis

      I have assembled code that...

      1. Generates Windows username.

      2. Sets the UserName as a property.

      3. Rebuilds the document (allowing any linked values to automatically update).


      Dim swApp As Object

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long


      Sub main()

      Set swApp = Application.SldWorks

      Dim swModelDoc As SldWorks.modelDoc2

      Set swModelDoc = swApp.ActiveDoc




      'get username from windows login

          Dim UserName As String

          UserName = Environ("USERNAME")

          '(i used this to verify code was working)MsgBox UserName


      Dim cpm As SldWorks.CustomPropertyManager

      Set cpm = swModelDoc.Extension.CustomPropertyManager("")

      'delete old "UserName"

          cpm.Delete "UserName"

      'regenerate new value for "UserName"

          cpm.Add2 "UserName", swCustomInfoText, UserName



      'rebulid drawing, so above-mentioned property will update

          Dim modelDoc2 As modelDoc2

          Set modelDoc2 = swApp.ActiveDoc


      End Sub


      All of the above is working correctly.

      What I now want to do is have this code launch every time a drawing is printed (as i want the username to appear on the drawing). 

      What are my options? I was thinking that maybe when the "print" command is activated it automatically happen?  or maybe when a drawing is opened it happens?  Not sure how difficult any of this is.  Thanks!