Ilogic Read values from Excel & update assembly component occurrences

Ilogic Read values from Excel & update assembly component occurrences

mcloughlin_b
Enthusiast Enthusiast
1,718 Views
7 Replies
Message 1 of 8

Ilogic Read values from Excel & update assembly component occurrences

mcloughlin_b
Enthusiast
Enthusiast

Hi Boffins

 

I am trying to achieve the following from within an Assembly

  1. For each component occurrence in the assembly (top level assembly, subassemblies, parts) lookup a value in an external excel file (Part Number) that matches the inventor part number
  2. populate custom properties for the active occurrence which are found in the corresponding excel row

 

My first stumbling block is an ilogic error message:"Error in rule program format: The rule must contain: Sub Main() ... End Sub" but my code does have these present. I'm sure this is totally simple but I just can't see where the problem lies. Code is below, pieced together with much hair pulling 

 

SyntaxEditor Code Snippet

Sub Main()
'Create variables'set a reference to the assembly component definintion
Dim oAssDoc As AssemblyDocument
oAssDoc = ThisApplication.ActiveDocument
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim ExcelFullName, FileName As String
'Set Excel database
ExcelFullName = ThisDoc.Path & "\" & "Import Files" & "\" & "DB_IMPORT.xlsx"
'Open Excel database
GoExcel.Open(ExcelFullName,"Sheet1")

'Iterate through all of the occurrences
Dim oOcc As ComponentOccurrence
    For Each oOcc In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef)  
    Try
        'Extract FileName of active occurrence 
           Words = oOcc.FullFileName.Split("\")
        FileName = Words(UBound(Words))
        'Define Custom property set
        Dim oPropSet As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
        
        i = GoExcel.FindRow(ExcelFullName, "Sheet1", "Part number", "=", FileName")
        iProperties.Value("Summary", "Comments") = GoExcel.CurrentRowValue("Comments")
        GetProperty(oPropSet, "SYBIZ PART No").Value = GoExcel.CurrentRowValue("SYBIZ PART No")
        GetProperty(oPropSet, "SUPPLYNO").Value = GoExcel.CurrentRowValue("SUPPLYNO")
        GetProperty(oPropSet, "SUPPLYTYPE").Value = GoExcel.CurrentRowValue("SUPPLYTYPE")
        GetProperty(oPropSet, "PBLAST").Value = GoExcel.CurrentRowValue("PBLAST")
        GetProperty(oPropSet, "PPRIME").Value = GoExcel.CurrentRowValue("PPRIME")
        GetProperty(oPropSet, "PTOPCOAT").Value = GoExcel.CurrentRowValue("PTOPCOAT")
        GetProperty(oPropSet, "PMACHINE").Value = GoExcel.CurrentRowValue("PMACHINE")
        GetProperty(oPropSet, "PWELD").Value = GoExcel.CurrentRowValue("PWELD")
        GetProperty(oPropSet, "PBEND").Value = GoExcel.CurrentRowValue("PBEND")
        GetProperty(oPropSet, "PINSTALL").Value = GoExcel.CurrentRowValue("PINSTALL")
        GetProperty(oPropSet, "PASSEMBLE").Value = GoExcel.CurrentRowValue("PASSEMBLE")    
    
        iLogicVb.UpdateWhenDone = True
    
    End Try
   
    Next 
'Close Excel database
GoExcel.Close
End Sub

Function GetProperty(oPropset As PropertySet, iProName As String) As Inventor.Property
    ErHa = "GetProperty: " & iProName
    Dim iPro As Inventor.Property
    Try
        'Attempt to get the iProperty from the document
        iPro = oPropSet.Item(iProName)
    Catch
        'Assume error means not found, so create it
        iPro = oPropSet.Add("", iProName)
    End Try
    Return iPro
End Function

 Can anyone please help?

 

Cheers

 

Bryan

0 Likes
1,719 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable

You need to go through the occurrence document definition to get to the property set and full filename.

Give this a try and let me know how you get on:

 

Sub Main ()
'Create variables'set a reference to the assembly component definintion
Dim oAssDoc As AssemblyDocument
oAssDoc = ThisApplication.ActiveDocument
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oAssDoc.ComponentDefinition
Dim ExcelFullName As String
Dim FileName As String 
'Set Excel database
ExcelFullName = ThisDoc.Path & "\" & "Import Files" & "\" & "DB_IMPORT.xlsx"

'Open Excel database
GoExcel.Open(ExcelFullName,"Sheet1")

'Iterate through all Of the occurrences
Dim oOcc As ComponentOccurrence
    For Each oOcc In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef)  
   ' Try
        'Extract FileName of active occurrence 
        Words = oOcc.Definition.Document.FullFileName.Split("\")
        FileName = Words(UBound(Words))

        'Define Custom property set
        Dim oPropSet As PropertySet = oOcc.Definition.Document.PropertySets.Item("Inventor User Defined Properties")
        
        i = GoExcel.FindRow(ExcelFullName, "Sheet1", "Part number", "=", FileName)
        'iProperties.Value("Summary", "Comments") = GoExcel.CurrentRowValue("Comments")
        GetProperty(oPropSet, "SYBIZ PART No").Value = GoExcel.CurrentRowValue("SYBIZ PART No")
        GetProperty(oPropSet, "SUPPLYNO").Value = GoExcel.CurrentRowValue("SUPPLYNO")
        GetProperty(oPropSet, "SUPPLYTYPE").Value = GoExcel.CurrentRowValue("SUPPLYTYPE")
        GetProperty(oPropSet, "PBLAST").Value = GoExcel.CurrentRowValue("PBLAST")
        GetProperty(oPropSet, "PPRIME").Value = GoExcel.CurrentRowValue("PPRIME")
        GetProperty(oPropSet, "PTOPCOAT").Value = GoExcel.CurrentRowValue("PTOPCOAT")
        GetProperty(oPropSet, "PMACHINE").Value = GoExcel.CurrentRowValue("PMACHINE")
        GetProperty(oPropSet, "PWELD").Value = GoExcel.CurrentRowValue("PWELD")
        GetProperty(oPropSet, "PBEND").Value = GoExcel.CurrentRowValue("PBEND")
        GetProperty(oPropSet, "PINSTALL").Value = GoExcel.CurrentRowValue("PINSTALL")
        GetProperty(oPropSet, "PASSEMBLE").Value = GoExcel.CurrentRowValue("PASSEMBLE")    
    
        iLogicVb.UpdateWhenDone = True
    'Catch    
    
   ' End Try
   
    Next 
'Close Excel database
GoExcel.Close

End Sub

Function GetProperty(oPropset As PropertySet, iProName As String) As Inventor.Property
    ErHa = "GetProperty: " & iProName
    Dim iPro As Inventor.Property
    Try
        'Attempt to get the iProperty from the document
        iPro = oPropSet.Item(iProName)
    Catch
        'Assume error means not found, so create it
        iPro = oPropSet.Add("", iProName)
    End Try
    Return iPro
End Function

 

I commented out the 'Comments' iProp because I wan't sure if it was the active document or occurrence document you were looking to write to. 

0 Likes
Message 3 of 8

mcloughlin_b
Enthusiast
Enthusiast

HI Craig

Thanks for your reply. I ended up recreating the code, which at least now runs but with some problems as follows:

 

  1. I am getting the error message: Conversion from string to type double is not valid and no iproperties are being updated

I have tried to skip excel cells which have no value or null value by using the following

 

SyntaxEditor Code Snippet

If GoExcel.CurrentRowValue("SYBIZ PART No") = ""
        Else If GoExcel.CurrentRowValue("SYBIZ PART No") = 0 Then
        End If

but I'm totally unsure if this has any effect. My revised code is below if you are willing to have a look for me. Many thanks as I am about to start screaming...

 

 SyntaxEditor Code Snippet

Sub Main ()
'Create variables'set a reference to the assembly component definintion
Dim oAssDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim ExcelFullName As String
Dim FileName As String 
'Set Excel database
ExcelFullName = ThisDoc.Path & "\" & "Import Files" & "\" & "DB_IMPORT.xlsx"

'Open Excel database
GoExcel.Open(ExcelFullName,"Sheet1")

'Iterate through each referenced document'Dim oOcc As ComponentOccurrence
    For Each oDoc As Document In oAssDoc.AllReferencedDocuments
    ERHA = "Start"
    Try
        'Extract Part Number of active occurrence 
        Dim oPropSet As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
        oSC = oPropSet.Item("SUBCAT")
        oPN = oPropSet.Item("PART NO")
        oPartNumber = oSC.Value & "-" & oPN.Value 
                    
'        MessageBox.Show(oPartNumber, "Title")

    ErHa = "Define custom property collection"
        Parameter.UpdateAfterChange = True       
        i = GoExcel.FindRow(ExcelFullName, "Sheet1", "Part Number", "=", oPartNumber)
        If GoExcel.CurrentRowValue("SYBIZ PART No") = ""
        Else If GoExcel.CurrentRowValue("SYBIZ PART No") = 0 Then
        End If
        If GoExcel.CurrentRowValue("SYBIZ PART No") <> "" Then
        GetProperty(oPropSet, "SYBIZ PART No").Value = GoExcel.CurrentRowValue("SYBIZ PART No")
        End If
        If GoExcel.CurrentRowValue("SUPPLYNO") = ""
        Else If GoExcel.CurrentRowValue("SUPPLYNO") = 0 Then
        End If        
        If GoExcel.CurrentRowValue("SUPPLYNO") <> "" Then
        GetProperty(oPropSet, "SUPPLYNO").Value = GoExcel.CurrentRowValue("SUPPLYNO")
        Else
        End If
        If GoExcel.CurrentRowValue("SUPPLYTYPE") = ""
        Else If GoExcel.CurrentRowValue("SUPPLYTYPE") = 0 Then
        End If        
        If GoExcel.CurrentRowValue("SUPPLYTYPE") <> "" Then
        GetProperty(oPropSet, "SUPPLYTYPE").Value = GoExcel.CurrentRowValue("SUPPLYTYPE")
        Else
        End If
        If GoExcel.CurrentRowValue("PBLAST") = ""
        Else If GoExcel.CurrentRowValue("PBLAST") = 0 Then
        End If    
        If GoExcel.CurrentRowValue("PBLAST") <> "" Then
        GetProperty(oPropSet, "PBLAST").Value = GoExcel.CurrentRowValue("PBLAST")
        Else
        End If
        If GoExcel.CurrentRowValue("PPRIME") <> "" Then
        GetProperty(oPropSet, "PPRIME").Value = GoExcel.CurrentRowValue("PPRIME")
        Else
        End If
        If GoExcel.CurrentRowValue("PTOPCOAT") <> "" Then
        GetProperty(oPropSet, "PTOPCOAT").Value = GoExcel.CurrentRowValue("PTOPCOAT")
        Else
        End If
        If GoExcel.CurrentRowValue("PMACHINE") <> "" Then
        GetProperty(oPropSet, "PMACHINE").Value = GoExcel.CurrentRowValue("PMACHINE")
        Else
        End If
        If GoExcel.CurrentRowValue("PWELD") <> "" Then
        GetProperty(oPropSet, "PWELD").Value = GoExcel.CurrentRowValue("PWELD")
        Else
        End If
        If GoExcel.CurrentRowValue("PBEND") <> "" Then
        GetProperty(oPropSet, "PBEND").Value = GoExcel.CurrentRowValue("PBEND")
        Else
        End If
        If GoExcel.CurrentRowValue("PINSTALL") <> "" Then
        GetProperty(oPropSet, "PINSTALL").Value = GoExcel.CurrentRowValue("PINSTALL")
        Else
        End If
        If GoExcel.CurrentRowValue("PASSEMBLE") <> "" Then
        GetProperty(oPropSet, "PASSEMBLE").Value = GoExcel.CurrentRowValue("PASSEMBLE")
        Else
        End If
'        If GoExcel.CurrentRowValue("NOTES") <> "" Then
        GetProperty(oPropSet, "NOTES").Value = GoExcel.CurrentRowValue("NOTES")
'        Else'        End If'    Catch    
        
    ErHa = "Update the file"
        iLogicVb.UpdateWhenDone = True
        Catch ex As Exception
        MsgBox("Part: " & oDoc.DisplayName & vbLf & "Code-Part: " & ErHa & vbLf & "Error: " & ex.Message)
    'Catch    
    
    End Try
   
    Next 
'Close Excel database
GoExcel.Close

End Sub

Private ErHa As String = vbNullString

Function GetProperty(oPropset As PropertySet, iProName As String) As Inventor.Property
    ErHa = "GetProperty: " & iProName
    Dim iPro As Inventor.Property
    Try
        'Attempt to get the iProperty from the document
        iPro = oPropSet.Item(iProName)
    Catch
        'Assume error means not found, so create it
        iPro = oPropSet.Add("", iProName)
    End Try
    Return iPro
End Function

 Cheers

0 Likes
Message 4 of 8

Anonymous
Not applicable

This works perfectly for me. Can you try it and let me know if it does for you also. If you receive any errors please post them also.

 

Sub Main ()
'Create variables'set a reference to the assembly component definintion
Dim oAssDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim ExcelFullName As String
Dim FileName As String 
'Set Excel database
ExcelFullName = ThisDoc.Path & "\" & "Import Files" & "\" & "DB_IMPORT.xlsx"

'Open Excel database
GoExcel.Open(ExcelFullName,"Sheet1")

'Iterate through each referenced document'Dim oOcc As ComponentOccurrence
    For Each oDoc As Document In oAssDoc.AllReferencedDocuments
    ErHa = "Start"
    Try
        'Extract Part Number of active occurrence 
        Dim oPropSet As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
        oSC = oPropSet.Item("SUBCAT")
        oPN = oPropSet.Item("PART NO")
        oPartNumber = oSC.Value & "-" & oPN.Value 
                    
        'MessageBox.Show(oPartNumber, "Title")

    ErHa = "Define custom property collection"
        Parameter.UpdateAfterChange = True       
        i = GoExcel.FindRow(ExcelFullName, "Sheet1", "Part Number", "=", oPartNumber)
        
        GetProperty(oPropSet, "SYBIZ PART No").Value = GoExcel.CurrentRowValue("SYBIZ PART No")
        GetProperty(oPropSet, "SUPPLYNO").Value = GoExcel.CurrentRowValue("SUPPLYNO")
        GetProperty(oPropSet, "SUPPLYTYPE").Value = GoExcel.CurrentRowValue("SUPPLYTYPE")
        GetProperty(oPropSet, "PBLAST").Value = GoExcel.CurrentRowValue("PBLAST")
        GetProperty(oPropSet, "PPRIME").Value = GoExcel.CurrentRowValue("PPRIME")
        GetProperty(oPropSet, "PTOPCOAT").Value = GoExcel.CurrentRowValue("PTOPCOAT")
        GetProperty(oPropSet, "PMACHINE").Value = GoExcel.CurrentRowValue("PMACHINE")
        GetProperty(oPropSet, "PWELD").Value = GoExcel.CurrentRowValue("PWELD")
        GetProperty(oPropSet, "PBEND").Value = GoExcel.CurrentRowValue("PBEND")
        GetProperty(oPropSet, "PINSTALL").Value = GoExcel.CurrentRowValue("PINSTALL")
        GetProperty(oPropSet, "PASSEMBLE").Value = GoExcel.CurrentRowValue("PASSEMBLE")
        GetProperty(oPropSet, "NOTES").Value = GoExcel.CurrentRowValue("NOTES")   
        
    ErHa = "Update the file"
        iLogicVb.UpdateWhenDone = True
        Catch ex As Exception
        MsgBox("Part: " & oDoc.DisplayName & vbLf & "Code-Part: " & ErHa & vbLf & "Error: " & ex.Message)
  
    End Try
   
    Next 
'Close Excel database
GoExcel.Close

End Sub

Private ErHa As String = vbNullString

Function GetProperty(oPropset As PropertySet, iProName As String) As Inventor.Property
    ErHa = "GetProperty: " & iProName
    Dim iPro As Inventor.Property
    Try
        'Attempt to get the iProperty from the document
        iPro = oPropSet.Item(iProName)
    Catch
        'Assume error means not found, so create it
        iPro = oPropSet.Add("", iProName)
    End Try
    Return iPro
End Function

 

0 Likes
Message 5 of 8

Anonymous
Not applicable

Hi @mcloughlin_b

 

Sorry was in a rush yesterday and I never properly read through the issue you were having.

 

The following code should handle any blank excel values it come across. It will set the corresponding iProperty to a blank string (""), although you can change this to whatever you need it to do.

 

P.S I only did a few properties to test the code and commented the rest out, but you get the gist of it Smiley Wink

 

Sub Main ()
'Create variables'set a reference to the assembly component definintion
Dim oAssDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim ExcelFullName As String
Dim FileName As String 
'Set Excel database
ExcelFullName = ThisDoc.Path & "\" & "Import Files" & "\" & "DB_IMPORT.xlsx"

'Open Excel database
GoExcel.Open(ExcelFullName,"Sheet1")

'Iterate through each referenced document'Dim oOcc As ComponentOccurrence
    For Each oDoc As Document In oAssDoc.AllReferencedDocuments
    ErHa = "Start"
    Try
        'Extract Part Number of active occurrence 
        Dim oPropSet As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
        oSC = oPropSet.Item("SUBCAT")
        oPN = oPropSet.Item("PART NO")
        oPartNumber = oSC.Value & "-" & oPN.Value 
                    
        'MessageBox.Show(oPartNumber, "Title")

    ErHa = "Define custom property collection"
        Parameter.UpdateAfterChange = True       
        i = GoExcel.FindRow(ExcelFullName, "Sheet1", "Part Number", "=", oPartNumber)
        
        Dim SYBIZ_PART_No As String = GoExcel.CurrentRowValue("SYBIZ PART No")
        If String.IsNullOrEmpty(SYBIZ_PART_No) Then
        GetProperty(oPropSet, "SYBIZ PART No").Value = ""
        Else
        GetProperty(oPropSet, "SYBIZ PART No").Value = SYBIZ_PART_No
        End If
        
        Dim SUPPLYNO As String = GoExcel.CurrentRowValue("SUPPLYNO")
        If String.IsNullOrEmpty(SUPPLYNO) Then
        GetProperty(oPropSet, "SUPPLYNO").Value = ""
        Else
        GetProperty(oPropSet, "SUPPLYNO").Value = SUPPLYNO
        End If
        
        Dim SUPPLYTYPE As String = GoExcel.CurrentRowValue("SUPPLYTYPE")
        If String.IsNullOrEmpty(SUPPLYTYPE) Then
        GetProperty(oPropSet, "SUPPLYTYPE").Value = ""
        Else
        GetProperty(oPropSet, "SUPPLYTYPE").Value = SUPPLYTYPE
        End If
        
        Dim PBLAST As String = GoExcel.CurrentRowValue("PBLAST")
        If String.IsNullOrEmpty(PBLAST) Then
        GetProperty(oPropSet, "PBLAST").Value = ""
        Else
        GetProperty(oPropSet, "PBLAST").Value = PBLAST
        End If
        
            
'        GetProperty(oPropSet, "PPRIME").Value = GoExcel.CurrentRowValue("PPRIME")
'        GetProperty(oPropSet, "PTOPCOAT").Value = GoExcel.CurrentRowValue("PTOPCOAT")
'        GetProperty(oPropSet, "PMACHINE").Value = GoExcel.CurrentRowValue("PMACHINE")
'        GetProperty(oPropSet, "PWELD").Value = GoExcel.CurrentRowValue("PWELD")
'        GetProperty(oPropSet, "PBEND").Value = GoExcel.CurrentRowValue("PBEND")
'        GetProperty(oPropSet, "PINSTALL").Value = GoExcel.CurrentRowValue("PINSTALL")
'        GetProperty(oPropSet, "PASSEMBLE").Value = GoExcel.CurrentRowValue("PASSEMBLE")
'        GetProperty(oPropSet, "NOTES").Value = GoExcel.CurrentRowValue("NOTES")   
        
        ErHa = "Update the file"
        iLogicVb.UpdateWhenDone = True
        
        Catch ex As Exception
        MsgBox("Part: " & oDoc.DisplayName & vbLf & "Code-Part: " & ErHa & vbLf & "Error: " & ex.Message)
  
    End Try
   
    Next 
'Close Excel database
GoExcel.Close

End Sub

Private ErHa As String = vbNullString

Function GetProperty(oPropset As PropertySet, iProName As String) As Inventor.Property
    ErHa = "GetProperty: " & iProName
    Dim iPro As Inventor.Property
    Try
        'Attempt to get the iProperty from the document
        iPro = oPropSet.Item(iProName)
    Catch
        'Assume error means not found, so create it
        iPro = oPropSet.Add("", iProName)
    End Try
    Return iPro
End Function

 

Message 6 of 8

mcloughlin_b
Enthusiast
Enthusiast

Hi Craig

 

That did the trick, working perfectly so far. I'll run some tests on a few assemblies with excel import containing both text and number strings to check.

 

Many thanks for your input.

 

Bryan

0 Likes
Message 7 of 8

heesu_kimHMK7G
Explorer
Explorer

Hello. 

I also wanted to use that content, so I modified the code as follows.

However, when running, an error message such as the image below appears. 

May I know what the problem is??

Sub Main()
    'Create variables and set a reference to the assembly component definition
    Dim oAssDoc As AssemblyDocument
    oAssDoc = ThisApplication.ActiveDocument
    
    Dim ExcelFullName As String
    Dim oStockNo As String
    Dim oStatus As String
    Dim oProject As String
    Dim oPartNumber As String
    Dim Parameter As Object ' Define Parameter object
    Dim i As Integer
    Dim iLogicVb As Object ' Define iLogicVb object
    
    'Set Excel database
    ExcelFullName = "C:\Users\heesu.kim\Desktop\고객사\에이유테크\bomlist.xlsx"
    
    ' Open Excel database
    Dim GoExcel As Object ' Define GoExcel object
    GoExcel = CreateObject("Excel.Application")
    GoExcel.Workbooks.Open(ExcelFullName)
    
    'Iterate through each referenced document
    For Each oDoc As Document In oAssDoc.AllReferencedDocuments
        Dim ErHa As String ' Move ErHa declaration inside the loop
        
        ErHa = "Start"
        Try
            'Extract Part Number of active occurrence
            Dim oPropSet As PropertySet
            oPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")
            oStockNo = oPropSet.Item("스톡 번호").Value
            oStatus = oPropSet.Item("상태").Value
            oProject = oPropSet.Item("PROJECT").Value
            oPartNumber = oStockNo & "-" & oStatus & "-" & oProject
            
            'Define custom property collection
            Parameter.UpdateAfterChange = True
            i = GoExcel.FindRow(ExcelFullName, "BOM", "PROJECT", "=", oPartNumber)
            
            Dim SYBIZ_PART_No As String
            SYBIZ_PART_No = GoExcel.CurrentRowValue("SYBIZ PART No")
            If String.IsNullOrEmpty(SYBIZ_PART_No) Then
                GetProperty(oPropSet, "SYBIZ PART No").Value = ""
            Else
                GetProperty(oPropSet, "SYBIZ PART No").Value = SYBIZ_PART_No
            End If
            
            Dim SUPPLYNO As String
            SUPPLYNO = GoExcel.CurrentRowValue("SUPPLYNO")
            If String.IsNullOrEmpty(SUPPLYNO) Then
                GetProperty(oPropSet, "SUPPLYNO").Value = ""
            Else
                GetProperty(oPropSet, "SUPPLYNO").Value = SUPPLYNO
            End If
            
            Dim SUPPLYTYPE As String
            SUPPLYTYPE = GoExcel.CurrentRowValue("SUPPLYTYPE")
            If String.IsNullOrEmpty(SUPPLYTYPE) Then
                GetProperty(oPropSet, "SUPPLYTYPE").Value = ""
            Else
                GetProperty(oPropSet, "SUPPLYTYPE").Value = SUPPLYTYPE
            End If
            
            Dim PBLAST As String
            PBLAST = GoExcel.CurrentRowValue("PBLAST")
            If String.IsNullOrEmpty(PBLAST) Then
                GetProperty(oPropSet, "PBLAST").Value = ""
            Else
                GetProperty(oPropSet, "PBLAST").Value = PBLAST
            End If
            
            ErHa = "Update the file"
            iLogicVb.UpdateWhenDone = True
            
        Catch ex As Exception
            MsgBox("Part: " & oDoc.DisplayName & vbLf & "Code-Part: " & ErHa & vbLf & "Error: " & ex.Message)
        End Try
    Next
    
    'Close Excel database
    GoExcel.Quit()
    ReleaseObject(GoExcel) ' Release Excel object
    
End Sub

Private Function GetProperty(oPropset As PropertySet, iProName As String) As Inventor.Property
    Dim iPro As Inventor.Property
    Try
        'Attempt to get the iProperty from the document
        iPro = oPropset.Item(iProName)
    Catch
        'Assume error means not found, so create it
        iPro = oPropset.Add("", iProName)
    End Try
    Return iPro
End Function

Private Sub ReleaseObject(ByVal obj As Object)
    Try
        If Not obj Is Nothing Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        End If
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

heesu_kimHMK7G_0-1712285792100.png

Thank you.

0 Likes
Message 8 of 8

A.Acheson
Mentor
Mentor

Hi @heesu_kimHMK7G 

 

Your error is occuring in this block and finding the error will involve stepping through each line within the try catch statement with either a message box or logger statement. If the message appears the line of code is good. When you receive the error message again then you have reached the error line. 

 

It might be as simple as checking the document is modifiable before you begin trying to process the document. Something like this will avoid trying to modify content center or library parts.

Syntax

Document.IsModifiable() As Boolean

and to use in a for loop

 

If oDoc.IsModifiable = False Then Continue For

 

 

Error in this section. 

 

Try
            'Extract Part Number of active occurrence
            Dim oPropSet As PropertySet
            oPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")
            oStockNo = oPropSet.Item("스톡 번호").Value
            oStatus = oPropSet.Item("상태").Value
            oProject = oPropSet.Item("PROJECT").Value
            oPartNumber = oStockNo & "-" & oStatus & "-" & oProject
            
            'Define custom property collection
            Parameter.UpdateAfterChange = True
            i = GoExcel.FindRow(ExcelFullName, "BOM", "PROJECT", "=", oPartNumber)
            
            Dim SYBIZ_PART_No As String
            SYBIZ_PART_No = GoExcel.CurrentRowValue("SYBIZ PART No")
            If String.IsNullOrEmpty(SYBIZ_PART_No) Then
                GetProperty(oPropSet, "SYBIZ PART No").Value = ""
            Else
                GetProperty(oPropSet, "SYBIZ PART No").Value = SYBIZ_PART_No
            End If
            
            Dim SUPPLYNO As String
            SUPPLYNO = GoExcel.CurrentRowValue("SUPPLYNO")
            If String.IsNullOrEmpty(SUPPLYNO) Then
                GetProperty(oPropSet, "SUPPLYNO").Value = ""
            Else
                GetProperty(oPropSet, "SUPPLYNO").Value = SUPPLYNO
            End If
            
            Dim SUPPLYTYPE As String
            SUPPLYTYPE = GoExcel.CurrentRowValue("SUPPLYTYPE")
            If String.IsNullOrEmpty(SUPPLYTYPE) Then
                GetProperty(oPropSet, "SUPPLYTYPE").Value = ""
            Else
                GetProperty(oPropSet, "SUPPLYTYPE").Value = SUPPLYTYPE
            End If
            
            Dim PBLAST As String
            PBLAST = GoExcel.CurrentRowValue("PBLAST")
            If String.IsNullOrEmpty(PBLAST) Then
                GetProperty(oPropSet, "PBLAST").Value = ""
            Else
                GetProperty(oPropSet, "PBLAST").Value = PBLAST
            End If
            
            ErHa = "Update the file"
            iLogicVb.UpdateWhenDone = True
            
        Catch ex As Exception
            MsgBox("Part: " & oDoc.DisplayName & vbLf & "Code-Part: " & ErHa & vbLf & "Error: " & ex.Message)
        End Try

 

 

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