VBA Form for Autodesk Inventor Drawing

VBA Form for Autodesk Inventor Drawing

shubham.raturi2308
Enthusiast Enthusiast
325 Views
2 Replies
Message 1 of 3

VBA Form for Autodesk Inventor Drawing

shubham.raturi2308
Enthusiast
Enthusiast

Hey Everyone,

I am writing to request your assistance in creating a VBA form for Autodesk Inventor Drawing Documents. The form should be able to:

  1. Add properties that are not already present in the files.
  2. Generate a PDF in the "C:\PDF export" directory with sub-directories as selected properties, with the file name dynamically changing based on the selected properties.
  3. Rename the file to include the description (System Project Property) and custom properties in the file name.

The form's input methods should include the following:

  • For two properties: ComboBoxes with multi-list values.
  • For the third property: Two Option Buttons as inputs—one for True and one for False.
  • A Command Button to execute the code.

I have made some progress on this but am encountering a "Compile error: Type mismatch."  in this line

 

 

 

Private Sub ExportPDF(doc As DrawingDocument)

 

 

 


I am sharing the work I’ve done so far below. I would greatly appreciate it if you could review it and provide guidance, including step-by-step instructions and the necessary VBA code.

Thank you in advance for your assistance.

Best regards,
Shubham Raturi

 

 

 

' UserForm Code
Private Sub UserForm_Initialize()
    ' Populate ComboBoxes
    With cboProperty1
        .AddItem "Value1"
        .AddItem "Value2"
        .AddItem "Value3"
    End With
    
    With cboProperty2
        .AddItem "OptionA"
        .AddItem "OptionB"
        .AddItem "OptionC"
    End With
    
    ' Set default option button
    optTrue.Value = True
End Sub

Private Sub cmdExecute_Click()
    ' Get the active document
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    ' Add and set custom properties
    AddCustomProperty oDoc, "CustomProperty1", cboProperty1.Value
    AddCustomProperty oDoc, "CustomProperty2", cboProperty2.Value
    AddCustomProperty oDoc, "CustomProperty3", optTrue.Value
    
    ' Export PDF
    ExportPDF oDoc
    
    MsgBox "Custom properties have been added and PDF exported!", vbInformation
    
    Unload Me
End Sub

Private Sub AddCustomProperty(doc As DrawingDocument, propName As String, propValue As Variant)
    On Error Resume Next
    doc.PropertySets("User Defined Properties").Add propValue, propName
    If Err.Number <> 0 Then
        ' Property already exists, update its value
        doc.PropertySets("User Defined Properties").Item(propName).Value = propValue
    End If
    On Error GoTo 0
End Sub

Private Sub ExportPDF(doc As DrawingDocument)
    Dim pdfAddin As TranslatorAddIn
    Dim context As TranslationContext
    Dim options As NameValueMap
    Dim pdfFileName As String
    
    ' Get the PDF translator
    Set pdfAddin = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
    
    ' Create a new translation context and get the PDF options
    Set context = ThisApplication.TransientObjects.CreateTranslationContext
    Set options = context.Options
    
    ' Set PDF options here if needed
    ' options.Value("All_Color_AS_Black") = 1
    
    ' Create the PDF file name
    pdfFileName = "C:\PDF export\" & doc.DisplayName & "_" & txtDescription.Text & "_" & _
                  cboProperty1.Value & "_" & cboProperty2.Value & "_" & IIf(optTrue.Value, "True", "False") & ".pdf"
    
    ' Publish the PDF
    Call pdfAddin.SaveCopyAs(doc, context, options, pdfFileName)
End Sub

' Module Code (in a separate module)
Sub ShowCustomPropertiesForm()
    UserForm1.Show
End Sub

 

 

 

@WCrihfield@Curtis_Waguespack , @Michael.Navara @AndrewHumiston 

0 Likes
326 Views
2 Replies
Replies (2)
Message 2 of 3

Michael.Navara
Advisor
Advisor

My recommendation is to stop wasting your time with VBA. This technology is obsolete and has limited functionality. Try to rewrite your code to VB.NET and WinForms or WPF.

0 Likes
Message 3 of 3

jjstr8
Collaborator
Collaborator

@shubham.raturi2308 :  I'd also recommend moving to iLogic and (eventually) .NET addins.  There's much more capability in those environments.  Regardless of the programming environment, all your code needs a DataMedium to tell the addin what the file name , setting the context type, and creating a NameValueMap for the options.

 

        ' Create a new translation context and get the PDF options
        Set context = ThisApplication.TransientObjects.CreateTranslationContext
        context.Type = kFileBrowseIOMechanism        

        ' Create a NameValueMap object
        Set options = ThisApplication.TransientObjects.CreateNameValueMap

        ' Create a DataMedium object
        Dim pdfDataMedium As dataMedium
        Set pdfDataMedium = ThisApplication.TransientObjects.CreateDataMedium
        
        ' Set PDF options here if needed
        ' options.Value("All_Color_AS_Black") = 1

        ' Create the PDF file name
        pdfFileName = "C:\PDF export\" & doc.DisplayName & "_" & txtDescription.Text & "_" & _
        cboProperty1.Value & "_" & cboProperty2.Value & "_" & IIf(optTrue.Value, "True", "False") & ".pdf"        

        'Set the destination file name
        pdfDataMedium.FileName = pdfFileName
        
        ' Publish the PDF
        Call pdfAddin.SaveCopyAs(doc, context, options, pdfDataMedium)

 

 

0 Likes