Essaie ce code. Je n'ai pas pu les tester. Tiens-moi au courant.
#If VBA6 Then
Private Declare Function FindWindow& Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName$, ByVal lpWindowName$)
Private Declare Function ScreenToClient& Lib "user32" _
(ByVal hwnd&, ByRef lpPoint As POINTAPI)
Private Declare Function GetWindowRect& Lib "user32" _
(ByVal hwnd&, lpRect As RECT)
Private Declare Function CreateWindowEx& Lib "user32" _
Alias "CreateWindowExA" _
(ByVal dwExStyle&, ByRef lpParam As Any)
Private Declare Function InitCommonControlsEx& Lib "COMCTL32" _
(ByRef INITCOMMONCONTROLSEXData As InitCommonControlsExType)
Private Declare Function DestroyWindow& Lib "user32" _
(ByVal hwnd&)
Private Declare Function SendMessage& Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd&, ByVal wMsg&, ByVal wParam&, ByRef lParam As Any)
Private Declare Function SetWindowPos& Lib "user32" _
(ByVal hwnd&, ByVal hWndInsertAfter&, ByVal x&, _
ByVal y&, ByVal cx&, ByVal cy&, ByVal wFlags&)
#Else
Private Declare PtrSafe Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName$, ByVal lpWindowName$) As LongPtr
Declare PtrSafe Function ScreenToClient Lib "user32" _
(ByVal hwnd As LongPtr, lpPoint As POINTAPI) As Long
Declare PtrSafe Function GetWindowRect Lib "user32" _
(ByVal hwnd As LongPtr, lpRect As RECT) As Long
Declare PtrSafe Function CreateWindowEx Lib "user32" _
Alias "CreateWindowExA" _
(ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName)
Private Declare PtrSafe Function InitCommonControlsEx Lib "COMCTL32" _
(ByRef INITCOMMONCONTROLSEXData As InitCommonControlsExType) As Boolean
Declare PtrSafe Function DestroyWindow Lib "user32" _
(ByVal hwnd As LongPtr) As Long
Private Declare Function SendMessage& Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd&, ByVal wMsg&, ByVal wParam&, ByRef lParam As Any)
Declare PtrSafe Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
Declare PtrSafe Function SetWindowPos Lib "user32" _
_
(ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, _
ByVal wFlags As Long) As Long
#End If
Private Type InitCommonControlsExType
dwSize As Long
dwICC As Long
End Type
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private mWnd&
Public ObjetSource As Object, MaDate As Date
Private Sub UserForm_Initialize()
Const WS_CHILD& = &H40000000, MONTHCAL_CLASS$ = "SysMonthCal32", _
MCM_FIRST& = &H1000&, MCM_GETMINREQRECT& = (MCM_FIRST + 9&), _
SWP_SHOWWINDOW& = &H40&, MCS_NOTODAY& = &H10&, _
MCS_NOTODAYCIRCLE& = &H8&, ICC_DATE_CLASSES& = &H100&
Dim CalRect As RECT, LeTop&, LeLeft&, hwnd&, Marge&, CvtPtPixel!, _
IniCtrl As InitCommonControlsExType, PtCal As POINTAPI
LeTop = 10&
Marge = 10&
CvtPtPixel = 3 / 4
hwnd = FindWindow(vbNullString, Me.Caption)
'Création du controle calendrier
With IniCtrl
.dwSize = Len(IniCtrl)
.dwICC = ICC_DATE_CLASSES
End With
InitCommonControlsEx IniCtrl
mWnd = CreateWindowEx(0&, MONTHCAL_CLASS, vbNullString, _
WS_CHILD Or MCS_NOTODAY Or MCS_NOTODAYCIRCLE _
, 0&, 0&, 0&, 0&, hwnd, 0&, 0&, 0&)
'Ajustement de la position du control calendrier
SendMessage mWnd, MCM_GETMINREQRECT, 0&, CalRect
SetWindowPos mWnd, 0, LeTop, Marge, CalRect.Right + Marge, _
CalRect.Bottom + LeTop, SWP_SHOWWINDOW
'Ajustement de la position des boutons
GetWindowRect mWnd, CalRect
With CalRect
PtCal.x = .Right
PtCal.y = .Top
End With
ScreenToClient hwnd, PtCal
LeLeft = PtCal.x * CvtPtPixel + Marge
With CommandButton1
.Left = LeLeft
.Top = PtCal.y * CvtPtPixel + 75
LeTop = LeTop * 2 + .Height
End With
'Ajustement de la taille du UserForm
With CalRect
PtCal.x = .Top
PtCal.y = .Bottom
End With
ScreenToClient hwnd, PtCal
With Me
.Width = CommandButton1.Width + LeLeft + Marge + 10
.Height = (PtCal.x + PtCal.y) * CvtPtPixel
End With
Set ObjetSource = ActiveCell
End Sub
Private Sub CommandButton1_Click()
Const MCM_FIRST& = &H1000&, MCM_GETCURSEL& = (MCM_FIRST + 1&)
Dim LeTime As SYSTEMTIME
'Récuperer la date sélectionnée dans une cellule
SendMessage mWnd, MCM_GETCURSEL, 0&, LeTime
With LeTime
'MsgBox "Vous avez choisi le : " & vbNewLine & vbTab & _
Format(DateSerial(.wYear, .wMonth, .wDay), "dddd dd mmmm yyyy") & "."
MaDate = DateSerial(.wYear, .wMonth, .wDay)
'Récupérer la date sélectionnée dans une TextBox
If Not ObjetSource Is Nothing Then
If InStr(1, "Range,TextBox", TypeName(ObjetSource)) >= 1 Then ObjetSource.Value = MaDate
End If
End With
Unload Me
End Sub
Private Sub UserForm_Terminate()
'Détruire le control calendrier
DestroyWindow mWnd
End Sub