SendKeys is the VBA command to literally send keystrokes to the interface, as if they were being typed on a keyboard. It’s a useful tool if you know how to use it. The CATIA VBA SendKeys syntax is simple: Access the command SendKeys and follow it with a string:
SendKeys “Cut”
You can find the syntax for the string arguments by using the object browser in the VBA editor (with VBA editor open hit F2 then search for “SendKeys” then right-click on Sendkeys and choose Help). For keystrokes that do not appear as characters (i.e. TAB, ENTER, etc), use the braces to encapsulate the string.
SendKeys “{Enter}”
It’s a good idea to code all SendKeys commands following a Display Refresh. See sample code below:
SubCATMain()
CATIA.StartCommand(“Disassemble”)
CATIA.RefreshDisplay = True
SendKeys “{ENTER}”
End Sub
Often, without the Display Refresh, the keystrokes will be sent so fast that the interface will still be processing the last line of code. Adding “CATIA.RefreshDisplay = True” will slow the process down and make SendKeys wait until it’s ready.
Other advice: You might want to double check for caps lock state. Other times you may need to create an object and use that with the SendKeys:
Dim WSShell
Set WSShell = CreateObject(“WScript.Shell”)
WSShell.SendKeys “c:FrmActivate”
WSShell.SendKeys “{ENTER}”
Below is a table of useful Sendkeys:
Key | Code |
Backspace | {BACKSPACE}, {BKSP} or {BS} |
Break | {BREAK} |
Caps Lock | {CAPSLOCK} |
Delete | {DELETE} or {DEL} |
Down Arrow | {DOWN} |
End | {END} |
Enter | {ENTER} or ~ |
Escape | {ESC} |
Help | {HELP} |
Home | {HOME} |
Insert | {INSERT} or {INS} |
Left Arrow | {LEFT} |
Num Lock | {NUMLOCK} |
Page Down | {PGDN} |
Page Up | {PGUP} |
Print Screen | {PRTSC} |
Right Arrow | {RIGHT} |
Scroll Lock | {SCROLLLOCK} |
Tab | {TAB} |
Up Arrow | {UP} |
F1 | {F1} |
F2 | {F2} |
F3 | {F3} |
F4 | {F4} |
F5 | {F5} |
F6 | {F6} |
F7 | {F7} |
F8 | {F8} |
F9 | {F9} |
F10 | {F10} |
F11 | {F11} |
F12 | {F12} |
F13 | {F13} |
F14 | {F14} |
F15 | {F15} |
F16 | {F16 |
To specify characters combinations use the following codes:
Key | Code |
Alt | % |
Ctrl | ^ |
Shift Lock | + |
For example to specify CTRL and C, the code would be object.SendKeys “^C” and for SHIFT F5 object.SendKeys “+{F5}”. To specify multiple combination sets such as ALT A Z, you use parentheses, for example, object.SendKeys “%(AZ)”.
Mr Emmett Ross hello,
I wanted to know if there is a book “VB Scripting for CATIA V5” version in Italian.
If so, could you indicate the link is to buy it ?.
Thank You
Vito
Hi Vito, I do have a translated version but it was done with software so I am guessing the translation is not very good.
Emmett
Hello Emmett,
I tried this the SendKeys command to create my “generate CATPart from CATProduct” macro but the command ENTER don’t work and the NUMLOCK activate/desactivate.
Config : Catia V5R21, hp laptop + monitor.
VBA script :
Sub CATMain()
‘selection (with item(1) like product)
CATIA.ActiveDocument.Selection.Clear
CATIA.ActiveDocument.Selection.Add CATIA.ActiveDocument.Product.Products.Item(1)
‘generate CATPArt
CATIA.StartCommand (“Generate CATPart from Product…”)
‘refresh window
CATIA.RefreshDisplay = True
‘enter
Dim WSShell
Set WSShell = CreateObject(“WScript.Shell”)
AppActivate (“Generate CATPart from Product”)
WSShell.SendKeys “{ENTER}”
End Sub
Can you help me for this macro please?
Thank you for your book, work, help, etc… 🙂
Glad you like the book and website! Try this:
Sub CATMain()
CATIA.DisplayFileAlerts = False
Dim Doc
Set Doc = CATIA.ActiveDocument
Set WindCol = CATIA.Windows
Dim DocNum
DocNum = WindCol.Count
Set WindCol2 = CATIA.Windows
Dim DocNum2
DocNum2 = WindCol2.Count
Dim ActWin
Dim StrProd
Set StrProd = Doc.Product
Dim Sel1
Set Sel1 = Doc.Selection
MsgBox “You will need to select the OK in the Generate CATPart from Product pop-up menu even though the hour glass is displayed.”
Sel1.Add StrProd
CATIA.StartCommand(“Generate CATpart from product…”)
While DocNum = DocNum2
Set WindCol2 = CATIA.Windows
DocNum2 = WindCol2.Count
Set ActWin = CATIA.Windows.Item(DocNum2)
ActWin.Activate
Wend
MsgBox “Finished”
End Sub
Hello ! Emmett, I am knew to CATIA macro programming
I needed a macro help.
I need to use print screen sendkey to capture screenshot in CATIA.
I want this macro in .CATScript format. It will be a big help if you could send it. Thanks in advance.
Hi. Why not use capture to take screen shots instead of print screen? Like this: https://www.scripting4v5.com/additional-articles/screen-shot-macro/
Screen shots macro of yours is absolutely great no doubt but captured screens is saved as a file in a folder. But I want the captured screen to go to clipboard so that I could open excel or PowerPoint and hit paste/ctrl+v. That way it’s easy.
But anyways after trying a bit hard I have done the macro which captures screen using send keys after which we should it paste that’s all.
Oh, OK, I see what you’re trying to do now. So you got it to work like you need it to?
yeah its working great. The code is like this
Sub CATMain()
Dim ObjViewer3D As Viewer3D
Set objViewer3D = CATIA.ActiveWindow.ActiveViewer
Dim objCamera3D As Camera3D
Set objCamera3D = CATIA.ActiveDocument.Cameras.Item(1)
Dim objSpecWindow As SpecsAndGeomWindow
Set objSpecWindow = CATIA.ActiveWindow
objSpecWindow.Layout = catWindowGeomOnly
CATIA.StartCommand(“Compass”)
objViewer3D.Viewpoint3D = objCamera3D.Viewpoint3D
objViewer3D.Reframe()
objViewer3D.ZoomIn()
objViewer3D.FullScreen = True
CATIA.RefreshDisplay = True
Set WshShell = CreateObject(“Word.Basic”)
WshShell.SendKeys “%{prtsc}”
CATIA.RefreshDisplay = True
CATIA.RefreshDisplay = True
CATIA.RefreshDisplay = True
CATIA.RefreshDisplay = True
objViewer3D.FullScreen = False
objSpecWindow.Layout = catWindowSpecsAndGeom
CATIA.StartCommand(“Compass”)
End Sub
Hi Emmett,
I am using both AutoCAD and CATIA. I am trying to work the same way in CATIA as AutoCAD by giving commands through key board instead of using mouse.
I have customized the commands by user alias. My problem is when I give command in AutoCAD, after enter the command, I pressed the space bar for enter.
So, in AutoCAD command line, if we type the space bar, it will be treated as enter. But, when we come out from the command line for typing text, it will consider space bar for providing space.
I am expecting this property in Catia also that when I enter the space bar in command line, it should be treated as enter.
Please suggest any idea to assign space bar as enter.
From the newsletter I received from you, I came to know that, it is possible by sendkeys option.
I tried the below script. But, it shows ‘VBScript compilation error’.
Kindly have a look and correct me the mistake.
SubCATMain()
CATIA.RefreshDisplay = True
SendKeys “{SPACE}”, True ‘ENTER
End Sub
Regards,
Suresh
Thanks a lot Guys for the wonderful information. It helped me a lot in my PhD work
Glad to help!
Hi Emmett,
Hope you are doing well.
I need a help from you. That I using VBA for creating macros. I used the send key for disassemble the curves.
First I selected the curve and use the above code but not working. It is showing the disassemble dialog box
Hi Emmett,
Need help on Catia V5 macro send keys and power commands.
I have referred the your code as below. It helped me to open VPM search box.
Basically I use CATIA with VPM.
My scrip should be like this :
1) I have list of Part /Product/Drawing numbers in excel
2) I keep open my Catia with VPM
3) If i run code. it should pick Excel content and search in VPM search under Document ID and enter command will list the part/ product/ drawing.
But to do this i have referred your send keys command as shown below But it did not gives a result.
But I am not able to write a code where i stuck in sending a value to Document ID attribute in VPM search.
Sub CATMain()
CATIA.StartCommand (“Enovia search”)
s = 1234 ‘Part number
s.SendKeys “^V”
CATIA.RefreshDisplay = True
CATIA.RefreshDisplay = True
SendKeys “{ENTER}”
End Sub
Please let me know on this.
DO you have to use SendKeys?
Hi
Sendkey for spacebar
…
Sub replace_spaceBySpaceSEndkey
For active autocorrects
Thanks
Hi Emmety,
How can we use the “Name” statement to change the name of an existing folder within a Catia macro?
No works Sendkey ENTER go to VBA windows not CATIA when I try use :
CATIA.StartCommand (“Generate CATpart from product…”)