teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

不要変数の削除

2020/09/16 00:47

投稿

radames1000
radames1000

スコア1925

answer CHANGED
@@ -7,7 +7,7 @@
7
7
  Dim ppApp As Object 'PowerPointアプリ
8
8
  Dim ppPst As Object 'PowerPointプレゼン
9
9
  Dim ppSld As Object 'PowerPointスライド
10
- Dim ppW As Single, ppH As Single, i As Integer
10
+ Dim i As Integer
11
11
 
12
12
  'PowerPointを起動
13
13
  Set ppApp = CreateObject("PowerPoint.Application")

1

追記

2020/09/16 00:47

投稿

radames1000
radames1000

スコア1925

answer CHANGED
@@ -1,2 +1,49 @@
1
1
  コードを書く時間がないので参考URLだけ共有しますね。
2
- [https://teratail.com/questions/142599](https://teratail.com/questions/142599)
2
+ [https://teratail.com/questions/142599](https://teratail.com/questions/142599)
3
+
4
+ ---
5
+ ```VBA
6
+ Sub test()
7
+ Dim ppApp As Object 'PowerPointアプリ
8
+ Dim ppPst As Object 'PowerPointプレゼン
9
+ Dim ppSld As Object 'PowerPointスライド
10
+ Dim ppW As Single, ppH As Single, i As Integer
11
+
12
+ 'PowerPointを起動
13
+ Set ppApp = CreateObject("PowerPoint.Application")
14
+
15
+ 'PowerPointを表示
16
+ ppApp.Visible = msoTrue
17
+ 'PowerPoint新規プレゼンテーション作成
18
+ Set ppPst = ppApp.Presentations.Add(WithWindow:=True)
19
+
20
+ '最終行の取得
21
+ Dim myRow As Long
22
+ myRow = Cells(Rows.Count, 1).End(xlUp).Row
23
+
24
+ 'Excel各シートの貼り付け
25
+ For i = 2 To myRow
26
+ Range(Cells(i, 1), Cells(i, 4)).Copy
27
+ 'PowerPointスライド追加
28
+ Set ppSld = ppPst.Slides.Add(Index:=i - 1, Layout:=12)
29
+ ppPst.Slides(i - 1).Select
30
+
31
+ '貼り付け
32
+ ppApp.CommandBars.ExecuteMso "PasteAsEmbedded" '埋め込み
33
+ ' ppApp.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting" '元の書式
34
+
35
+ Next
36
+
37
+ End Sub
38
+ ```
39
+ ・埋め込みで貼り付ければ書式はそのままになります。ダブルクリックをすればExcelが開いて編集できます。
40
+ ・コメントアウトしていますが、元の書式で貼り付ければ書式や罫線がそのまま貼り付けられるはず、
41
+  なのですが、一行だけだと書式が反映されないようです。
42
+  二行以上だと書式も反映されるようですがこちらがお好みであれば色々修正してみてください。
43
+
44
+ こちらのコードはステップインで試せばうまくいくのですが、
45
+ そのまま実行してもうまくいかないと思います。
46
+ > ※ExecuteMsoは非同期実行なので、グラフ貼り付けが終わるまで待つ処理がポイントです。 
47
+
48
+ 待つ処理は参考URLやその先を見て、ご自身で調整してみてください。
49
+ 環境によっても異なると思いますので…