AnsweredAssumed Answered

Auto-generate barcode on drawings

Question asked by Darius Teichroew on Jun 14, 2017
Latest reply on Jun 14, 2017 by Darius Teichroew

This is probably easy for those of you who are fluent in APIs and VB, but I am stuck trying to get a macro to run at a workflow state. In short, I have this macro that creates a CODE128 barcode image and places it on my drawing. This works fine. However, what I want is for it to auto-generate when it is opened and saved IF it is in a certain workflow state. Below is my code currently, and I've highlighted in red the part that I'm stuck on. I'm not sure if the IF THEN is written correctly, but for some reason, I can't get the "workflowState = aFile.CurrentState" to work correctly. It says "Object variable or With block variable not set".

 

Any help would be appreciated!

 

Sub main()

 

  pic_name = "barcode1" ' The name of a sketch picture on the drawing.

  pic_path = TempPath() & "barcode.emf" ' The path to store the temporary barcode picture on a disk.

  Dim swCustProp As CustomPropertyManager

  Dim mde As ModelDocExtension

  Dim doc As ModelDoc2

  Dim swApp As SldWorks.SldWorks

  Set swApp = Application.SldWorks

  Set doc = swApp.ActiveDoc  ' We will place the barcode into the currently active drawing.

  Set mde = doc.Extension

  Set swCustProp = mde.CustomPropertyManager("")

Dim aFile As IEdmFile5

  Dim workflowState As IEdmState5

  workflowState = aFile.CurrentState

  If workflowState = "MODIFIED STANDARD - IN CE" Then

  doc.SelectByName 0, pic_name  ' If we already have a barcode picture on the drawing, let's delete it.

  doc.EditDelete

 

  Dim ss As StrokeScribeClass  ' The barcode generator object.

  Set ss = CreateObject("STROKESCRIBE.StrokeScribeClass.1")

  Dim bool As Boolean

  Dim val As String

  Dim OrderNumber As String

  Dim resolved As Boolean

  bool = swCustProp.Get5("OrderNumber", False, val, OrderNumber, resolved)

  ss.Alphabet = CODE128B  ' We want to create a Code128 barcode.

  ss.Text = OrderNumber  ' A text to be encoded in the barcode.

  ss.TextBelow = " "

 

  rc = ss.SavePicture(pic_path, EMF, 50, 50)  ' This will store a 50x50mm barcode picture in a temporary file.

  If rc > 0 Then  ' In a case of error, this displays the error description message.

    MsgBox ss.ErrorDescription

    Exit Sub

  End If

 

  doc.EditSketch

  Dim SkPicture As SketchPicture   ' This loads the picture from the temporary file.

  Set SkPicture = doc.SketchManager.InsertSketchPicture(pic_path)

  SkPicture.SetSize 0.05, 0.035, True  ' Specifying barcode's position and size.

  SkPicture.SetOrigin 0.185, 0.011

  Dim feat As Feature  ' Let's change the name of the barcode object. This allows fo find and delete an old picture if we

  Set feat = SkPicture.GetFeature()                        ' need to update the barcode.

  feat.Name = pic_name

  doc.EditSheet

  Kill pic_path  ' We don't need the temporary picture file anymore.

  End If

End Sub

Outcomes