PowerPoint VBA: Quelle commande (ou un ensemble de commandes) créerait des cadres PPT hors de mes images .jpg?

StackOverflow https://stackoverflow.com/questions/5037655

Question

J'ai quelques fichiers .jpg en c: my_folder

Voici leurs noms: pic_1.jpg, pic_2.jpg, pic_3.jpg, pic_4.jpg, pic_5.jpg.

Quelle commande ou un groupe de commandes dans Power Point VBA dois-je utiliser afin de pouvoir créer automatiquement plusieurs images dans PowerPoint afin que chaque trame contienne une image?

Était-ce utile?

La solution

Ce VBScript crée une nouvelle présentation PowerPoint et y ajoute deux diapositives, chacune avec une image. Vous devrez ajuster l'emplacement et la taille de l'image en fonction de votre goût. Vous devrez également utiliser le script.FilesScriptingObject pour énumérer vos images si vous souhaitez saisir automatiquement les images dans un répertoire pour intégrer la présentation. Si vous voulez que votre script puisse également enregistrer la présentation en appelant pptPresentation.SaveAs Une fois vos diapositives générées.

La documentation MSDN est située à http://msdn.microsoft.com/en-us/library/ff746873.aspx.

Dim pptDoc
Dim pptPresentation
Dim pptSlide

Set pptDoc = WScript.CreateObject( "PowerPoint.Application" )
pptDoc.Visible = True
Set pptPresentation = pptDoc.Presentations.Add( True )

' Add a new slide with a blank layout to the end of the Slides collection
' 12 = ppLayoutBlank
Set pptSlide = pptPresentation.Slides.Add( pptPresentation.Slides.Count + 1, 12 )

' Add a picture into the slide, saving the picture into the PowerPoint document
' 10, 10 are the Left and Top coordinates respectively
pptSlide.Shapes.AddPicture "c:\FullPath\1.JPG", False, True, 10, 10

' Add another slide with a picture
Set pptSlide = pptPresentation.Slides.Add( pptPresentation.Slides.Count + 1, 12 )
pptSlide.Shapes.AddPicture "c:\FullPath\2.jpg", False, True, 10, 10

Autres conseils

Comme la réponse précédente était spécifique aux scripts VBS, voici une version pour intégrer PowerPoint en tant que macro VBA. Cela a été créé avec PowerPoint 2010.

Cela a le répertoire codé en dur, c'est donc un exercice pour le lecteur d'inviter un nom de répertoire à numériser.

Sub CreatePictureSlideshow( )
  Dim presentation
  Dim layout
  Dim slide

  Dim FSO
  Dim folder
  Dim file
  Dim folderName

  ' Set this to point at the folder you wish to import JPGs from
  ' Note: make sure this ends with a backslash \
  folderName = "c:\somedirectory\"

  ' Delete all slides and setup variables
  Set presentation = Application.ActivePresentation
  If presentation.Slides.count > 0 Then
     presentation.Slides.Range.Delete
  End If
  Set layout = Application.ActivePresentation.SlideMaster.CustomLayouts(1)
  Set FSO = CreateObject("Scripting.FileSystemObject")

  ' Retrieve the folder's file listing and process each file
  Set folder = FSO.GetFolder(folderName)
  For Each file In folder.Files

     ' Filter to only process JPG images
     If LCase(Mid(file.Name, Len(file.Name) - 3, 4)) = ".jpg" Then

        ' Create the new slide and delete any pre-existing contents
        Set slide = presentation.Slides.AddSlide(presentation.Slides.count + 1, layout)
        While slide.Shapes.count > 0
          slide.Shapes(1).Delete
        Wend

        ' Add the picture
        slide.Shapes.AddPicture folderName + file.Name, False, True, 10, 10

        ' Optional: create a textbox with the filename on the slide for reference
        '   Dim textBox
        '   Set textBox = slide.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 10, 200, 200)
        '   textBox.TextFrame.TextRange.Text = file.Name
     End If
  Next

End Sub
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top