×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

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!
  • Students Click Here

*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

Jobs

Extract Mass Properties to Excel

Extract Mass Properties to Excel

Extract Mass Properties to Excel

(OP)
Hi to all.
I'm trying to modify a Catia macro (CAASpoInertia.CATScript located in the CAAScdDmuUseCases module) to export Excel instead to a Msgbox. Thus far, I cannot make it run. I keep getting a Compile error (User-defined type not defined).
Can someone please help me?
Thank you.

Here is the code I'm using,
Option Explicit

Sub CATMain()

' Retrieve the selected component
Dim oSelection As Selection
Set oSelection = CATIA.ActiveDocument.Selection
Dim oProduct As AnyObject
On Error Resume Next
Set oProduct = oSelection.FindObject("CATIAProduct")
If (Err.Number <> 0) Then
MsgBox "No selected product"
Else
On Error GoTo 0

' Compute the inertia
Dim oInertia As AnyObject
Set oInertia = oProduct.GetTechnologicalObject("Inertia")

' Read the inertia data
Dim dMass As Double
dMass = oInertia.Mass
Dim dCoordinates(2)
oInertia.GetCOGPosition dCoordinates

' Display the results
'MsgBox oProduct.Name & ": Mass = " & CStr(dMass) & ", Center of gravity : X = " & _
'CStr(dCoordinates(0)) & ", Y = " + CStr(dCoordinates(1)) & ", Z = " + CStr(dCoordinates(2))

Dim Excel As Object
Dim workbooks As workbooks
Dim workbook As Excel.workbook
Dim worksheet As Excel.worksheet

Set Excel = CreateObject("Excel.Application")
Excel.Visible = True

Set workbooks = Excel.Application.workbooks
Set myworkbook = Excel.workbooks.Add
Set objsheet1 = Excel.Sheets.Add

objsheet1.Cells(1, 1) = oProduct.Name
objsheet1.Cells(1, 2) = CStr(dMass)

End If

End Sub

RE: Extract Mass Properties to Excel

Your problem is in the following:

Dim Excel As Object
Dim workbooks As workbooks
Dim workbook As Excel.workbook
Dim worksheet As Excel.worksheet

You likely do not have the Excel Type Libraries Referenced in your VBA Application so it does not recognize the Object Declarations of Workbooks, Excel.Workbook and Excel.Worksheet.

Select Tools > References and find the Excel Type Library to add to your project. If you are planning on keeping this Excel Version agnostic, declare all of those variable as type Object. Keep in mind you will not be provided with any intellisense/Auto-Complete listings for these objects unless the type library reference is added to your project.

--Doug

RE: Extract Mass Properties to Excel

Hi,

CODE

Option Explicit

Sub CATMain()

'....


'Now for Excel

With CreateObject("Excel.Application")
'This level is the Excel Application

   .Visible = True

   With .workbooks.Add
'This level is the Workbook

       With .WorkSheets(1)
'This level is the first Worksheet

            .Cells(1, 1) = oProduct.Name
            .Cells(1, 2) = CStr(dMass)

       End With

       .SaveAs "YourWorkbookName"

       .Close

   End With
   .Quit
End With
End Sub 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Extract Mass Properties to Excel

(OP)
Doug,
you were absolutely correct. I never thought to check on that.


Skip,
your Excel code worked like a charm.

Thank you so much both.

Yanni

RE: Extract Mass Properties to Excel

That has happened to me enough that I recognized the scenario from the error message smile

--Doug

RE: Extract Mass Properties to Excel

Yes, it helps if you have an appropriate object library. Then you can use familiar application objects like Excel, Workbook and Worksheet variables.

However, if you understand some basic things about an application's object model, in this case, Excel, an object library is not necessary, just more convenient.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

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!


Resources