Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VBA: Having trouble assigning a variable string to SketchSymbolDefinition , getting type Mismatch

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
A.Acheson
408 Views, 2 Replies

VBA: Having trouble assigning a variable string to SketchSymbolDefinition , getting type Mismatch

A.Acheson
Mentor
Mentor

Having trouble assigning a variable string to SketchSymbolDefinition. This works fine in VB.NET.

Anyone know what is going wrong?

AAcheson_0-1642975875589.pngAAcheson_1-1642976079573.png

AAcheson_2-1642976093315.png

 

Public Sub addnote()
    Dim Notes As New ArrayList
    Notes.Add ("Note1")
    Notes.Add ("Note2")
    Notes.Add ("Note3")

    Dim oSketchedSymbol As SketchedSymbol
    Dim oSketchedSymbolDef As SketchedSymbolDefinition
    'This assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument '  a reference to the drawing document.
    
    Dim oSheet As Sheet
    Set oSheet = oDrawDoc.ActiveSheet

    Dim oTG As TransientGeometry
    Set oTG = Inventor.ThisApplication.TransientGeometry
   
    For Each oName In Notes
         Debug.Print oName
         Set oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions(oName) ' Constant Integer or string works fine but string by variable is failing.
        
         Dim oTGPoint As Point2d
         Set oTGPoint = oTG.CreatePoint2d(0, 0)

        ' Add an instance of the sketched symbol definition to the sheet.
        Set oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oTGPoint)
    Next
                'Call MoveNote(oDrawDoc, oSheet, oTG)
    End Sub

 From the API help

SketchedSymbolDefinitions.Item( Index As Variant ) As SketchedSymbolDefinition

IndexVariantInput Variant value that specifies the SketchedSymbolDefinition to return. This can be either a numeric value indicating the index of the item in the collection or it can be a string indicating the sketched symbol definition's name. If an out of range index or a name of a non-existent sketched symbol definition is specified, an error occurs.
If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

VBA: Having trouble assigning a variable string to SketchSymbolDefinition , getting type Mismatch

Having trouble assigning a variable string to SketchSymbolDefinition. This works fine in VB.NET.

Anyone know what is going wrong?

AAcheson_0-1642975875589.pngAAcheson_1-1642976079573.png

AAcheson_2-1642976093315.png

 

Public Sub addnote()
    Dim Notes As New ArrayList
    Notes.Add ("Note1")
    Notes.Add ("Note2")
    Notes.Add ("Note3")

    Dim oSketchedSymbol As SketchedSymbol
    Dim oSketchedSymbolDef As SketchedSymbolDefinition
    'This assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument '  a reference to the drawing document.
    
    Dim oSheet As Sheet
    Set oSheet = oDrawDoc.ActiveSheet

    Dim oTG As TransientGeometry
    Set oTG = Inventor.ThisApplication.TransientGeometry
   
    For Each oName In Notes
         Debug.Print oName
         Set oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions(oName) ' Constant Integer or string works fine but string by variable is failing.
        
         Dim oTGPoint As Point2d
         Set oTGPoint = oTG.CreatePoint2d(0, 0)

        ' Add an instance of the sketched symbol definition to the sheet.
        Set oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oTGPoint)
    Next
                'Call MoveNote(oDrawDoc, oSheet, oTG)
    End Sub

 From the API help

SketchedSymbolDefinitions.Item( Index As Variant ) As SketchedSymbolDefinition

IndexVariantInput Variant value that specifies the SketchedSymbolDefinition to return. This can be either a numeric value indicating the index of the item in the collection or it can be a string indicating the sketched symbol definition's name. If an out of range index or a name of a non-existent sketched symbol definition is specified, an error occurs.
If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Labels (1)
  • VBA
2 REPLIES 2
Message 2 of 3
Michael.Navara
in reply to: A.Acheson

Michael.Navara
Advisor
Advisor
Accepted solution

This is because your variable oName is Variant type in general. You need to cast its value to string and then it works

 

 

 

Sub GetSketchedSymbolByName()
    Dim drw As DrawingDocument
    Set drw = ThisApplication.ActiveDocument
    
    Dim names As New ArrayList
    Call names.Add("test")
    
    For Each name In names
        Debug.Print name
        'Doesn't work because name is Variant in general
        'Set sDef1 = drw.SketchedSymbolDefinitions(name)
        
        Dim defName As String
        defName = name
        Set sDef2 = drw.SketchedSymbolDefinitions(defName)
        
        Debug.Print "RESULT: " & sDef2.name
    Next
End Sub

 

 

 

This is because your variable oName is Variant type in general. You need to cast its value to string and then it works

 

 

 

Sub GetSketchedSymbolByName()
    Dim drw As DrawingDocument
    Set drw = ThisApplication.ActiveDocument
    
    Dim names As New ArrayList
    Call names.Add("test")
    
    For Each name In names
        Debug.Print name
        'Doesn't work because name is Variant in general
        'Set sDef1 = drw.SketchedSymbolDefinitions(name)
        
        Dim defName As String
        defName = name
        Set sDef2 = drw.SketchedSymbolDefinitions(defName)
        
        Debug.Print "RESULT: " & sDef2.name
    Next
End Sub

 

 

 

Message 3 of 3
A.Acheson
in reply to: Michael.Navara

A.Acheson
Mentor
Mentor

Thanks for the confirmation and solution Michael. I was there when I was testing and had read about needing to change to a string but hadn't implemented correctly. VB.NET is more forgiving on these little details.  

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

Thanks for the confirmation and solution Michael. I was there when I was testing and had read about needing to change to a string but hadn't implemented correctly. VB.NET is more forgiving on these little details.  

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report