How to Position a Shape at Mouse Click Location using PowerPoint VBA
Question details
The user wants to modify a macro so that it inserts a shape or arrow exactly at the location of a user's mouse click, rather than at predefined coordinates.

- Product
- PowerPoint / WPS Presentation
- Device & OS
- not provided
- Scenario
- Automating the insertion of drawing objects dynamically on a presentation slide based on user interaction.
- Observed behavior
- The current VBA macro only inserts the shape or arrow at fixed, hardcoded coordinates because PowerPoint VBA does not natively expose mouse click events or screen coordinates.
Ensure your presentation is saved as a macro-enabled file (.pptm) and that you have enabled Developer tools and macros in your security settings before attempting to run Windows API calls.
Use Windows user32 API to Capture Mouse Coordinates
Since PowerPoint VBA does not natively support mouse click events, you must declare Windows API functions to capture the cursor's screen position and convert it to slide coordinates.
Mouse operations are handled by the Windows operating system rather than PowerPoint itself. To achieve dynamic shape placement, your macro must use Windows system calls (specifically the GetCursorPos function from user32.dll) to obtain the X and Y screen coordinates of the mouse click.
After obtaining the screen coordinates in pixels, the macro must mathematically convert these values into PowerPoint points (1/72 of an inch) while accounting for the current zoom level and window position.
Press ALT + F11 in your presentation software to open the Visual Basic for Applications (VBA) window.
In the Project Explorer panel on the left, right-click your presentation project, hover over 'Insert', and select 'Module'.
At the very top of the new module, declare the GetCursorPos function and the POINTAPI type structure to interact with the Windows user32 library.
Create a subroutine that calls GetCursorPos to get the mouse pixels, and apply a conversion formula (multiplying by 72 and dividing by the system DPI) to translate pixels into slide points.
Use the ActiveWindow.Selection.SlideRange.Shapes.AddLine method, passing your newly converted X and Y point coordinates as the positional arguments for the shape.

Automate Your Slides with WPS Presentation VBA
WPS Office provides robust VBA support, allowing you to run complex macros and Windows system API calls seamlessly. You can easily manage shapes and interactive scripts using the built-in VBA editor.
- 1. Download and Install WPS Office: Download WPS Office and ensure you select the version that includes VBA support during installation.
- 2. Open your Presentation: Launch WPS Presentation and open your macro-enabled presentation file.
- 3. Access the Developer Tools: Navigate to the 'Tools' tab on the ribbon and click on 'Developer' to access the Macro and VBA Editor features.
- 4. Run your Macro: Press ALT + F11 to open the editor, paste your API-based mouse tracking code, and run the macro directly to insert shapes dynamically.

Frequently Asked Questions
Why doesn't PowerPoint VBA have a built-in mouse click event?
PowerPoint VBA is designed primarily for automating document object manipulation rather than capturing global UI events. Interactive events like generic mouse clicks on a slide are handled by the operating system, requiring external API calls to intercept.
Can I capture mouse clicks during a running slideshow?
Yes, but it is much easier to achieve during a slideshow by using ActiveX controls. Inserting a transparent Image or Label ActiveX control over the slide allows you to natively use the control's _MouseDown event to capture X and Y coordinates without Windows API calls.
How do I convert screen pixels to slide points?
You must determine the screen DPI (usually 96 DPI on standard Windows settings) and the current zoom percentage of the application window. Because one VBA point is 1/72 of an inch, you generally multiply the pixel value by 72 and divide by your DPI.
Will Windows API calls like user32 work on Mac?
No, user32.dll and GetCursorPos are specific to the Windows operating system architecture. Mac users cannot use these Windows APIs and must rely on AppleScript or conditional VBA compilation methods to achieve OS-level interactions.




