1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PowerPoint VBA 図形の名前の設定/取得、指定名の図形を狙って読み書き

Posted at

実行コード

Sub a()
  Call Set選択されている図形の名前("test")
End Sub
Sub b()
  Call 指定された名前の図形にテキストデータ書き込み("test", "内容")
End Sub
Sub c()
  Call 指定された名前の図形にテキストデータ読み込み("test")
End Sub

図形の名前の設定/取得

Sub Get選択されている図形の名前()
  Dim shp As Shape

  With ActiveWindow.Selection

    If .Type = ppSelectionNone Or _
      .Type = ppSelectionSlides Then Exit Sub

    For Each shp In .ShapeRange
      Debug.Print shp.Name
    Next shp

  End With

End Sub
Sub Set選択されている図形の名前(ShapeName)
  Dim shp As Shape

  With ActiveWindow.Selection

    If .Type = ppSelectionNone Or _
      .Type = ppSelectionSlides Then Exit Sub

    For Each shp In .ShapeRange
      shp.Name = ShapeName
    Next shp

  End With

End Sub

指定名の図形を狙って読み書き

Sub 指定された名前の図形にテキストデータ書き込み(ShapeName, 書き込む内容)
  Dim prs As Presentation
  Dim sld As Slide
  Dim shp As Shape
  
  Set prs = PowerPoint.ActivePresentation
  
  For Each sld In prs.Slides
    Debug.Print sld.Name
    For Each shp In sld.Shapes
      If shp.Name = ShapeName Then
        shp.TextFrame.TextRange.Text = 書き込む内容
      End If
    Next
  Next

End Sub
Sub 指定された名前の図形にテキストデータ読み込み(ShapeName)
  Dim prs As Presentation
  Dim sld As Slide
  Dim shp As Shape
  
  Set prs = PowerPoint.ActivePresentation
  
  For Each sld In prs.Slides
    Debug.Print sld.Name
    For Each shp In sld.Shapes
      If shp.Name = ShapeName Then
        Debug.Print shp.TextFrame.TextRange.Text
      End If
    Next
  Next

End Sub
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?