logo
search
VBA & Macro Problems

How to Dynamically Crop Charts from Screenshots Using VBA

WPS EditorWPS Editor Oct 9, 2026 869 views

Question details

The user wants to use VBA to dynamically crop charts from screenshots because fixed pixel coordinates fail when screen resolution or aspect ratio changes.

How to Automatically Crop Charts from Screenshots Using VBA
Product
Presentation
Device & OS
not provided
Scenario
Automating the extraction of specific charts from inserted screenshots without using hardcoded crop dimensions that break across different devices.
Observed behavior
Fixed crop coordinates in VBA work for a single screen size but crop the wrong areas when the display resolution or scaling settings change.
Before you start

Ensure the Developer tab is enabled in your presentation software to access the VBA editor, and verify that the screenshots are already inserted into your active slide.

Solution 1Recommended

Use Boundary Markers for Dynamic VBA Cropping

Add consistent visual markers or standardized capture area ratios to your charts before taking screenshots so VBA can calculate boundaries dynamically.

Relying on fixed coordinates often fails because display scaling and resolutions vary widely. By adding a recognizable marker (like a distinct colored border) or maintaining a strict capture ratio, you can write a VBA script that calculates crop boundaries based on the shape's relative dimensions rather than absolute points.

1
Standardize your screenshot capture

Before capturing the chart from the source application (such as Power BI or Excel), add a distinct border or ensure the screenshot tool captures an exact aspect ratio every time.

2
Insert the screenshot

Paste or insert the captured screenshot into your slide.

3
Open the VBA Editor

Press ALT + F11 to open the VBA Editor, then insert a new Module from the Insert menu.

4
Define dynamic crop calculations

Write your macro to select the image shape. Instead of fixed numbers, use formulas based on the image's total width and height (e.g., Shape.Width * 0.1) to define variables for the crop margins.

5
Apply crop properties

Apply the calculated variables to the PictureFormat properties: CropLeft, CropRight, CropTop, and CropBottom, then run the macro.

Use Boundary Markers for Dynamic VBA Cropping
Advanced Pixel Scanning: Standard VBA does not natively scan image pixels for colors. If you must detect specific colored markers automatically, you will need to incorporate advanced Windows API calls within your VBA script to check screen pixels.
Automate Presentations with WPS Office

Use WPS Presentation to Automate Image Formatting

WPS Office provides robust VBA support, allowing you to seamlessly run macros for cropping images, formatting slides, and automating repetitive presentation tasks.

  1. 1. Download WPS Office: Download and install WPS Office from the official website.
  2. 2. Open your presentation: Launch WPS Presentation and open your document containing the screenshots.
  3. 3. Access the VBA Editor: Navigate to the Tools tab and click on the Macro or VBA Editor button.
  4. 4. Run your macro: Paste your dynamic cropping VBA code into a new module and execute it to format your slide images.
Full support for standard VBA macros and automation scripts.High compatibility with Microsoft PowerPoint (.pptx, .ppt) formats.Built-in advanced image cropping and formatting tools.Lightweight interface for fast execution of automated tasks.
microsoft office alternative - wps office

Frequently Asked Questions

Why do my VBA crop coordinates change on different computers?

Fixed VBA crop coordinates depend on screen resolution, display scaling (DPI), and aspect ratios. If these settings vary between computers, the absolute pixel coordinates will not align with the target image area.

Can VBA natively scan an image to find a specific color marker?

No, native VBA does not have a built-in function to scan image files for specific pixel colors. You must use external libraries or Windows API calls (like GetPixel) to analyze colors on the screen.

Is there a way to crop an image by percentage rather than exact points in VBA?

Yes. You can calculate crop values dynamically by multiplying the Shape.Width and Shape.Height properties by a specific percentage decimal, then applying those calculated values to the PictureFormat.Crop properties.