Is it possible to use Interop to select text in a Powerpoint TextFrame by location (top and left offset)?

StackOverflow https://stackoverflow.com/questions/23398723

  •  13-07-2023
  •  | 
  •  

Question

I'm attempting to automate the basic process of editing text in a TextFrame in Powerpoint via Interop, and I've hit a snag. I need to be able to begin the text editing process at a particular location on screen, and after crawling MSDN I still don't know of a way to do it. The use case boils down to this:

  1. My service receives an X and Y coordinate
  2. I tell Powerpoint to select the shape at that location
  3. I tell Powerpoint to place the blinking cursor at the location it would be if the user had clicked there himself to start editing text.

It's the third step that's tripping me up. Word has RangeFromPoint, which returns a text range. In Powerpoint, though, that method returns a shape. I can use TextRange.Characters() to place the cursor manually within the shape's text range, but that accepts character index values rather than screen coordinates.

Anyone know how to go about this (other than brute forcing in mouse messages via Win32 calls)?

Was it helpful?

Solution

Every bit of text, down to the character level, can be treated as a Range; every text range has .BoundLeft, .BoundTop, .BoundHeight and .BoundWidth properties that return the coordinates of a rectangle that bounds the text range.

For example, this snippet will give you the left coordinate of the third text character in the currently selected shape:

With ActiveWindow.Selection.ShapeRange(1)
   Debug.Print .TextFrame.TextRange.Characters(3, 1).BoundLeft
End With

Coordinates are returned in Points. It sounds like you already have a handle on converting screen coordinates to PPT coordinates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top