+ Reply to Thread
Results 1 to 4 of 4

VBA runtime error in ppt vba code

  1. #1
    Registered User
    Join Date
    05-10-2023
    Location
    New jersey, USA
    MS-Off Ver
    Microsoft 365
    Posts
    2

    VBA runtime error in ppt vba code

    Dim pptApp As Object

    Sub copyAllSlidesFromMultipleFiles()
    'Prompt the user to select a folder for the target presentation
    Dim targetFolderPath As String
    With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a Folder for the Target Presentation"
    .ButtonName = "Select"
    .AllowMultiSelect = False
    If .Show = -1 Then
    targetFolderPath = .SelectedItems(1) & ""
    Else
    MsgBox "No folder selected for the target presentation"
    Exit Sub
    End If
    End With

    'Search for the latest .pptm file in the specified folder
    Dim targetFileName As String
    Dim latestFile As String
    Dim latestDate As Date

    targetFileName = Dir(targetFolderPath & "*.pptm")
    Do While targetFileName <> ""
    If FileDateTime(targetFolderPath & targetFileName) > latestDate Then
    latestFile = targetFileName
    latestDate = FileDateTime(targetFolderPath & targetFileName)
    End If
    targetFileName = Dir()
    Loop

    'Check if a file was found
    If latestFile = "" Then
    MsgBox "No PowerPoint files found in the specified folder for the target presentation"
    Exit Sub
    End If

    'Set the latest presentation in the specified folder as the target presentation
    Dim targetPresentation As Presentation
    Set targetPresentation = Presentations(targetFolderPath & latestFile)

    'Prompt the user for the number of source presentations
    Dim numSourcePresentations As Integer
    numSourcePresentations = InputBox("Enter the number of source presentations:")

    'Prompt the user to select each source presentation and copy its slides to the target presentation
    Dim i As Integer
    For i = 1 To numSourcePresentations
    'Prompt the user to select a folder for the source presentation
    Dim sourceFolderPath As String
    With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a Folder for Source Presentation " & i
    .ButtonName = "Select"
    .AllowMultiSelect = False
    If .Show = -1 Then
    sourceFolderPath = .SelectedItems(1) & ""
    Else
    MsgBox "No folder selected for source presentation " & i
    Exit Sub
    End If
    End With

    'Search for the most recent .pptx or .pptm file in the specified folder for the source presentation
    Dim sourceFileName As String
    Dim mostRecentFile As String
    Dim mostRecentDate As Date

    sourceFileName = Dir(sourceFolderPath & "*.pptx")
    Do While sourceFileName <> ""
    If FileDateTime(sourceFolderPath & sourceFileName) > mostRecentDate Then
    mostRecentFile = sourceFileName
    mostRecentDate = FileDateTime(sourceFolderPath & sourceFileName)
    End If
    sourceFileName = Dir()
    Loop

    sourceFileName = Dir(sourceFolderPath & "*.pptm")
    Do While sourceFileName <> ""
    If FileDateTime(sourceFolderPath & sourceFileName) > mostRecentDate Then
    mostRecentFile = sourceFileName
    mostRecentDate = FileDateTime(sourceFolderPath & sourceFileName)
    End If
    sourceFileName = Dir()
    Loop

    'Check if a file was found for the source presentation and open it if it was found.
    If mostRecentFile <> "" Then

    Dim pptApp As Object
    If PowerPointIsRunning() Then
    Set pptApp = GetObject(, "Powerpoint.Application")
    Else
    Set pptApp = CreateObject("Powerpoint.Application")
    End If

    Dim sourcePresentation As Object
    Set sourcePresentation = pptApp.Presentations.Open(sourceFolderPath & mostRecentFile)

    'Copy all non-hidden slides from the source presentation to the target presentation after the currently selected slide.
    If targetPresentation.Slides.Count = 0 Then
    MsgBox "Target presentation is empty"
    Exit Sub
    End If

    Dim slide As slide
    Dim currentIndex As Long
    currentIndex = targetPresentation.Windows(1).Selection.SlideRange.SlideIndex

    For Each slide In sourcePresentation.Slides
    If Not slide.SlideShowTransition.Hidden Then
    slide.Copy
    targetPresentation.Slides.Paste currentIndex + 1
    currentIndex = currentIndex + 1

    End If
    Next slide

    'Close the source presentation after copying its slides to the target presentation.

    sourcePresentation.Close
    Set sourcePresentation = Nothing

    Else
    MsgBox "No PowerPoint files found in specified folder for Source Presentation " & i
    End If

    Next i

    End Sub

    Function PowerPointIsRunning() As Boolean
    Dim pptApp As Object
    On Error Resume Next
    Set pptApp = GetObject(, "Powerpoint.Application")
    If Not pptApp Is Nothing Then
    PowerPointIsRunning = True
    Else
    PowerPointIsRunning = False
    End If
    Set pptApp = Nothing
    End Function
    Last edited by sps225; 05-12-2023 at 10:24 AM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,532

    Re: VBA runtime error in ppt vba code

    In the debugger, examine the value of the expression

    sourceFolderPath & mostRecentFile

    when the error occurs. What is it?

    I do not see any references to Excel in this code. Any reason you didn't write it in a PowerPoint file? Then you don't need to create and manage a PowerPoint application object. In any case it's not really an Excel question.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    05-10-2023
    Location
    New jersey, USA
    MS-Off Ver
    Microsoft 365
    Posts
    2

    Re: VBA runtime error in ppt vba code

    Apologies, this is a ppt macro. I am getting the error attached when it runs multiple times (given 3 source presentations to open for example).
    Attached Images Attached Images

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,532

    Re: VBA runtime error in ppt vba code

    First you did not address my question. This is essential to solving your problem.

    Quote Originally Posted by 6StringJazzer View Post
    In the debugger, examine the value of the expression

    sourceFolderPath & mostRecentFile

    when the error occurs. What is it?
    Second, for some reason you removed the CODE tags I added to your first post. You will need to restore them. You also removed the description of your problem.

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Last edited by 6StringJazzer; 05-12-2023 at 10:46 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. I get a runtime error with this code
    By pwyller in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-08-2023, 02:26 PM
  2. Runtime Error 1004 in VBA code
    By viswanathchenna04 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-20-2020, 09:25 AM
  3. [SOLVED] Runtime error '91'; in my code please help!
    By A440 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-08-2017, 02:28 PM
  4. Runtime error 13 problem with a code
    By elmnas in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-28-2015, 07:54 AM
  5. [SOLVED] Getting a runtime error with this code
    By rinser in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-20-2013, 10:22 AM
  6. [SOLVED] Range error in code, runs alone but not inside my full program, giving runtime error 1004
    By charizzardd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-23-2012, 03:34 PM
  7. runtime error 9 problem with this code
    By cuewoz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-03-2006, 09:54 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1