[VBA] How to Position Excel Charts Pasted into PowerPoint at Arbitrary Locations

When pasting Excel charts into PowerPoint presentation materials, there are often scenarios where you want to freely control the placement of the graph using VBA.

This article introduces how to use VBA to place a graph pasted into PowerPoint at any specific coordinate on the slide.

目次

Object Positioning: Using .Left and .Top

Shapes pasted onto a PowerPoint slide (such as graphs and images) have their coordinates controlled by .Left (horizontal position) and .Top (vertical position).

The origin point (0, 0) is the top-left corner of the slide.

Sample Code for Positioning

' After pasting the graph (myShape), specify the position
myShape.Left = 100  ' 100 points from the left edge
myShape.Top = 50    ' 50 points from the top edge
  • .Left = 100: Sets the position 100 points to the right of the slide’s left edge.
  • .Top = 50: Sets the position 50 points down from the slide’s top edge.

Positioning at the Top-Left

If you want to paste the graph exactly at the top-left corner of the slide, specify 0 for both properties.

myShape.Left = 0
myShape.Top = 0

Slide Size and Points Unit

Coordinates on a slide are specified in “Points (pt)”.

Below are the reference coordinates for standard PowerPoint slide sizes:

Slide Aspect RatioWidth (.Width)Height (.Height)
4:3 (Standard)Approx. 960 ptApprox. 720 pt
16:9 (Widescreen)Approx. 1280 ptApprox. 720 pt

Using these values allows for calculations to target specific areas or center alignment.

Application: How to Center the Graph

To place a graph in the exact center of the slide, you can calculate the coordinates by subtracting half the graph’s size from half the slide’s size.

' Assuming 'pres' is the Presentation object and 'myShape' is the pasted shape
Dim slideW As Single
Dim slideH As Single

' Get slide dimensions
slideW = pres.PageSetup.SlideWidth
slideH = pres.PageSetup.SlideHeight

' Calculate center position
myShape.Left = (slideW - myShape.Width) / 2
myShape.Top = (slideH - myShape.Height) / 2

Summary

When pasting charts into PowerPoint using Excel VBA, you can place them freely using the .Left and .Top properties.

  • Explicitly specify coordinates using .Left and .Top.
  • Specifying 0, 0 aligns the object to the top-left.
  • It is important to specify appropriate coordinates based on the slide size.

This method is highly useful for improving work efficiency, such as automatically generating presentation materials or batch-placing objects on a large number of slides.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次