Always position XL DataForm on top right corner !

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,615
Office Version
  1. 2016
Platform
  1. Windows
I am not sure if this has been addressed before but how can the screen position of a standard XL Dialogue box be controled via code?

One example is the XL Records entry form whih I would like to always have it displayed on the top right corner of the screen everytime the user evoques it through Data->Form.

Any ideas how to best achieve this ?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Ok, something interesting :) ...this seems to work without even having to use a CBT Hook ! Instead, I have adopted a simpler approach by using an API Timer and it works just as well !

In case anyone is interested , here is the code :

Place this in a Standard Module :

Code:
Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long

Declare Function SetTimer Lib "user32" _
(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long

Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, _
ByVal nIDEvent As Long) As Long

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Declare Function GetWindowRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long

Declare Function SetWindowPos Lib "user32" ( _
    ByVal hwnd As Long, _
    ByVal hWndInsertAfter As Long, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal cx As Long, _
    ByVal cy As Long, _
    ByVal wFlags As Long) As Long


Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Const SM_CXSCREEN = 0
Const SWP_NOSIZE = &H1
Const SWP_NOACTIVATE = &H10
Const SWP_NOZORDER = &H4

Dim lngTimerID As Long


Sub ShowDialogue()

    lngTimerID = SetTimer(0, 0, 1, AddressOf TimerProc)
    Application.DisplayAlerts = False
    ActiveSheet.ShowDataForm

End Sub

Sub TimerProc()

    Dim rectDlgPos As RECT
    Dim lngDialogWidth As Long
    Dim lngScreenWidth As Long
    
    lngStaticTextHwnd = FindWindow("bosa_sdm_XL9", vbNullString)
    GetWindowRect lngStaticTextHwnd, rectDlgPos
    lngScreenWidth = GetSystemMetrics(SM_CXSCREEN)
    lngDialogWidth = (rectDlgPos.Right - rectDlgPos.Left)
    SetWindowPos lngStaticTextHwnd, 0, lngScreenWidth - lngDialogWidth _
    , 0, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
    KillTimer 0, lngTimerID

End Sub

Sub Customise_DataForm()

    CommandBars.FindControl(ID:=860).OnAction = "CustomProc"
    
End Sub

Sub CustomProc()

    ShowDialogue
    
End Sub

Sub Reset_DataForm()

    CommandBars.FindControl(ID:=860).Reset
    
End Sub


Place this in ThisWorkBook Module :

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Reset_DataForm
End Sub

Private Sub Workbook_Open()
    Customise_DataForm
End Sub


Now, just save the Workbook, close it , reopen it and try displaying the DataForm via Data->Form... .You will find that the Dataform always appears on the Top Right corner of the screen.

In fact, the same could be achieved with any other XL Dialogue for that matter.

Regards.
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,183
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top