Consulting

Results 1 to 19 of 19

Thread: Simple Macro to import slides from a file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Question Simple Macro to import slides from a file

    I am trying to import slides from multiple files into a single presentation.

    Here is what I have in VB so far - what else do I need - I have minimal skills in this area - but I have to import 28 slides into a single presentation on a weekly basis. It would be easier to rename the files to be consistent and run a macro. Can anyone please help me?

    Sub ImportAgencySlides()
    Dim FileName As String
    Dim Index, SlideStart, SlideEnd As Long
    ActivePresentation.Slides.InsertFromFile("c:\filename1.ppt", 1, 1, 1)
    End Sub
    When I debug, I get Compile Error: Syntax Error

    If I change it (based on MS help sites), to this format:

    ActivePresentation.Slides.InsertFromFile_ "c:\filename1.ppt", 1, 1, 1
    I get the following error: Method or Data Member not found
    And, the file is in the path and file name described.

    I also want to have it import multiple files, what would that sytax be?

    Thanks for any and all help!

    Kelly
    Last edited by Aussiebear; 04-08-2023 at 03:51 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Firstly are you sure that's the path to the file? ie its a file called filename1.ppt directly on the C drive.

    secondly the code you quote will only insert one slide (assuming it's the right path!)

    syntax is insertfrom file > the (correct) path&filename > The slide number you want to insert after (so 1 is probably good)>The slide number to start inserting from (1 is still good) > THE NUMBER OF SIDES TO INSERT 1 is probably bad!. If you simply omit this last value all slides will be inserted.

    So (assuming the path is correct :
    ActivePresentation.Slides.InsertFromFile_ "c:\filename1.ppt", 1, 1

    For the next file count the slides and start inserting after that number:

    ActivePresentation.Slides.InsertFromFile "the proper path", ActivePresentation.Slides.Count, 1
    Last edited by Aussiebear; 04-08-2023 at 03:52 PM. Reason: Adjusted the code tags
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3

    Follow-up question

    Thanks for your quick response. I truly do only want to include the first slide from any presentation I import into this presentation. Is the rest of the syntax right, or am I missing big chunks of what is required to make this thing run because I keep getting the syntax errors listed in my original post.

    Thanks,

    Kelly

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    No it's correct but is that filepath really correct?

    Also you might lose the design template.

    Move the source file to your desktop and try this code see if it works.
    Sub testing()
    ActivePresentation.Slides.InsertFromFile Environ("USERPROFILE") & "\desktop\" & "put just the file name here include .ppt", 1, 1, 1
    End Sub
    Last edited by Aussiebear; 04-08-2023 at 03:52 PM. Reason: Adjusted the code tags
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    John,

    After looking back at my code...I had mis-named my folder in the code (because - no it isn't in the root, but I didn't want to bore you with my lengthy file structure. It is working, thanks so much for your assist.

    Regards,

    Kelly

  6. #6
    Hi,
    Just stumbled across this thread and think this is what i need, does anyone know how to keep the design to the imported slide?!
    /Enfant terrible

  7. #7
    VBAX Regular
    Joined
    Dec 2008
    Posts
    86
    Location
    i am too facing the same problem..

  8. #8
    VBAX Newbie
    Joined
    Nov 2011
    Posts
    3
    Location

    Help needed to import different pp files and store them in a set

    Hey,

    I really need some support to accomplish what I am trying to do.

    This is what I want:
    In a macro, I want to specify a folder where all my powerpoint files (hence slides) are located and then import them into the macro script in a set.

    So I was thinking a bit in terms of the below, but change the ActivePres... to a set where I can store all the powerpoint files I am importing.

    ActivePresentation.Slides.InsertFromFile "C:\*.pptx", Index:=CurrentSlideIndex

    Is this possible? And if, how do I do it?

    Thanks

  9. #9
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    I would suggest you start a new thread rather that bring an old one back to life.

    It's not clear what you need.

    Do you mean combine all of the ppt files in a given folder into one presentation or something else?
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  10. #10
    VBAX Newbie
    Joined
    Nov 2011
    Posts
    3
    Location
    Ok, sorry about that, this is my first thread here.

    Ok so this is my complete plan:
    I want to build a power point file (master powerpoint file) with a macro by defining the folder (where I have stored X number of power point files, i.e. which includes slides) and then import all these files from the folder (with some *.pptx) format and store it in a set.
    I dont know if it is possible to store a complete set of power point files (where every object in the set contains an array of slides from the specific place in the set) or if I have to import all the powerpoint files and store it in a set where every object in the set contains a slide.

    I would prefer the former, because my final plan is to generate this master power point file by randomly inserting the power point files (i.e. the slides should stick together in every powerpoint file) from the imported set.

    An example:
    Lets say I have 3 pp files containing 3 slides each at the location c:\test. Now I want to create a power point file with a macro which imports these 3 pp files from the folder and store them in a set. After this point, I want to create a loop which runs through the set and randomly adding the lump of slides in each pp file in the master power point file. So the result will be this master pp file with 9 slides and where the 3 lump slide groups are arranged in a random order.

    Does it make sense?

    Thanks

  11. #11
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    This should get you going:
    Modify the path to point to your folder (don't forget the final \)

    Sub Combine_fromFolder()
        Dim strFPath As String
        Dim strSpec As String
        Dim strFileName As String
        Dim oTarget As Presentation
        Set oTarget = Application.Presentations.Add(WithWindow:=True)
        strFPath = "C:\Users\John\Desktop\Test\"        ' Edit this
        strSpec = "*.PPTX" 'to include PPT etc use "*.PP*"
        strFileName = Dir$(strFPath & strSpec)
        While strFileName <> ""
       oTarget.Slides.InsertFromFile strFileName, oTarget.Slides.Count, 1, 1
       strFileName = Dir()
        Wend
    End Sub
    Last edited by Aussiebear; 04-08-2023 at 03:54 PM. Reason: Adjusted the code tags
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  12. #12
    VBAX Newbie
    Joined
    Mar 2016
    Posts
    1
    Location
    Hello,

    I tried this solution and it is not working for me could you please walk me through your comments, may be I am not replacing what needs to be replaced to make it work

  13. #13
    VBAX Newbie
    Joined
    Aug 2016
    Posts
    3
    Location
    Quote Originally Posted by anton86 View Post
    Hello,

    I tried this solution and it is not working for me could you please walk me through your comments, may be I am not replacing what needs to be replaced to make it work

    I know, this topic is quite old, still I was using the code today. That is why I am trying to help with errors that may occur.
    For me it worked in the first place but did not anymore at further tries. I tried to monitor the mistake, could not really figure the mistake and nevertheless tried to build a workaround just by guessing. Here's what worked fine for me in the end:

    Sub Combine_fromFolder() 
        Dim strFPath As String 
        Dim strSpec As String 
        Dim strFileName As String 
        Dim oTarget As Presentation 
        Set oTarget = Application.Presentations.Add(WithWindow:=True) 
        strFPath = "C:\Users\John\Desktop\Test\" ' Edit this
        strSpec = "*.PPTX" 'to include PPT etc use "*.PP*"
        strFileName = Dir$(strFPath & strSpec) 
        While strFileName <> "" 
            strFileName = strFPath & strSpec
            oTarget.Slides.InsertFromFile strFileName, oTarget.Slides.Count, 1, 1 
            strFileName = Dir() 
        Wend 
    End Sub

  14. #14
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Isn't that exactly the code I posted back in 2007?? Obviously it won't work if you look for a folder on my (John) Desktop so you need to edit the marked line to show your desktop

    OR Use :
    strFPath =Environ("USERPROFILE") &  "\Desktop\Test\"
    Last edited by Aussiebear; 04-08-2023 at 03:56 PM. Reason: Adjusted the code tags
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  15. #15
    VBAX Newbie
    Joined
    Aug 2016
    Posts
    3
    Location
    Quote Originally Posted by John Wilson View Post
    Isn't that exactly the code I posted back in 2007??
    No it's not. I added the first line within the loop since I had the feeling the string did not always come out completely as it should but only as a file name instead of the complete path.

  16. #16
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    I see the difference now but it should NOT be within the loop.

    strFileName = Dir$(strFPath & strSpec) Finds the first file in the Folder that has a pptx ending

    In the loop strFileName = Dir() finds the NEXT file that matches

    Your line

    strFileName = strFPath & strSpec

    Would look for a file with path:

    C:\Users\JohnDesktop\data\*.PPTX

    Which cannot exist because * is not an acceptable name
    Last edited by John Wilson; 08-10-2016 at 09:07 AM.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  17. #17
    VBAX Newbie
    Joined
    Aug 2016
    Posts
    3
    Location
    Yes I see your point but exactly that is what's (in some cases) not working in the original.

    strFileName = Dir$(strFPath & strSpec)

    only had the name of the file as a result:

    strFileName = example.pptx
    StrFPath = C:\examplefolder\

    This is why I combined those variables in the second step, which somehow only worked within the loop.

    strFileName = strFPath & strSpec

    It was just trial an error for me, since I do not really have programming knowledge and just common sense within my set of skills...

  18. #18
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    It might seem wrong that strFileName is only the name NOT the path but that is how Dir works and it should insert the slide OK.

    When you said it only worked the first time what did you mean. It should create a new file every time it runs as it is written and add only the first slide from each presentation in the folder. Maybe you wanted something different?
    Last edited by John Wilson; 08-10-2016 at 01:24 PM.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  19. #19
    Quote Originally Posted by John Wilson View Post
    It might seem wrong that strFileName is only the name NOT the path but that is how Dir works and it should insert the slide OK.

    When you said it only worked the first time what did you mean. It should create a new file every time it runs as it is written and add only the first slide from each presentation in the folder. Maybe you wanted something different?
    Hi, Your code does not work for me.

    It shows error in the line oTarget.Slides.InsertFromFile strFileName, oTarget.Slides.Count, 1, 1
    Any help would be appreciated.

    Just a background on what i am trying to do.

    I get alot of PPT's from various heads of projects and i am supposed to compile them all into one Master PPT and send it forward.

    The order in which i compile them doesn't matter however, say if a PPTX file has 4 Slides, i would want those 4 slides to be together; and not one in the start and one in the end.

    For clarity's sake, in a nutshell, Can you help me with a code where

    1. I put all the files i get in a folder
    2. I run the Macro in a blank .PPTX File saved in the same directory, maybe i'll name it Main PPT
    3. The Macro takes slides from all the other .PPTX files in the Directory and Consolidates it into the PPT.
    4. Ta da! Half an hour of Copy pasting done in minutes!

    PS: If there would be a way of Checking if all the files have been pasted from the directory i would greatly appreciate it? Like a message-box saying slides X and Y are missing from X.pptx and Y.pptx file in case of an error in copy pasting?

    I am using PowerPoint 2013 if that helps.
    Thank you! for taking the time to read this!
    I am by no means a pro in VBA so go easy on me :P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •