Question : How to get the Co ordinates of the Excel Cell

Hi I am able to get the co ordinates of teh Excel cell for Excel 2007 version but the same code is not working for Excel 2003.

Actually the "PointsToScreenPixelsX"method is not there for Active Pane in Excel 2003 which is i guess is giving a problem

The code for Excel 2007 is as below:-
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
Set ExcelApp = Sys.OleObject("Excel.Application", "")
  ExcelApp.Visible = true
  Set Workbooks = ExcelApp.Workbooks
  
  
  Set wnd = Sys.Process("EXCEL").Window("XLMAIN") 
           
  Set FirstBook = Workbooks.Item(1)
  Set ActiveSheet = FirstBook.ActiveSheet
  Set Cell = ActiveSheet.Cells.Item(40,4)
 
  ' Get the Top , Left , Width and Height of the Cell
  
  Tp= Cell.Top
  Log.Message("Top = " &  Tp   )
  
  wdt= Cell.Width
  Log.Message("Width = "&  wdt )    
  
  hgt= Cell.Height
  Log.Message("Height = "&  hgt)    
  
  lft= Cell.Left 
  Log.Message("Left = "&  lft )    
 
  ExcelApp.ActiveWindow.ActivePane.ScrollColumn = 1
  ExcelApp.ActiveWindow.ActivePane.ScrollRow = 1 
 
  ' Getting the Screen Co ordinates of the Cell
  x1 = excelapp.ActiveWindow.ActivePane.PointsToScreenPixelsX(lft + wdt/2)
  y1 = excelapp.ActiveWindow.ActivePane.PointsToScreenPixelsY(Tp+ hgt/ 2)

Answer : How to get the Co ordinates of the Excel Cell

Below is a revised version of what I posted above.

---

The two functions PointsToScreenPixelsX and PointsToScreenPixelsY are, in short, a joke. Not only are they incorrectly named, the help for these functions has been and continues to be incorrect in Excel 2007. The help states that these functions convert points in document coordinates to pixels in screen coordinates. What they really do is return the position in screen coordinates, in pixels, of the top left corner of cell A1. The help also states that any non-zero value passed to the routine is assumed to be points and is converted to pixels. In actuality they are assumed to be pixels and are only added to the result - there is no conversion done from points to pixels. They also are buggy and do not produce usable values when the document are is split.

Despite the confusion, what the functions actually provide is valuable and much better than if they did what their name and the help suggests. Converting from points to pixels is a relatively easy process as illustrated with the sample code below. What is not nearly as easy is getting the position, in screen coordinates, of the document area. Without these routines an extensive amount of code is required to calculate the distance from the left and top edge of the window to the left and top edge of the document area because there are no routines to provide the widths and heights of the command bars and worksheet headers.

The position, in screen coordinates, of the document area (the position of the top left corner of cell A1) is useful when positioning user forms relative to specific cells on a worksheet because the left and top user form positions are expressed in screen coordinates. The code below illustrates how to get the position in screen coordinate points of any cell on a worksheet.

   Left = ActiveWindow.PointsToScreenPixelsX(0) + GetPixelsFromPointsX(Range.Left) * ActiveWindow.Zoom / 100
   Top = ActiveWindow.PointsToScreenPixelsY(0) + GetPixelsFromPointsY(Range.Top) * ActiveWindow.Zoom / 100

Note that the top left position of the document area is not the top left corner of the first visible cell in the left-most, top-most corner of the document area, rather it is always the position of cell A1 even when A1 is scrolled to the left or scrolled up.

Setting a user form to these coordinates sets the top left corner of the user form exactly at the top left corner of the range. Another possible request is to get the position, in screen coordinates and in pixels, of the left and top edge of the document area (the functions ActiveWindow.PointsToScreenPixelsX and ActiveWindow.PointsToScreenPixelsY return the position of cell A1 which may be scrolled to the left or up).

   Left = ActiveWindow.PointsToScreenPixelsX(0) + GetPixelsFromPointsX(ActiveSheet.Columns(ActiveWindow.ScrollColumn).Left) * ActiveWindow.Zoom / 100
   Top = ActiveWindow.PointsToScreenPixelsY(0) + GetPixelsFromPointsY(ActiveSheet.Rows(ActiveWindow.ScrollRow).Top) * ActiveWindow.Zoom / 100

When the worksheet is split then getting the cell position in screen coordinate pixels is virtually impossible. This due to a couple of unworkable bugs. The first bug is that the PointsToScreenPixels functions give incorrect values for split windows. Not only is the value adjusted for columns/rows to the left/above the split, the adjustment also includes the window frame, toolbars, and headers. In other words, the value returned is the left or top of the Excel document window and not the document area. In order to find the left or top edge of the document area, the sizes of the window frame, any toolbars, and the header have to be determined and added. While the window frame and toolbars are not that difficult to determine, the header is difficult to determine because it varies based on the document font and font size, and the left header also varies depending on the bottom-most row displayed (the number of digits required to display the row numbers varies.) The second bug is that the PointsToScreenPixels functions return different values depending on which pane is active in a split window. However, the active pane is impossible to determine since is does not change when the selection is moved from the scrollable position to the non scrollable portion with the keyboard versus the mouse.

Note: These functions do not provide any meaningful results when the VBE is active because the ActiveWindow object requires a sheet to be active. These two functions return zero when stepping through them in the VBE debugger.

The code below provides point to pixel conversion routines.

[Begin Code Segment]

Private Const LOGPIXELSX As Long = 88 ' GetDeviceCaps request: logical pixels per inch horizontal
Private Const LOGPIXELSY As Long = 90 ' GetDeviceCaps request: logical pixels per inch vertical
Private Const PointsPerLogicalInch = 72& ' points per logical inch

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

Public Function GetPixelsFromPointsX( _
      ByVal Points As Double _
   ) As Double
   
' Return as pixels the provided horizontal distance in points.
   
   Dim DC As Long

   DC = GetDC(0)
   GetPixelsFromPointsX = Points * GetDeviceCaps(DC, LOGPIXELSX) / PointsPerLogicalInch
   ReleaseDC 0, DC
   
End Function

Public Function GetPixelsFromPointsY( _
      ByVal Points As Double _
   ) As Double
   
' Return as pixels the provided vertical distance in points.
   
   Dim DC As Long

   DC = GetDC(0)
   GetPixelsFromPointsY = Points * GetDeviceCaps(DC, LOGPIXELSY) / PointsPerLogicalInch
   ReleaseDC 0, DC
   
End Function

[End Code Segment]

Kevin
Random Solutions  
 
programming4us programming4us