×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Macro Fails Because of Component

Macro Fails Because of Component

Macro Fails Because of Component

(OP)
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 --> VBA

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 

RE: Macro Fails Because of Component

CODE --> catvba

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 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close