Results 1 to 8 of 8

Thread: VBA, screen width/height?

  1. #1

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Post

    Anyone know how I can find the screen height and width in VBA? I want to make a form fill the screen.

  2. #2
    Member
    Join Date
    Jan 1999
    Location
    Gig Harbor, WA, USA
    Posts
    48

    Post

    To find the screen's height in twips:
    Code:
    Screen.Width
    Screen.Height
    To find it in pixels:
    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

  3. #3

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Post

    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...)

  4. #4
    Guest

    Post

    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




  5. #5

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Post

    Thanks Azzmodan, that works great - you're a star

  6. #6
    Member
    Join Date
    Jan 2016
    Posts
    51

    Re: VBA, screen width/height?

    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
    Sorry for the late post but I only had almost 1 year when you posted this
    When someone talks sh*t about me:


    Are you ta'king to me?..
    Are you ta'king to the special one?..
    Are you ta'king to the special one?..
    There's nobody else here,
    you must be talking to the special one!
    Are you ta'king to me?
    Are are, are you ta'king to me?


  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: VBA, screen width/height?

    No need to alias GetSystemMetrics(), and magic numbers are an annoying newb hack. Define the appropriate constants (see GetSystemMetrics function).

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: VBA, screen width/height?

    A 17 year old question

    "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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width