Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro Fails Because of Component

Status
Not open for further replies.

jzecha

Aerospace
Jan 20, 2016
235
US
I have the following section of a large Macro that fails when it encounters a Component, instead of a product.

What is the best way to handle components since they do not have a save feature?

Error Message:
Run-Time error -2147467259 (80004005)
The Method ReferenceProduct Failed

It fails at the this line:
If Right(sel.Item(X).LeafProduct.ReferenceProduct.Parent.Name, 7) = "Product" Then

Section of Code
Code:
Dim sel As Selection
Set sel = CATIA.ActiveDocument.Selection
sel.Search "CATAsmSearch.Product,all"
For X = 1 To sel.Count

If X = 1 Then
Input1TopProduct = sel.Item(X).LeafProduct.ReferenceProduct.Parent.Product.PartNumber
End If

'Checking To See If Current Item X is a Product
If Right(sel.Item(X).LeafProduct.ReferenceProduct.Parent.Name, 7) = "Product" Then
'    If MsgBox(sel.Item(X).LeafProduct.ReferenceProduct.Parent.Name, vbOKCancel, "Product") = vbCancel Then
'    MsgBox ("Canceled")
'    Exit Sub
'    End If

Set oDoc1 = sel.Item(X).LeafProduct.ReferenceProduct.Parent
Set oProduct1 = oDoc1.Product

'Get The Current Product Number
    InputProd = oProduct1.PartNumber
    
'Check To See If Suffix Has Already Been Applied
If Right(InputProd, Input1CharacterLength) = InputSuffix1 Then
'Do Nothing
Else

'Creating The Current Product Number with Suffix
    InputsuffixedProd = oProduct1.PartNumber & InputSuffix1
    
 'Removing Periods From Product Number to Prevent Any Errors in Saving
        InputsuffixedProd = Replace(InputsuffixedProd, ".", "_")
        InputsuffixedProd = Replace(InputsuffixedProd, ".", "_")
        InputsuffixedProd = Replace(InputsuffixedProd, "/", "_")
        InputsuffixedProd = Replace(InputsuffixedProd, "#", "_")
        InputsuffixedProd = Replace(InputsuffixedProd, "____", "_")
        InputsuffixedProd = Replace(InputsuffixedProd, "___", "_")
        InputsuffixedProd = Replace(InputsuffixedProd, "__", "_")

'Changing the Current ProductNumber To Include Suffix
    oProduct1.PartNumber = InputsuffixedProd

'Saving Current Product As New Name Into Input1-1 Folder
    oDoc1.SaveAs Folder1 & "\" & oProduct1.PartNumber & ".CATProduct"
End If
End If


'Checking To See If Current Item X is a Part
If Right(sel.Item(X).LeafProduct.ReferenceProduct.Parent.Name, 4) = "Part" Then
'    If MsgBox(sel.Item(V).LeafProduct.ReferenceProduct.Parent.Name, vbOKCancel, "Part") = vbCancel Then
'    MsgBox ("Canceled")
'    Exit Sub
'    End If

Set oDoc2 = sel.Item(X).LeafProduct.ReferenceProduct.Parent
Set oPart1 = oDoc2.Product

'Get The Current Part Number
    InputPart = oPart1.PartNumber
    
'Check To See If Suffix Has Already Been Applied
If Right(InputPart, Input1CharacterLength) = InputSuffix1 Then
'Do Nothing
Else

'Creating The Current Part Number with Suffix
    Input1suffixedPart = oPart1.PartNumber & InputSuffix1
    
'Removing Periods From Part Number to Prevent Any Errors in Saving
    Input1suffixedPart = Replace(Input1suffixedPart, ".", "_")
    Input1suffixedPart = Replace(Input1suffixedPart, "/", "_")
    Input1suffixedPart = Replace(Input1suffixedPart, "#", "_")
    Input1suffixedPart = Replace(Input1suffixedPart, "____", "_")
    Input1suffixedPart = Replace(Input1suffixedPart, "___", "_")
    Input1suffixedPart = Replace(Input1suffixedPart, "__", "_")
    
'Changing the Current ProductNumber To Include Suffix
    oPart1.PartNumber = Input1suffixedPart
    
'Saving Current Part As New Name Into Input1-1 Folder
    oDoc2.SaveAs Folder1 & "\" & oPart1.PartNumber & ".CATPart"
End If
End If

Next
 
Replies continue below

Recommended for you

Code:
Function IsComponent(inProduct As Product) As Boolean
    'DESCRIPTION: Function to to check if a product is actually a component.
    'INPUT: Pass the function a product (inProduct) to check.
    'OUTPUT: Returns boolean value on whether the inProduct is a component or not.
    
    Dim testPartNum As String
    testPartNum = inProduct.PartNumber
    
    Dim parentPartNum As String
    parentPartNum = inProduct.ReferenceProduct.Parent.Product.PartNumber
    
    If testPartNum = parentPartNum Then
        IsComponent = True
    Else
        IsComponent = False
    End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top