Excel vba to modify Catia v5 model properties - vba

I am a novice when it comes to CATIA, but I have a fair amount of experience with VBA in excel. I am trying to develop an excel macro that goes through the all the parts in a catia assembly, renames them by splicing the text where i want, and then reorder them (alpha-numeric ascending order). I believe I can write the algorithm for the actual splicing, renaming, reordering bit. What I am struggling with is actually operating Catia using excel. There is not a lot of information on the internet. I have ticked all the boxes in the references section that start with CATIA. I have written this so far:
Dim CATIA as object
Set CATIA = Getobject(,"CATIA.Application")
Dim oMyDoc as Document
Dim oCurrentProd as Product 'I assume the individual parts within CATIA are_
referenced as products?
Set oMyDoc = CATIA.ActiveDocument
If I try and run just the above code, I get an error saying 'Class doesn't support Automation'. Which means my basics are wrong. Would appreciate help on this and any other information that would enable me to complete my task. Thanks.

When running a code from CATIA in VBA, some objects gets an error when they are properly typed. Try declaring those objects without a type or as a variant.
Dim CATIA as 'object
Set CATIA = Getobject(,"CATIA.Application")
Dim oMyDoc as 'Document
Dim oCurrentProd as 'Product
Set oMyDoc = CATIA.ActiveDocument
or
Dim CATIA as variant
Set CATIA = Getobject(,"CATIA.Application")
Dim oMyDoc as variant
Dim oCurrentProd as variant
Set oMyDoc = CATIA.ActiveDocument
I recommend you to first declare them properly typed so you can see the object's methods and properties and afterwards change it to variant

Related

How to instantiate a Graph Chart?

I am currently trying to create a chart, e.g. a piechart through word vba.
The problem is that the machine(s) where this word vba will be run do not have excel available, only word and ms graph.
I have tried to create a chart with ms graph, but I am not able to make it work.
As from the code below, I can create charts with inlineshapes and chart and I get a nice bar chart presented in the word window.
I am not able to instantiate the chart element for Ms Graph. I found a french guide that shows how it should be done, but I am not able to get the "me.graphique1"-part to make sense.
Inlineshapes
Dim vlChart As Chart
Set vlChart = ActiveDocument.InlineShapes.AddChart2.Chart
Graph
Dim vlChart As Graph.Chart
Set vlChart = Me.Graphique1.Object.Application.Chart
I would expect to be able to create a graph object, but I don't know what I am doing.
No guarantees that this will still work, since MS Graph has long been deprecated. The following code sample demonstrates how to insert an MS Graph object and access its data sheet.
This code requires a reference to the MS Graph object library in VBA Tools/References. I highly recommend you use it, at least while writing the code so that you have Intellisense.
Dim grph as Graph.Chart
Dim grphData as Graph.DataSheet
Dim grphApp as Graph.Application
Dim doc as Word.Document
Dim oleF as Word.OLEFormat
Dim rng as Word.Range
Set doc = ActiveDocument
Set rng = doc.Content
Set oleF = doc.InlineShapes.AddOLEObject(ClassType:="MSGraph.Chart.8", Range:=rng).OLEFormat
oleF.DoVerb
Set grphChart = oleF.Object
Set grphApp = grphChart.Application
Set grphData = grphApp.DataSheet
grphData.Cells.Clear
'Now start entering the data and formatting the chart

How can I export point coordinates from a CATIA product to excel

What I'm looking for is a VB script written in either excel or CATIA that can export the coordinates of points in a CATProduct to an excel spreadsheet. The process needs to be as automated as possible due to the large number of points that I am dealing with.
Eventually, I will need to export only specific points and group these points together in 4's to identify what part they belong to.
I have an excel script that allow for points to be imported, but this only takes points from a geometry set and the points in the product I'm looking at are in the part body.
Follow this link here to write to a CSV file which can be imported to excel:
http://www.coe.org/p/fo/et/thread=27438
You'll need to add the excel VBA references files to you Catia VBA Project.
Regarding your point information:
Just to show you how to drill down to a point, I used Insert > Object Resolution of a basic point and included some comments on how to get the coordinates and also where to loop. There is one thing to note, some methods are "marked as restricted" which necessitates the intermediate "hack" of setting the point object to a variant before you can use the "GetCoordinates" sub.
Sub GetPointData()
'---- Begin resolution script for object : Point.1
Dim partDocument1 As PartDocument
Set partDocument1 = CATIA.ActiveDocument
Dim part1 As Part
Set part1 = partDocument1.Part
Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = part1.HybridBodies
Dim hybridBody1 As HybridBody
Set hybridBody1 = hybridBodies1.Item("Geometrical Set.1")
Dim hybridShapes1 As HybridShapes
Set hybridShapes1 = hybridBody1.HybridShapes
Dim hybridShapePointCoord1 As HybridShapePointCoord
Dim XYZ(2) As Variant
Dim var As Variant
'BEGIN LOOP THROUGH YOUR POINTS HERE
Set hybridShapePointCoord1 = hybridShapes1.Item("Point.1")
Set var = hybridShapePointCoord1
var.GetCoordinates XYZ
'WRITE XYZ TO CSV
'NEXT POINT
'END LOOP
'---- End resolution script
End Sub
I believe it can be done, what I would do is to search and select all points in CATProduct, then get parent for each selected point up to the Part, then get coordinates (of course, you need to write everything in Excel if you have the code there).
I don't know if you can upload here your excel vba but shouldn't be so difficult.

User defined type not defined- controlling Word from Excel

I am trying to do some relatively simple copy and pasting from Excel 2007 into Word 2007. I've looked through this site and others, and keep getting hung up on the same thing- the third line n the code below keeps giving me the "User type note defined" error msg. I am really confused since I just lifted this from another solution (and had similar issues with other solutions I tried to lift). Could someone please educate me on what is causing the error, and why?
Sub ControlWord()
' **** The line below gives me the error ****
Dim appWD As Word.Application
' Create a new instance of Word & make it visible
Set appWD = CreateObject("Word.Application.12")
appWD.Visible = True
'Find the last row with data in the spreadsheet
FinalRow = Range("A9999").End(xlUp).Row
For i = 1 To FinalRow
' Copy the current row
Worksheets("Sheet1").Rows(i).Copy
' Tell Word to create a new document
appWD.Documents.Add
' Tell Word to paste the contents of the clipboard into the new document
appWD.Selection.Paste
' Save the new document with a sequential file name
appWD.ActiveDocument.SaveAs Filename:="File" & i
' Close this new word document
appWD.ActiveDocument.Close
Next i
' Close the Word application
appWD.Quit
End Sub
This answer was mentioned in a comment by Tim Williams.
In order to solve this problem, you have to add the Word object library reference to your project.
Inside the Visual Basic Editor, select Tools then References and scroll down the list until you see Microsoft Word 12.0 Object Library. Check that box and hit Ok.
From that moment, you should have the auto complete enabled when you type Word. to confirm the reference was properly set.
As per What are the differences between using the New keyword and calling CreateObject in Excel VBA?, either
use an untyped variable:
Dim appWD as Object
appWD = CreateObject("Word.Application")
or
Add a reference to Microsoft Word <version> Object Library into the VBA project via Tools->References..., then create a typed variable and initialize it with the VBA New operator:
Dim appWD as New Word.Application
or
Dim appWD as Word.Application
<...>
Set appWd = New Word.Application
CreateObject is equivalent to New here, it only introduces code redundancy
A typed variable will give you autocomplete.

Object name changes between Office 2007 and 2003

I had assigned object names to objects in a 2007 Powerpoint file for automation purposes. I ran the automated code in another machine which has office 2003 and found that the object names are dynamically changing there hence the automated code throws an error.
I tried renaming the objects again in that machine using VBA and it works while debugging. But the error throws up when I rerun the automation code.
Is it a compatibility issue between the 2 versions or something else?
that was my problem with differentversions of Word. Generally, Code to the highest version of PowerPoint might not be supported under earlier versions. but sometimes the code works but the method which it refereed may be caused the problem.
I recommend use late binding (... As Object) to solve the problem
revision with an example:
The difference is that you bind the object library in code at run-time. You don't use Tools-References. something like that:
Sub ppt()
Dim olApp As Object
Set olApp = CreateObject("PowerPoint.Application")
End Sub
By "object names", do you mean shape names? Ie the shape's .Name property? This does seem to be buggy.
Consider using tags on the shapes you need to work with. For example, assuming a reference to your shape in oSh, instead of using oSh.Name = "Remember me", do something like:
oSh.Tags.Add "ShapeName","RememberMe"
Then when you need to get a reference to a shape, use a function like:
Function ShapeNamed(oSl as Object, sName as string) as Shape
Dim oSh as Shape
For Each oSh in oSl.Shapes
If oSh.Tags("ShapeName") = sName Then
Set ShapeNamed = oSh
Exit Function
End If
Next
End Function
oSl is declared as Object rather than Slide so you can pass the function Slides, Masters, Layouts, etc.

User-defined type not defined for AppointmentItem in Excel

I copied example at http://support.microsoft.com/kb/220595 to the VBA in Excel.
My code follows:
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim olAppt As Outlook.AppointmentItem
Set olAppt = olApp.CreateItem(olAppointmentItem)
I obtained the following error on the line Dim olAppt As Outlook.AppointmentItem:
"User-defined type not defined".
How should this be fixed?
I use MS Office 2003.
There are four prerequisite steps listed in that article. They are listed right before the code block.
You probably forgot to follow the step two, Adding a reference to Outlook object library.
The only difference is, in VBA the menu item is under Tools, not Project.
You need to refer to all constants by their value, so, as olAppointmentItem = 1 :
Set olAppt = olApp.CreateItem(1)
You can either look up values, for example http://msdn.microsoft.com/en-us/library/aa911356.aspx, or use Outlook's Object Browser to get the values.