
VBAでパワーポイントにデータを転記したい。
そんな悩みにお答えして、今回は、
- 「表やテキスト」にデータをセットする方法
- 表の背景色を表示する方法
- フォント色を表示する方法
VBAでパワーポイントを操作する方法について上記3つについて解説していきましょう。
最初にパワーポイントの項目名称をセットしよう!
パワーポイントにデータをセットするときにまず最初に行うべきことがあります。それは。。。
パワーポイントにデータをセットする際に大切なこと
・項目名を付ける
家と同じで住所がないと郵便が届きませんよね。それと同じで、1つ1つのテキストや表に項目名を付けてあげましょう。
これで、データをセットすることができます。
項目名の付け方
項目名の付け方は下記を参考にしましょう。
上の図のように、
■「検索と選択Tab」
この項目から、テキストやグラフの名前をセットすることができます。
パワーポイントにデータをセットする方法
次に表内の各項目にデータをセットしていましょう。
テキストにデータをセットする方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | '|========================================================================================== '| 定義 : パワーポイントにテキストにセットする。 '|------------------------------------------------------------------------------------------ '| PG名 : Main() '|------------------------------------------------------------------------------------------ '| 説明 : ピボットテーブルのOPEN_itemの情報をPPTに移行する。 '|------------------------------------------------------------------------------------------ '|作成日 : 2014/11/16 '|作成者 : miyata '|========================================================================================== Sub Main() 'PPtアプリ起動用変数 Dim mySlide As Object 'PowerPointスライド Dim strName As String 'シェイプ名 Dim intSlideNo As Integer Dim strMoji As String Dim intMsg As Integer Dim buf As String Dim myShape As Shape Dim strM As String For Each mySlide In ActivePresentation.Slides 'Slide読み込み intSlideNo = mySlide.SlideIndex For Each myShape In mySlide.Shapes 'PPT Shape セット 'Debug.Print myShape.Name If intSlideNo > 21 Then strName = myShape.Name 'shape名 Select Case myShape.Name Case "text0" strMoji = "(As of " & Format(buf, "YYYY/M/D") & ")" 'intlen = Len(strMoji) - 7 '上付き文字位置(st) myShape.TextFrame.TextRange.Text = strMoji ’テキストにデータをセットする 'myShape.TextFrame.TextRange.Characters(intlen, 2).Font.Superscript = True '上付き文字設定(st) Exit For Case Else End Select Else Exit For End If Next Next myPres.SaveAs FileName:=ThisWorkbook.Path & "_" & Replace(StartDate, "/", "") & ".pptx" '保存 myPres.Close '閉じる myApp.Quit 'アプリ閉じる Set myPres = Nothing '変数クリア Set myApp = Nothing '変数クリア End Sub |
パワーポイントの表にデータをセットする方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | Dim mySlide As Object 'PowerPointスライド Dim myShape As Shape Dim strName As String 'シェイプ名 Dim intSlideNo As Integer Dim strMoji As String Dim intMsg As Integer Dim buf As String Dim strM As String ''''PPT読み込み For Each mySlide In myPres.Slides 'Slide読み込み intSlideNo = mySlide.SlideIndex For Each myShape In mySlide.Shapes 'PPT Shape セット 'Debug.Print myShape.Name strName = myShape.Name 'shape名 Select Case myShape.Name Case "Table 6" '表指定 With myShape.Table ' intMaxRow = .Rows.Count '表最大行数 intMaxCol = .Columns.Count '表最大列数 Select Case intSlideNo Case 1 'MFG For intNo = 0 To UBound(gstrMFG) .Cell(intRow, intCol - 1).Shape.TextFrame.TextRange.Text = "値取得" '値取得 .Cell(intRow, intCol - 1)TextFrame.TextRange.BackColor.RGB = RGB(0, 0, 0) '背景色取得 .Cell(intRow, intCol - 1)TextFrame.TextRange.Font.Color = RGB(0, 0, 0)'表内のフォント色をセットする Next End Select End With Case "Rectangle 3" 'テキスト指定 intlen = Len(strMoji) - 7 '上付き文字位置(st) myShape.TextFrame.TextRange.Text = strMoji myShape.TextFrame.TextRange.Characters(intlen, 2).Font.Superscript = True '上付き文字設定(st) End Select Next Next |
表内も番地があります。
.Cell(intRow, intCol - 1)
この記述のintRow 及びintColの中に縦横の番地を指定することができます。
表内の背景色を表示する方法
表内の背景色をセットするメソッドは、次のメソッドを使用します。
ポイント
myShape.Table.Cell(○○, ××)TextFrame.TextRange.BackColor.RGB = RGB(0, 0, 0) '表内の背景色をセットする
表内のフォントカラーを表示する方法
表内のフォントカラーを取得するメソッドは、次のメソッドを使用します。
ポイント
myShape.Table.Cell(○○, ××)TextFrame.TextRange.Font.Color = RGB(0, 0, 0) '表内のフォント色をセットする
まとめ
このように、パワーポイントの表やテキストを操作する上で大切なことが、必ず項目の名称を付けることが必要不可欠です。
デフォルトで、「テキスト1、テキスト2等」の名称がついていますが、まずは自分がわかりやすくコーディングできるように名称を付けることで非常に大切であることを覚えておきましょう。