我找到了this post,其中包含下面使用的API調用。我還被提醒說,你可以通過Application.Commandbars(「Ribbon」)獲得Ribbon的高度。所以,在VBA你會這樣做:
編輯:在迴應公式欄和標題高度問題我添加了一個函數,隱藏它們,獲取ActiveWindow.Height,然後顯示它們並獲取新的ActiveWindow.Height和數字區別。該函數現在在下面的行中被調用,在轉換之前將高度加在一起。我認爲它可行,但我沒有做很多測試。
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
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Const LOGPIXELSX As Long = 88
Private Const LOGPIXELSY As Long = 90
Sub CellTopLeftPixels(rng As Excel.Range)
Dim RibbonHeight As Long
Dim TotalTop As Long
Dim TotalLeft As Long
RibbonHeight = Application.CommandBars("Ribbon").Height
TotalTop = (RibbonHeight + GetFormulaBarAndHeadingsHeight + rng.Top) * PixelsPerPointY
TotalLeft = rng.Left * PixelsPerPointX
Debug.Print "Top: "; TotalTop; " Left: "; TotalLeft
End Sub
Function GetFormulaBarAndHeadingsHeight()
Dim ActiveWindowHeightWhenHidden As Long
Dim ActiveWindowHeightWhenShown As Long
Application.DisplayFormulaBar = False
ActiveWindow.DisplayHeadings = False
ActiveWindowHeightWhenHidden = ActiveWindow.Height
Application.DisplayFormulaBar = True
ActiveWindow.DisplayHeadings = True
ActiveWindowHeightWhenShown = ActiveWindow.Height
GetFormulaBarAndHeadingsHeight = ActiveWindowHeightWhenHidden - ActiveWindowHeightWhenShown
End Function
Function PixelsPerPointX() As Double
Dim hdc As Long
Dim PixPerInchX As Long
hdc = GetDC(0)
PixPerInchX = GetDeviceCaps(hdc, LOGPIXELSX)
PixelsPerPointX = PixPerInchX/72
ReleaseDC 0, hdc
End Function
Function PixelsPerPointY() As Double
Dim hdc As Long
Dim PixPerInchY As Long
hdc = GetDC(0)
PixPerInchY = GetDeviceCaps(hdc, LOGPIXELSY)
PixelsPerPointY = PixPerInchY/72
ReleaseDC 0, hdc
End Function
的72的上方是每英寸的點。
這樣稱呼它:
Sub test()
CellTopLeftPixels ActiveCell
End Sub
僅供參考,您鏈接到不會幫助,因爲它是確定的高度的代碼XL 2003和更早的工具欄在屏幕的頂部。 – 2012-02-12 14:12:50
這是有點我的觀點,並沒有把我自己完全愚弄,我沒有到處看。 – cdavid 2012-02-13 00:02:08
@cdavid有點晚了,但看看這對你沒有問題:http://stackoverflow.com/a/29354020/904156 – Loathing 2015-03-30 23:26:12