Code:
Option Explicit'sendmessage variants
'Public Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, wParam As Long, lParam As Any) As Long
'Public Declare Function SendMessageAny Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, wParam As Any, lParam As Any) As Long
Public Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'Public Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
'window functions
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function EnumChildWindows Lib "user32" (ByVal hwndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Long
'button
Public Const BM_CLICK = &HF5& 'button message: schickt ein 'klick' an das ausgewählte fenster
Public Const BM_GETCHECK = &HF0 'get option button state
Public Const BM_SETCHECK = &HF1 'set
Public Const BST_CHECKED = &H1 'checked
Public Const BST_INDETERMINATE = &H2 'greyed
Public Const BST_UNCHECKED = &H0 'unchecked
'dialog handles
Public hWndApply As Long 'handle zu catia 'apply'-button des dialoges
Public hWndOK As Long 'handle zu catia 'ok'-button des dialoges
Public hWndClose As Long 'handle zu catia 'close'-button des dialoges
Public hWndDlg As Long 'handle zum dialog-fenster
Public hWndDisplay As Long 'handle zum 'Display-Button - Sheet Dialog
Public CATIA As Application 'CATIA-Object
Sub EditDisplayProps()
Dim lRet As Long
CATIA.StartCommand "Properties" 'open the dialog window
CATIA.RefreshDisplay = True 'give catia some time to react
DoEvents
hWndDlg = FindWindow(vbNullString, "Properties") 'get parent handle
If hWndDlg = 0 Then 'no such window
MsgBox "Dialog not found!", vbOKOnly Or vbCritical, "CatMain"
Exit Sub
End If
EnumChildWindows hWndDlg, AddressOf EnumChildProc, ByVal 0& 'get children handles
If hWndDisplay <> 0 Then
lRet = SendMessageByNum(hWndDisplay, BM_GETCHECK, 0&, 0&)
If lRet <> BST_CHECKED Then
PushButton hWndDisplay 'select display
CATIA.RefreshDisplay = True 'give catia some time to react
End If
End If
PushButton hWndOK 'send click message to OK dialog button
CATIA.RefreshDisplay = True
End Sub
Public Function EnumChildProc(ByVal hwndChild As Long, ByVal lParam As Long) As Long
Dim strBuff As String 'string buffer
On Error GoTo EnumChildProc_Error
strBuff = Space$(GetWindowTextLength(hwndChild) + 1) 'Get the windowtext length
GetWindowText hwndChild, strBuff, Len(strBuff) 'get the window text
strBuff = Left$(strBuff, Len(strBuff) - 1) 'remove the last Chr$(0)
Select Case strBuff
Case "OK" 'look for OK-Button
hWndOK = hwndChild 'assign to public handle
Case "Close" 'look for Close-Button
hWndClose = hwndChild 'assign to public handle
Case "Apply" 'look for Apply-Button
hWndApply = hwndChild 'assign to public handle
Case "Display" 'look for Apply-Button
hWndDisplay = hwndChild 'assign to public handle
Case Else
End Select
EnumChildProc = 1 'continue enumeration
Exit Function
'---------------------------------------------------------------------------------------
EnumChildProc_Error:
Dim errMsg As String
Dim errRet As VbMsgBoxResult
Select Case Err.Number
' Case 438
' Case -2147467259
Case Else
errMsg = Err.Number & ": " & Err.Description & " in procedure EnumChildProc"
errRet = MsgBox(errMsg, vbOKOnly, "EnumChildProc")
End Select
'Resume Next 'fall thru to quit sub
'---------------------------------------------------------------------------------------
End Function
Public Sub PushButton(hwnd As Long) 'push catia button
SetActiveWindow hWndDlg 'activate dialog
SendMessageByNum hwnd, BM_CLICK, 0&, 0& 'send click message to dialog button
End Sub
Sub GetCatiaObject()
On Error Resume Next 'Disable automatic error handling
Set CATIA = GetObject(, "CATIA.Application")
If (Err.Number <> 0) Then 'Manually handle all errors
MsgBox Err.Description & vbCrLf & "Is Catia running?" _
& vbCrLf & "Trying to start Catia!" _
, vbOKOnly Or vbInformation, "GetCatiaObject"
On Error GoTo 0 'Invalidates the Resume Next and clears the error
DoEvents
Set CATIA = CreateObject("CATIA.Application") 'try to start catia
Do 'wait for it
If Not CATIA Is Nothing Then 'then it's something
Exit Do
End If
DoEvents
Loop
CATIA.Visible = True 'show the main window
End If
On Error GoTo 0 'Invalidates the Resume Next and clears the error
End Sub