簡體   English   中英

在 PowerPoint 演示文稿中調整大小/位置形狀

[英]Resize/ position shapes in PowerPoint presentation

我有舊代碼,我將其重新用於更廣泛的用途。

我有一個 PowerPoint 演示文稿,我想將特定的圖像文件粘貼到其中,創建一個新幻燈片,然后重復直到 A 列中的所有變量名稱都完成。

它在特定文件位置查找圖像名稱,根據變量名稱值的左側、變量名稱值(A 列)和變量名稱值的右側構建名稱。 前任。 (“設備”“23”“通用產品線”)。

找到此圖像名稱后,它會將該圖像插入幻燈片,調整大小並將其定位到左側,然后找到另一張比較圖像,將其放置在同一張幻燈片上並將其調整大小並將其定位到右側。

調整大小和定位不再正常工作。 似乎圖像沒有被視為形狀。 我認為第一張圖像是之前實驗中的形狀(2),因為幻燈片上有一些剪貼畫可以算作形狀。 出於同樣的原因,我然后讓形狀(3)是圖像 2。

Sub Export_To_PowerPoint_JAH()
' Keyboard Shortcut: Ctrl+Shift+M

Dim Shape1 As PowerPoint.Shape
Dim Shape2 As PowerPoint.Shape
Dim objSlide As Slide
Dim New_Slide As Slide
Dim pptLayout As CustomLayout
Dim PP As PowerPoint.Application
Dim PPpres As PowerPoint.Presentation

'Create a PP application and make it visible
Set PP = New PowerPoint.Application
PP.Visible = msoCTrue

'Open the presentation you wish to copy to

'Opens the Template
Set PPpres = PP.Presentations.Open("A file path name to a template")

i = 7

Pre_Left = Range("H2")
Pre_Right = Range("H4")
Post_Left = Range("K2")
Post_Right2 = Range("K4")

Do

    Set objSlide = PPpres.Slides(i - 5)
    Set Title = PPpres.Slides(i - 5)


    If Cells(i, 1) = "" Then
        Exit Do
    Else: End If

    Variable_Name = Cells(i, 1)

    'Searches Image Bank Folder for pre and post file names
    If Not Range("H2") = "" Then
        Image_Name_Pre = Pre_Left & " " & Variable_Name & " " & Pre_Right
    Else
        Image_Name_Pre = Variable_Name & " " & Pre_Right
    End If

    If Not Range("K2") = "" Then
        Image_Name_Post = Post_Left & " " & Variable_Name & " " & Post_Right2
    Else
        Image_Name_Post = Variable_Name & " " & Post_Right2
    End If

    Set Shape1 = objSlide.Shapes.AddPicture(Range("B5") & Image_Name_Pre, msoCTrue, msoCTrue, 100, 100)

    objSlide.Shapes.Item(2).Width = 300
    objSlide.Shapes.Item(2).Height = 400
    objSlide.Shapes.Item(2).Top = 140
    objSlide.Shapes.Item(2).Left = 90

    Set Shape2 = objSlide.Shapes.AddPicture(Range("B5") & Image_Name_Post, msoCTrue, msoCTrue, 100, 100)

    objSlide.Shapes.Item(3).Width = 300
    objSlide.Shapes.Item(3).Height = 400
    objSlide.Shapes.Item(3).Top = 140
    objSlide.Shapes.Item(3).Left = 500

    Title.Shapes.Title.TextFrame.TextRange.Text = Cells(i, 3) & " Pre (Left) : " & Cells(i, 3) & " Post (Right) Offset=" & Cells(i, 4)

    'Create new slide
    Set New_Slide = PPpres.Slides.Add(PPpres.Slides.Count + 1, PpSlideLayout.ppLayoutObject)

    'ActivePresentation.Slides.Add Index:=ActivePresentation.Slides.Count + 1, Layout:=ppLayoutCustom

    i = i + 1

Loop

End Sub

假設您想要的形狀將是幻燈片上的第 n 個形狀並不是一個好主意,在您的情況下,沒有必要這樣做。 這個:

Set Shape1 = objSlide.Shapes.AddPicture(Range("B5") & Image_Name_Pre, msoCTrue, msoCTrue, 100, 100)

為您提供對變量 Shape1 中新插入圖像的引用,因此您可以執行以下操作:

With Shape1
  .Width = 300
  .Height = 400
  .Top = 140
  .Left = 90
End With

Shape2 也是如此。

另外,你這樣做:

Set Title = PPpres.Slides(i - 5)

這里有兩個問題:

1) 您尚未聲明變量 Title,並且

2) 使用對象/方法/屬性名稱作為變量名稱不是一個好習慣。

反而:

Dim oTitle as Slide
Set oTitle = PPpres.Slides(i - 5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM