Anyone know how I can find the screen height and width in VBA? I want to make a form fill the screen.
Printable View
Anyone know how I can find the screen height and width in VBA? I want to make a form fill the screen.
To find the screen's height in twips:
To find it in pixels:Code:Screen.Width
Screen.Height
Code:Screen.Width / Screen.TwipsPerPixelX
Screen.Height / Screen.TwipsPerPixelY
------------------
(¯`·.¸¸.·´¯`·->ShadowCrawler<-·´¯`·.¸¸.·´¯)
Teenage Programmer
Visual Basic, HTML, C++, JavaScript
http://welcome.to/X12Tech
Email: [email protected]
ICQ#: 9872708
Sorry ShadowCrawler, I'm talking VBA here, not the real thing. It doesn't recognise the Screen object. Any thoughts? (I'm sure you all have many thoughts, most of them unprintable, but hey...)
I'm sure there has to be a better way but this works for me (in word 2000).
Code:Option Explicit
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub ScreenSize()
Dim ScreenRect As RECT
Dim ret As Long
ret = GetClientRect(CLng(GetDesktopWindow()), ScreenRect)
MsgBox "Width: " & ScreenRect.Right - ScreenRect.Left
MsgBox "Height: " & ScreenRect.Bottom - ScreenRect.Top
End Sub
Private Sub Document_New()
ScreenSize
End Sub
------------------
Vincent van den Braken
EMail: [email protected]
ICQ: 15440110
Homepage: http://www.azzmodan.demon.nl
Thanks Azzmodan, that works great - you're a star
Sorry for the late post but I only had almost 1 year when you posted this :lol:Code:Private Declare Function GetSystemMetrics32 Lib "User32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
Private Sub UserForm_Activate()
Me.Height = GetSystemMetrics32(1)
Me.Width = GetSystemMetrics32(0)
End Sub
No need to alias GetSystemMetrics(), and magic numbers are an annoying newb hack. Define the appropriate constants (see GetSystemMetrics function).
A 17 year old question :eek:
"Magic Numbers" are hard to read and really should not be used but I do not know that they are a sign of a newb and definitely not a hack.
I remember when I first started many many years ago, you either used "magic numbers" or you created your own constants. Constants made the code easier to read at the expense of less available lines for processing.
Now of course we are well past the days where we needed to worry so much about how many bytes our code was using but for some programmers it is habit to use numbers directly in some cases, especially when using more than one language where the constant names are all different.