簡體   English   中英

VBA:將excel范圍粘貼到powerpoint占位符,而不使用.Select

[英]VBA: Paste excel range to powerpoint placeholder without using .Select

我想在自定義布局中將命名的excel范圍粘貼到powerpoint中的內容占位符。 我目前正在使用這樣的代碼

ranger.Copy
currentPPT.ActiveWindow.View.GotoSlide ppt.slides.Count
activeSlide.shapes("Picture").Select msoTrue
ppt.Windows(1).View.PasteSpecial (ppPasteEnhancedMetafile)

它通常有效,但有時會莫名其妙地失敗。 我在這個網站的其他地方看過, 例如這里說要避免使用.Select方法。 而是使用類似的東西

Dim oSh As Shape
Set oSh = ActivePresentation.Slides(9).Shapes.PasteSpecial(ppPasteEnhancedMetafile)(1)

但是,我無法弄清楚如何使用第二種方法直接復制到內容占位符。 那可能嗎?

編輯,關於Shai的建議。 目前的代碼是

For ii = activeSlide.shapes.Count To 1 Step -1
If activeSlide.shapes.Item(ii).Name = "Picture" Then
    shapeInd = ii
    Exit For
End If
Next ii

Set oSh = activeSlide.shapes.PasteSpecial(2, msoFalse)(shapeInd)

“圖片”形狀是“內容”占位符。 另外兩個形狀是文本框。

下面的代碼將按照您在帖子中提到的那樣執行。

首先,它創建所有必需的PowerPoint對象,包括設置Presentation和PPSlide

然后,它循環遍歷PPSlide所有Shapes ,當它找到帶有Name = "Picture"Shape ,它會檢索該工作表中形狀的索引,因此它可以將Range對象直接粘貼到此Shape(作為Placeholder)。

Option Explicit

Sub ExporttoPPT()

Dim ranger          As Range
Dim PPApp           As PowerPoint.Application
Dim PPPres          As Presentation
Dim PPSlide         As Slide
Dim oSh             As Object

Set PPApp = New PowerPoint.Application
Set PPPres = PPApp.Presentations("PPT_TEST") ' <-- change to your open Presentation
Set PPSlide = PPPres.Slides(9)

Set ranger = Worksheets("Sheet1").Range("A1:C5")    
ranger.Copy

Dim i As Long, ShapeInd As Long

' loop through all shapes in Slide, check for Shape Name = "Picture"
For i = PPSlide.Shapes.Count To 1 Step -1
    If PPSlide.Shapes.Item(i).Name = "Picture" Then
        ShapeInd = i '<-- retrieve the index of the searched shape
        Exit For
    End If
Next i

Set oSh = PPSlide.Shapes.PasteSpecial(2, msoFalse)(ShapeInd) ' ppPasteEnhancedMetafile = 2

End Sub

暫無
暫無

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

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