2017-04-20 81 views
0

我用自定義幻燈片佈局創建了一個PowerPoint。我希望能夠使用Excel VBA使用這些自定義佈局之一來創建新幻燈片,但我無法弄清楚正確的語法。使用Excel VBA在PowerPoint中添加自定義幻燈片佈局?

這是我目前擁有的代碼:

Sub runPPT() 

Application.ScreenUpdating = False 

Dim wb As Workbook 
Set wb = ThisWorkbook 

Dim ws As Worksheet 
Set ws = wb.Sheets("SG2") 

Dim pptName As String 
Dim ppt As Object 
Dim myPres As Object 
Dim slds As Object 
Dim sld As Object 

MsgBox ("Please choose PowerPoint to open.") 
pptName = openDialog() 
Set ppt = CreateObject("PowerPoint.Application") 
Set myPres = ppt.Presentations.Open(pptName) 

Set slds = myPres.Slides 
'This is where I want to add my custom layout 
'My layouts all have similar names like "Gate 2 Main" if that helps 
Set sld = slds.AddSlides(Slides.Count + 1, ActivePresentation.SlideMaster.CustomLayouts(1)) 

Application.ScreenUpdating = True 
End Sub 


Private Function openDialog() 
Dim fd As Office.FileDialog 
Dim txtFileName As String 
Set fd = Application.FileDialog(msoFileDialogFilePicker) 
With fd 
    .AllowMultiSelect = False 
    ' Set the title of the dialog box. 
    .Title = "Please select the file." 

    ' Clear out the current filters, and add our own. 
    .Filters.Clear 

    ' Show the dialog box. If the .Show method returns True, the 
    ' user picked at least one file. If the .Show method returns 
    ' False, the user clicked Cancel. 
    If .Show = True Then 

    txtFileName = .SelectedItems(1) 'replace txtFileName with your textbox 

    End If 

End With 

openDialog = txtFileName 

End Function 
+1

請在當前的代碼說明確切的問題。 –

回答

1

我能夠改變我的代碼下面來解決我的問題:

Sub runPPT() 

    Application.ScreenUpdating = False 

    Dim wb As Workbook 
    Set wb = ThisWorkbook 

    Dim ws As Worksheet 
    Set ws = wb.Sheets("SG2") 

    Dim pptName As String 
    Dim ppt As PowerPoint.Application 
    Dim myPres As PowerPoint.Presentation 
    Dim slds As PowerPoint.Slides 
    Dim sld As PowerPoint.slide 

    Dim oLayout As CustomLayout 

    MsgBox ("Please choose PowerPoint to open.") 
    pptName = openDialog() 
    Set ppt = CreateObject("PowerPoint.Application") 
    Set myPres = ppt.Presentations.Open(pptName) 

    Set slds = myPres.Slides 
    Set sld = slds.Add(myPres.Slides.Count + 1, ppLayoutBlank) 

    For Each oLayout In myPres.Designs("Gate Main").SlideMaster.CustomLayouts 
     If oLayout.Name = "Gate 2 Main" Then 
      sld.CustomLayout = oLayout 
      Exit For 
     End If 
    Next 

    Application.ScreenUpdating = True 

End Sub 


Private Function openDialog() 

    Dim fd As Office.FileDialog 

    Dim txtFileName As String 

    Set fd = Application.FileDialog(msoFileDialogFilePicker) 

    With fd 

    .AllowMultiSelect = False 

    ' Set the title of the dialog box. 
    .Title = "Please select the file." 

    ' Clear out the current filters, and add our own. 
    .Filters.Clear 

    ' Show the dialog box. If the .Show method returns True, the 
    ' user picked at least one file. If the .Show method returns 
    ' False, the user clicked Cancel. 
    If .Show = True Then 

     txtFileName = .SelectedItems(1) 'replace txtFileName with your textbox 

    End If 

    End With 

    openDialog = txtFileName 

End Function 
相關問題