How to update CATIA part in vba? - vba

I am trying to edit the part dimension in CATIA by changing the dimension values in the design table in the excel file. Everytime when i change the values i should manually update the part model. I want to update it automatically through VBA code and save it.
I tried
Sub CATMain()
CATIA.DisplayFileAlerts = False
Dim part As PartDocument
Set part = CATIA.ActiveDocument
part.Update
part.SaveAs "D:\E\CSE\.....\Part2.CATPart"
End Sub
and it is not working.
How can we update and save it??

You called ".Update" on the Document object, not the Part-object!
Answer is:
Dim part As PartDocument
Set partDoc = CATIA.ActiveDocument
partDoc.Part.Update

Your Code is correct and it should be working. Do you get any errors?
If you simply want an automatic update try to change your settings ...
Goto Tools->Options Then in the options dialog, goto Infrastructure Tree Node, and expand that and goto Part Infrastructure. Now on the right pane in the General Tab, Make sure you select Automatic for the Updates.
If this doesn't work you could try to the Part.UpdateObject objectToUpdate method to update the individual feature(s) that need to be updated.

Related

How to make copies of a not-saved part in CATIA in assembly evironment by VBA?

I have an CATIA assembly created automatically by VBA Excel. Now I need to add 6 same bolts to that, for example. Theoretically, I can add some pieces of VBA code to create each bolt in that assembly as the separate one, which seems to be so cumbersome. Is it possible to create a bolt (not saved yet, because I want to see how it is in 3D CAITA environment first) and make copies of it as the others in the assembly, like the way we often do interactively in CATIA. If possilbe, please tell me how to do that. Many thanks!
If I understand your request correctly, you would like to have 6 copies of the not yet saved CATPart in the Assembly.
You can achieve this with a simple copy/paste.
For example:
Sub CATMain()
Dim oDoc As Document
Dim oSel As Object
Dim aFilter(0)
Dim oSelected
Dim oRef As Reference
Set oDoc = CATIA.ActiveDocument
Set oSel = oDoc.Selection
oSel.Clear
aFilter(0) = "Product"
oSelected = oSel.SelectElement2(aFilter, "Select Object in the tree which you want to insert six times or ESC..", True)
If oSelected = "Cancel" Then
Debug.Print "KO"
Exit Sub
End If
oSel.Copy
Dim i As Integer
For i = 0 To 6
oSel.Clear
oSel.Add oDoc.Product
oSel.Paste
Next
End Sub
You can of course replace the system to pick product you want to copy to the automatic way, depend on your demanded logic.

SolidWorks VBA - Translating API Help into useable code

I'd like to do what feels like a fairly simple task, and I've found the specific API Help pages which should make it clear, but, I can't actually make things work.
The Key steps that I would like to achieve are:
Rename the active document
Update References to this document to accommodate new name
Save active document.
This help page shows the Usage for renaming the doc, and under the "Remarks" heading, includes links to the next two steps, mentioning them off hand as if implementing them would be easy.
https://help.solidworks.com/2020/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~RenameDocument.html?verRedirect=1
The trouble is, I'm a bit of a VBA beginner - usually I get by with the 'record' function, and then tidying things up from there - but undertaking the steps above manually doesn't result in anything being recorded at all for one reason or another.
Assuming I am able to pass in the item to be renamed (I'll define a variable at the start of the Sub for this e.g. swModel = swApp.ActiveDoc), and the new name (NewName = "NEW NAME HERE"), How would I translate the Help API into a Sub that I can actually run?
Two of them suggest declaring as a Function, and one as a Public Interface - I've never used these before - do these just run in a standard Module? Do I need to write a 'master Sub' to call the different functions sequentially, or could these be included directly in the sub, if they're only to be used once?
[Feeling a little lost - it's demoralizing when the help files aren't all that helpful]
Let me know if there's any more information missing that I can add to improve my question - as I said, I'm fairly new to this coding thing...
The "record" function is sometimes a good point to start but there are a lot of functions it can't recognize while you execute them manually.
The API Help is then useful to find out how to use a specific function.
In almost every example the use of a specific method (e.g. RenameDocument) is only shown abstract. There is always a instance variable which shows you the object-type needed to call this method. So you can use these in every sub you want, but beforehand need access to the specific instance objects.
For your example the RenameDocument method is called with an object of the type IModelDocExtension. First thing for you to do is to get this object and then you can call the method as described in the help article.
Under Remarks in the article you find additional information for what you maybe have to do before or after calling a method.
For your example it is mentioned that the renaming takes permanently place after saving the document.
And finally here is what you want to do with some VBA code:
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Sub main()
' get the solidworks application object
Set swApp = Application.SldWorks
'get the current opened document object
Set swModel = swApp.ActiveDoc
' get the modeldocextension object
Dim swModelExtension As ModelDocExtension
Set swModelExtension = swModel.Extension
Dim lRet As Long
lRet = swModelExtension.RenameDocument("NEW NAME")
If lRet = swRenameDocumentError_e.swRenameDocumentError_None Then
MsgBox "success renaming"
Else
MsgBox "failed with error: " & lRet
End If
End Sub
Afterwars you have to process the return value to check for errors described in this article: https://help.solidworks.com/2020/English/api/swconst/SolidWorks.Interop.swconst~SolidWorks.Interop.swconst.swRenameDocumentError_e.html

VB.Net equivalent for (CustomizationContext) in VBA

I'm busy with some word automation and have run into an issue whereby a context menu within a document has items in, that I wish to remove.
Once the document is open, through vba I can remove these items by running the following code;
[VBA]
Dim oContextMenu As CommandBar
Dim oContextMenuItem As CommandBarControl
'Make changes to the ActiceDocument only (this is needed to make any changes to this document).
CustomizationContext = ActiveDocument
For Each oContextMenu In ActiveDocument.CommandBars
If oContextMenu.Type = MsoBarType.msoBarTypePopup Then 'Loop through all the context menus of type (msoBarTypePopup)
For Each oContextMenuItem In oContextMenu.Controls
If (InStr(oContextMenuItem.Caption, "Smokeball")) Then
oContextMenuItem.Delete
End If
Next
End If
Next
If I execute this code and check the document, all contextMenu sub items that contain the text "smokeball" are removed.
When I try move this code to my VB.NET solution (I have no choice of language, so VB it is), I get errors on the CustomizationContext = ActiveDocument line (this line has to be there for it to affect the current document).
The error I get is CustomizationContext' is not a by reference property.
Does anyone know how to get just that ONE line equivalent for vb.net?
Thanks in advance.
EDIT: In case you need to see the vb.net sub:
Private Sub RemoveUnwantedContextMenuItems()
Dim oContextMenu As CommandBar
Dim oContextMenuItem As CommandBarControl
'Make changes to the ActiceDocument only (this is needed to make any changes to this document).
WordApplication.CustomizationContext = WordApplication.ActiveDocument 'This is the error.
For Each oContextMenu In WordApplication.CommandBars
If oContextMenu.Type = MsoBarType.msoBarTypePopup Then 'Loop through all the context menus of type (msoBarTypePopup)
For Each oContextMenuItem In oContextMenu.Controls
If (InStr(oContextMenuItem.Caption, "Smokeball")) Then
oContextMenuItem.Delete()
End If
Next
End If
Next
End Sub
PS - I have also already tried using the .AttachedTemplate as well as .Normal / .NormalTemplate
Jules pointed me in the right direction with his sample code.
After lots of playing around I noticed that somewhere in the solution, the [TYPE] of WordApplication was getting changed to a dynamic type of sorts, hence, it couldn't use CustomizationContext.
My solution was this:
I changed this line;
WordApplication.CustomizationContext = WordApplication.ActiveDocument
To this:
CType(WordApplication, Microsoft.Office.Interop.Word.Application).CustomizationContext = WordApplication.ActiveDocument
Forcing the types to be correct.
Simple solution but took some time.
Thanks to Jules for pointing me in the right direction.
(Points should go to you).

Insert Table from Builiding Block in specified location in the word document.

I want to insert a table which is defined as building block. I placed a content control in specified location in the document and refer to it by "selectcontetcontrolsbytag". Unfortunetly when table is inserted to the conentcontrol, it is convertered to RichText. Here is my code:
ThisDocument.SelectContentControlsByTag("TermsConditions").Item(1).Range = _
ActiveDocument.AttachedTemplate.BuildingBlockTypes.Item(wdTypeTables).Categories.Item("Terms and Conditions Translation").BuildingBlocks.Item("Terms and Conditions Eng")
Could you help me with proper code to insert building block in specified location. Also I would like this building block to be replaced by another, when user will select other item from userform, combobox etc.
Complete solution for my problem is:
Solution proposed by Cindy Meister Replacing content inside content
control:
To change content inside content control "TermsConditions" I added following code:
If doc.SelectContentControlsByTag("TermsConditions").Item(1).Range.Text <> doc.SelectContentControlsByTag("TermsConditions").Item(1).PlaceholderText Then
doc.SelectContentControlsByTag("TermsConditions").Item(1).Range.Cut
Else
End If
I'm not sure what you mean by "it is converted to Rich Text"...
The accepted way to insert a BuildingBlock is to use the BuildingBlock.Insert method to which you pass the target Range object. For example (based on your code sample):
Dim doc as Word.Document
Dim rngTarget as Word.Range
Set doc = ActiveDocument
Set rngTarget = doc.SelectContentControlsByTag("TermsConditions").Item(1).Range
doc.AttachedTemplate.BuildingBlockTypes.Item(wdTypeTables).Categories.Item("Terms and and Conditions Translation").BuildingBlocks.Item("Terms and Conditions Eng").Insert rngTarget, true
Take a look at the example in the VBA Language Reference...
Also, you should not use ThisDocument in your code, use ActiveDocument. (Even better, declare a Document object, assign ActiveDocument to it, then use that.)

Outlook Appointment - how to change the items in the Start Time dropdown list

I need to change the items in the start/end time dropdown list to be 5 minutes apart. Seems there's no simple way to set this up, so I'm trying VBA.
I can get an Inspector to look for when an appointment form is opened:
If Inspector.CurrentItem.Class = olAppointment Then
... but I don't know how to refer to or change the start/end time dropdown control to have the list of times I want it to.
If anyone out there knows of an alternative method of allowing the user to choose the times in intervals of 5 minutes, that would be great, too!
Check the Appointment.Start property. Use the following function to get current object
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
GetCurrentItem.UnRead = False
Set objApp = Nothing
End Function
This doesn't answer your question exactly - but I don't believe that it is possible to do what you need because there isn't a programmatic interface to those drop downs, nor is there a way to manually achieve what you want, so I don't know how having a macro recorder would help
This Website will tell you how to set a meeting up with a custom start time and duration, but you could build your own userform to popup whenever a meeting/appointment is created requesting a start and end time (or start time & duration if you prefer), which is probably the best workaround you'll get.
You could go crazy with this and make a non-modal userform that places itself over the existing controls and replicates their functionality - but that's a lot of coding, and it would be slow because it would be constantly updating its position or hiding itself when the appointment item loses focus, but its very achievable if you are determined.
Outside of VBA/Outlook, you could use Visual Studio to make a VSTO Add-In, creating a new ribbon in the appointment/meeting section, which has two custom controls which modify the start and end dates. You could also populate these with whatever code you want, but you'll probably need to learn C# to do this, although it may be possible with VB.net (which is much more similar to VBA). This Website is a reasonable starting point.
I'm sorry that this isn't really an answer, but I'm afraid that there isn't a satisfactory answer to this question.