#If VBA7 Then
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDC As LongPtr, ByVal nIndex As Long) As Long
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hDC As LongPtr) As Long
#Else
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
#End If
Public PixelsPerX!, PixelsPerY!, PointsPerX!, PointsPerY!
Sub GetPerPointsAndPerPixels()
If PixelsPerX <> 0 Then Exit Sub
Const POINTSPERINCH = 72, LOGPIXELSX = 88, LOGPIXELSY = 90
#If VBA7 Then
Dim hDC^, SngX!, SngY!
#Else
Dim hDC&, SngX!, SngY!
#End If
hDC = GetDC(0)
SngX = GetDeviceCaps(hDC, LOGPIXELSX): SngY = GetDeviceCaps(hDC, LOGPIXELSY)
PixelsPerX = SngX / POINTSPERINCH: PixelsPerY = SngY / POINTSPERINCH 'Pixels = POINTS * PixelsPer
PointsPerX = POINTSPERINCH / SngX: PointsPerY = POINTSPERINCH / SngY 'Points = PIXELS * PointsPer
ReleaseDC 0, hDC
End Sub
'Sửa lại hàm sau:
'Với đối số IsPoints = True thì hàm trả về giá trị với đơn vị Points
'Với đối số IsPoints = False thì hàm trả về giá trị với đơn vị Pica
'Phương thức [A1].EntireColumn.ColumnWidth nhận giá trị phải là Pica
'1 Pica = 12 Point - Phụ thuộc thông số window mà giá trị này thay đổi
'Ví dụ:
'Trong giãn Cột: 1 Pica = 16 Pixel
'nhưng: 10 Pica = 97 Pixel, 100 Pica = 907 Pixel
Function RangeWidth!(ByVal Rng As Range, Optional ByVal IsPoints As Boolean = True)
If IsPoints Then
RangeWidth = Rng.EntireColumn.Width
Else
Dim I&
For I = 1 To Rng.Columns.Count
RangeWidth = RangeWidth + Rng(1, I).EntireColumn.ColumnWidth
Next I
End If
End Function