I've got some macros for positioning and sizing the word window but currently
am using hard coded numbers for width, height, left and top parameters. These
numbers were obtained empirically for my desktop. When I take documents to my
laptop with different screen dimensions, these macros do not produce the
intended effect.

I am simply asking how I can determine the screen dimensions and how they
relate to the word window. It appears I may have to take title bars and word
task bar into account. I was wondering if anyone has either figured this all
out or better yet has code that does this.

Re: Determining screen size by Jonathan

Jonathan
Wed Jul 23 09:47:39 PDT 2008


"Peter" <Peter@discussions.microsoft.com> wrote in message
news:2545A221-CC1B-41B8-9006-74A1F7C03780@microsoft.com...
> I've got some macros for positioning and sizing the word window but
> currently
> am using hard coded numbers for width, height, left and top parameters.
> These
> numbers were obtained empirically for my desktop. When I take documents to
> my
> laptop with different screen dimensions, these macros do not produce the
> intended effect.
>
> I am simply asking how I can determine the screen dimensions and how they
> relate to the word window. It appears I may have to take title bars and
> word
> task bar into account. I was wondering if anyone has either figured this
> all
> out or better yet has code that does this.

System.HorizontalResolution and System.VerticalResolution will give you the
number of pixels on the screen in each direction. Unfortunately, this is not
quite enough, and you also need to know the points-per-pixel ratio in each
direction, as UserForm sizes and positions are determined in points, not
pixels. This ratio varies according to the screen resolution and the system
font size settings in Windows.

Place the following code in a separate module, and you will have available
functions PixelsPerInchX and PixelsPerInchY, which will allow you to make
conversions as needed. You may need to adjust the code to remove line
breaks.


' Device capabilities
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal
nIndex As Long) As Long
Private Const LOGPIXELSY = 90 ' Logical pixels/inch in Y
Private Const LOGPIXELSX = 88 ' Logical pixels/inch in X

' Used to obtain screen device context
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal
hDC As Long) As Long

Public Function PixelsPerInchX() As Long
Dim hWnd As Long
Dim hDC As Long
' Retrieves the number of pixels per logical
' inch in the X-direction on screen.
hWnd = GetDesktopWindow()
hDC = GetDC(hWnd)
PixelsPerInchX = GetDeviceCaps(hDC, LOGPIXELSX)
Call ReleaseDC(hWnd, hDC)
End Function

Public Function PixelsPerInchY() As Long
Dim hWnd As Long
Dim hDC As Long
' Retrieves the number of pixels per logical
' inch in the Y-direction on screen.
hWnd = GetDesktopWindow()
hDC = GetDC(hWnd)
PixelsPerInchY = GetDeviceCaps(hDC, LOGPIXELSY)
Call ReleaseDC(hWnd, hDC)
End Function

The code above is adapted from Karl Peterson's NCMetrics class module,
available on his site http://vb.mvps.org. That site is full of useful little
routines for this sort of thing. Originally written in and for VB6, large
parts of his code can be dropped into VBA projects unmodified, and the site
indicates which of his code samples have been checked for VBA compatibility.
A very useful resource, I use stuff from his site all the time.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup



Re: Determining screen size by Peter

Peter
Wed Jul 23 11:54:07 PDT 2008

Works great thank you! I have one more problem: The system.verticalResolution
function gives the entire screen's vertical dimensions. I now have to
subtract the space taken up by the Windows taskbar. This needs to be obtained
from the system as well (not hard-coded) because I use two rows for my
Windows Taskbar on my desktop but only one on my laptop. Do you have code to
determine the location and size of the Windows Taskbar?

Thanks,
Peter

"Jonathan West" wrote:

>
> "Peter" <Peter@discussions.microsoft.com> wrote in message
> news:2545A221-CC1B-41B8-9006-74A1F7C03780@microsoft.com...
> > I've got some macros for positioning and sizing the word window but
> > currently
> > am using hard coded numbers for width, height, left and top parameters.
> > These
> > numbers were obtained empirically for my desktop. When I take documents to
> > my
> > laptop with different screen dimensions, these macros do not produce the
> > intended effect.
> >
> > I am simply asking how I can determine the screen dimensions and how they
> > relate to the word window. It appears I may have to take title bars and
> > word
> > task bar into account. I was wondering if anyone has either figured this
> > all
> > out or better yet has code that does this.
>
> System.HorizontalResolution and System.VerticalResolution will give you the
> number of pixels on the screen in each direction. Unfortunately, this is not
> quite enough, and you also need to know the points-per-pixel ratio in each
> direction, as UserForm sizes and positions are determined in points, not
> pixels. This ratio varies according to the screen resolution and the system
> font size settings in Windows.
>
> Place the following code in a separate module, and you will have available
> functions PixelsPerInchX and PixelsPerInchY, which will allow you to make
> conversions as needed. You may need to adjust the code to remove line
> breaks.
>
>
> ' Device capabilities
> Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal
> nIndex As Long) As Long
> Private Const LOGPIXELSY = 90 ' Logical pixels/inch in Y
> Private Const LOGPIXELSX = 88 ' Logical pixels/inch in X
>
> ' Used to obtain screen device context
> Private Declare Function GetDesktopWindow Lib "user32" () As Long
> Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
> Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal
> hDC As Long) As Long
>
> Public Function PixelsPerInchX() As Long
> Dim hWnd As Long
> Dim hDC As Long
> ' Retrieves the number of pixels per logical
> ' inch in the X-direction on screen.
> hWnd = GetDesktopWindow()
> hDC = GetDC(hWnd)
> PixelsPerInchX = GetDeviceCaps(hDC, LOGPIXELSX)
> Call ReleaseDC(hWnd, hDC)
> End Function
>
> Public Function PixelsPerInchY() As Long
> Dim hWnd As Long
> Dim hDC As Long
> ' Retrieves the number of pixels per logical
> ' inch in the Y-direction on screen.
> hWnd = GetDesktopWindow()
> hDC = GetDC(hWnd)
> PixelsPerInchY = GetDeviceCaps(hDC, LOGPIXELSY)
> Call ReleaseDC(hWnd, hDC)
> End Function
>
> The code above is adapted from Karl Peterson's NCMetrics class module,
> available on his site http://vb.mvps.org. That site is full of useful little
> routines for this sort of thing. Originally written in and for VB6, large
> parts of his code can be dropped into VBA projects unmodified, and the site
> indicates which of his code samples have been checked for VBA compatibility.
> A very useful resource, I use stuff from his site all the time.
>
>
> --
> Regards
> Jonathan West - Word MVP
> www.intelligentdocuments.co.uk
> Please reply to the newsgroup
>
>
>

Re: Determining screen size by Jonathan

Jonathan
Wed Jul 23 12:19:37 PDT 2008


"Peter" <Peter@discussions.microsoft.com> wrote in message
news:418C1B59-DBB2-432C-8B85-0D2E85BF9DFB@microsoft.com...
> Works great thank you! I have one more problem: The
> system.verticalResolution
> function gives the entire screen's vertical dimensions. I now have to
> subtract the space taken up by the Windows taskbar. This needs to be
> obtained
> from the system as well (not hard-coded) because I use two rows for my
> Windows Taskbar on my desktop but only one on my laptop. Do you have code
> to
> determine the location and size of the Windows Taskbar?

What I can give you is the coordinates (in pixels) of the available screen
real-estate excluding the taskbar. (The following code is adapted from a
sample on Randy Birch's website http://vbnet.mvps.org - another very useful
resource for oddments of this kind). Put the following into a separate
module.

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

Private Const SPI_GETWORKAREA& = 48

Private Declare Function SystemParametersInfo Lib "user32" _
Alias "SystemParametersInfoA" _
(ByVal uAction As Long, _
ByVal uParam As Long, _
lpvParam As Any, _
ByVal fuWinIni As Long) As Long

Public Function ScreenArea() As RECT
Dim rc As RECT
Call SystemParametersInfo(SPI_GETWORKAREA, 0&, rc, 0&)
ScreenArea = rc
End Function

You can't assume the taskbar is at the bottom, it could be at the top or the
side. Hence, this function gives you a user-defined type with 4 parameters,
Bottom, Top, Left & Right, giving the position in pixels of the desktop
area.



--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup



Re: Determining screen size by JeanGuyMarcil

JeanGuyMarcil
Wed Jul 23 12:36:06 PDT 2008

"Peter" wrote:

> Works great thank you! I have one more problem: The system.verticalResolution
> function gives the entire screen's vertical dimensions. I now have to
> subtract the space taken up by the Windows taskbar. This needs to be obtained
> from the system as well (not hard-coded) because I use two rows for my
> Windows Taskbar on my desktop but only one on my laptop. Do you have code to
> determine the location and size of the Windows Taskbar?

I have never used it, but maybe that working with
Application.ActiveWindow.UsableHeight
will help.

This return a Point value and the active window has to be visible.
See the VBA help for more details (It states that you have to substract 1
from the returned value to get the actual value...).

Re: Determining screen size by Peter

Peter
Wed Jul 23 12:37:02 PDT 2008

Thanks again for the excellent and speedy responses!
In my code I use:
Dim l As Long, t As Long, b As Long, r As Long, ScrDim As RECT

Set ScrDim = ScreenArea
l = ScrDim.Left
t = ScrDim.Top
r = ScrDim.Right
b = ScrDim.Bottom

I get an error "Object Required" at the Set call.

What am I doing wrong?

Thanks,
Peter
"Jonathan West" wrote:

>
> "Peter" <Peter@discussions.microsoft.com> wrote in message
> news:418C1B59-DBB2-432C-8B85-0D2E85BF9DFB@microsoft.com...
> > Works great thank you! I have one more problem: The
> > system.verticalResolution
> > function gives the entire screen's vertical dimensions. I now have to
> > subtract the space taken up by the Windows taskbar. This needs to be
> > obtained
> > from the system as well (not hard-coded) because I use two rows for my
> > Windows Taskbar on my desktop but only one on my laptop. Do you have code
> > to
> > determine the location and size of the Windows Taskbar?
>
> What I can give you is the coordinates (in pixels) of the available screen
> real-estate excluding the taskbar. (The following code is adapted from a
> sample on Randy Birch's website http://vbnet.mvps.org - another very useful
> resource for oddments of this kind). Put the following into a separate
> module.
>
> Public Type RECT
> Left As Long
> Top As Long
> Right As Long
> Bottom As Long
> End Type
>
> Private Const SPI_GETWORKAREA& = 48
>
> Private Declare Function SystemParametersInfo Lib "user32" _
> Alias "SystemParametersInfoA" _
> (ByVal uAction As Long, _
> ByVal uParam As Long, _
> lpvParam As Any, _
> ByVal fuWinIni As Long) As Long
>
> Public Function ScreenArea() As RECT
> Dim rc As RECT
> Call SystemParametersInfo(SPI_GETWORKAREA, 0&, rc, 0&)
> ScreenArea = rc
> End Function
>
> You can't assume the taskbar is at the bottom, it could be at the top or the
> side. Hence, this function gives you a user-defined type with 4 parameters,
> Bottom, Top, Left & Right, giving the position in pixels of the desktop
> area.
>
>
>
> --
> Regards
> Jonathan West - Word MVP
> www.intelligentdocuments.co.uk
> Please reply to the newsgroup
>
>
>

Re: Determining screen size by Tony

Tony
Wed Jul 23 14:00:57 PDT 2008

ScrDim is not an object, so you don't want to *set* it. Just use

ScrDim = ScreenArea

--
Enjoy,
Tony

"Peter" <Peter@discussions.microsoft.com> wrote in message
news:E1CD81A1-575A-4E89-A8EB-8DA087A7E6F0@microsoft.com...
> Thanks again for the excellent and speedy responses!
> In my code I use:
> Dim l As Long, t As Long, b As Long, r As Long, ScrDim As RECT
>
> Set ScrDim = ScreenArea
> l = ScrDim.Left
> t = ScrDim.Top
> r = ScrDim.Right
> b = ScrDim.Bottom
>
> I get an error "Object Required" at the Set call.
>
> What am I doing wrong?
>
> Thanks,
> Peter
> "Jonathan West" wrote:
>
>>
>> "Peter" <Peter@discussions.microsoft.com> wrote in message
>> news:418C1B59-DBB2-432C-8B85-0D2E85BF9DFB@microsoft.com...
>> > Works great thank you! I have one more problem: The
>> > system.verticalResolution
>> > function gives the entire screen's vertical dimensions. I now have to
>> > subtract the space taken up by the Windows taskbar. This needs to be
>> > obtained
>> > from the system as well (not hard-coded) because I use two rows for my
>> > Windows Taskbar on my desktop but only one on my laptop. Do you have
>> > code
>> > to
>> > determine the location and size of the Windows Taskbar?
>>
>> What I can give you is the coordinates (in pixels) of the available
>> screen
>> real-estate excluding the taskbar. (The following code is adapted from a
>> sample on Randy Birch's website http://vbnet.mvps.org - another very
>> useful
>> resource for oddments of this kind). Put the following into a separate
>> module.
>>
>> Public Type RECT
>> Left As Long
>> Top As Long
>> Right As Long
>> Bottom As Long
>> End Type
>>
>> Private Const SPI_GETWORKAREA& = 48
>>
>> Private Declare Function SystemParametersInfo Lib "user32" _
>> Alias "SystemParametersInfoA" _
>> (ByVal uAction As Long, _
>> ByVal uParam As Long, _
>> lpvParam As Any, _
>> ByVal fuWinIni As Long) As Long
>>
>> Public Function ScreenArea() As RECT
>> Dim rc As RECT
>> Call SystemParametersInfo(SPI_GETWORKAREA, 0&, rc, 0&)
>> ScreenArea = rc
>> End Function
>>
>> You can't assume the taskbar is at the bottom, it could be at the top or
>> the
>> side. Hence, this function gives you a user-defined type with 4
>> parameters,
>> Bottom, Top, Left & Right, giving the position in pixels of the desktop
>> area.
>>
>>
>>
>> --
>> Regards
>> Jonathan West - Word MVP
>> www.intelligentdocuments.co.uk
>> Please reply to the newsgroup
>>
>>
>>


Re: Determining screen size by Jonathan

Jonathan
Wed Jul 23 12:46:19 PDT 2008


"Peter" <Peter@discussions.microsoft.com> wrote in message
news:E1CD81A1-575A-4E89-A8EB-8DA087A7E6F0@microsoft.com...
> Thanks again for the excellent and speedy responses!
> In my code I use:
> Dim l As Long, t As Long, b As Long, r As Long, ScrDim As RECT
>
> Set ScrDim = ScreenArea
> l = ScrDim.Left
> t = ScrDim.Top
> r = ScrDim.Right
> b = ScrDim.Bottom
>
> I get an error "Object Required" at the Set call.
>
> What am I doing wrong?
>


You don't need Set. RECT is a type, not an object


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup