+ Reply to Thread
Results 1 to 15 of 15

Method 'Open' of object 'Presentations' failed when running a VBA loop

Hybrid View

apantorsk Method 'Open' of object... 06-20-2016, 08:57 AM
Kenneth Hobson Re: Method 'Open' of object... 06-20-2016, 10:10 AM
apantorsk Re: Method 'Open' of object... 06-20-2016, 10:25 AM
Kenneth Hobson Re: Method 'Open' of object... 06-20-2016, 11:06 AM
apantorsk Re: Method 'Open' of object... 06-20-2016, 11:30 AM
Kenneth Hobson Re: Method 'Open' of object... 06-20-2016, 11:44 AM
apantorsk Re: Method 'Open' of object... 06-20-2016, 11:49 AM
Kenneth Hobson Re: Method 'Open' of object... 06-20-2016, 12:01 PM
apantorsk Re: Method 'Open' of object... 06-20-2016, 12:17 PM
Norie Re: Method 'Open' of object... 06-20-2016, 12:19 PM
apantorsk Re: Method 'Open' of object... 06-23-2016, 03:47 AM
Norie Re: Method 'Open' of object... 06-23-2016, 10:04 AM
apantorsk Re: Method 'Open' of object... 06-27-2016, 03:54 AM
apantorsk Re: Method 'Open' of object... 06-28-2016, 10:14 AM
apantorsk Re: Method 'Open' of object... 07-04-2016, 05:54 AM
  1. #1
    Registered User
    Join Date
    06-20-2016
    Location
    Sweden
    MS-Off Ver
    Windows 7
    Posts
    13

    Method 'Open' of object 'Presentations' failed when running a VBA loop

    Hello Excel Gurus,

    This is the first time I am trying to make a VBA macro which generates PDF presentations automatically, so I am a newbie to this. It works to run once, but I get an error message when the macro runs a second time in a loop.

    I get the following two error messages:
    - Method 'Open' of object 'Presentations' failed
    - The remote server machine does not exist or is unavailable (run-time error '462')

    I have Googled around a bit to figure out what the issue is, but not found a solution which I fully understand, i.e. I still do not know what is missing in my code.

    I am using the below code:

    PHP Code: 
    Sub generateReport()
          
        
    Dim PPT As PowerPoint.Application
        Set PPT 
    = New PowerPoint.Application
        PPT
    .Visible True
        
        Dim objReport 
    As Presentation
        Dim objSlide 
    As Slide
        Dim objShape 
    As Object
         
        excelPath 
    Application.ActiveWorkbook.Path
        pptTemplateName 
    "Report.pptx"
        
        'THIS IS WHERE THE ERROR APPEARS IN THE SECOND LOOP'
        
    Set objReport PPT.Presentations.Open(Filename:=excelPath "\" & pptTemplateName)    
        
        'Defining presentation version'
        versionNumber = Sheets("
    Analysis").Range("D4").Value 

        'Inputting text to presentation from Excel'
        Dim i As Integer
        
        firstInputRow = 38
        inputLength = Sheets("
    Analysis").Range("D" & firstInputRow - 3).Value
        
        For i = 1 To inputLength
        
            slideNum = Sheets("
    Analysis").Range("B" & firstInputRow + i - 1).Value
            boxName = Sheets("
    Analysis").Range("C" & firstInputRow + i - 1).Value
            textInput = Sheets("
    Analysis").Range("D" & firstInputRow + i - 1).Value
          
            Set objSlide = objReport.Slides(slideNum)
            objSlide.Shapes(boxName).TextFrame.TextRange.Text = textInput
      
        Next i

        'Updating charts in presentation'
        inputLength = Sheets("
    Analysis").Range("J" & firstInputRow - 3).Value
          
        For i = 1 To inputLength
        
            slideNum = Sheets("
    Analysis").Range("I" & firstInputRow + i - 1).Value
            boxName = Sheets("
    Analysis").Range("J" & firstInputRow + i - 1).Value
            objReport.Slides(slideNum).Shapes(boxName).LinkFormat.Update
          
        Next i
        
        'Saving presentation as PDF'
        FileName2 = Replace(excelPath & "
    \" & versionNumber & " " & pptTemplateName, "pptx", "pdf")
        
        objReport.SaveAs FileName2, ppSaveAsPDF
        objReport.Close

        Set objShape = Nothing
        Set objSlide = Nothing
        Set objReport = Nothing
        
        PPT.Quit
        Set PPT = Nothing
        
    End Sub 

    Any help would be greatly appreciated.
    Last edited by apantorsk; 06-20-2016 at 09:08 AM.

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    Welcome to the forum!

    Maybe replace the Close line with:
    ppt.ActiveWindow Close

  3. #3
    Registered User
    Join Date
    06-20-2016
    Location
    Sweden
    MS-Off Ver
    Windows 7
    Posts
    13

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    Hi Kenneth,

    Thank you for your reply. I appreciate it.

    I replaced the following code:
    objReport.Close
    with the following code:
    PPT.ActiveWindow.Close
    This did not solve the issue, and I still get the same error messages on the second loop.

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    On your 2nd run, run by pressing F8 at each line. Then you can see where it goes amiss.

  5. #5
    Registered User
    Join Date
    06-20-2016
    Location
    Sweden
    MS-Off Ver
    Windows 7
    Posts
    13

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    I did this and I got a surprising result. I scanned through by slowly running the code, line by line, and this way I ran through the whole code without a problem.

    But when I run the code without breaks I get the same error as before on this line when running the second loop:

    Set objReport = PPT.Presentations.Open(Filename:=excelPath & "\" & pptTemplateName)
    The same happens when I run the code with a break on that line.

  6. #6
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    After the 1st run, Alt+Ctrl+Del > Task Manager. Look for an open instance of the file. IF there, delete it (right click and stop the task) and try the 2nd run. I suspect that it is leaving an instance open.

  7. #7
    Registered User
    Join Date
    06-20-2016
    Location
    Sweden
    MS-Off Ver
    Windows 7
    Posts
    13

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    Thank you for the suggestion. How do I look for an open instance of the file?

    Unfortunately my VBA skills are on a quite low level, especially when it comes to PPT connections.

  8. #8
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    That is a Windows thing. It is often referred to as the 3 finger salute. For more detail see: https://technet.microsoft.com/en-us/...(v=ws.11).aspx

    It is an easy way to see if you have an orphan task like the ppt file still open. Setting your object to Nothing should fix that. Howsoever, when it errors, setting those will not happen. You need an Error catching routine.

    e.g.
    '2nd line
    On Error Goto TheEnd
    'line before setting objects to nothing
    TheEnd:
    Of course you may want a 2nd Goto so that you can skip a MsgBox that shows that an error occurred.

    Example error handling:
    Sub AnError()
      CorrectTimepoint 1
    End Sub
    
    Sub NoError()
      CorrectTimepoint 0
      MsgBox "CorrectTimepoint generated no error."
    End Sub
    
    Sub CorrectTimepoint(aNumber As Long)
      Dim Msg As String
      On Error GoTo ErrMsg
      If aNumber <> 0 Then Err.Raise aNumber
      MsgBox "aNumber=0 so no error was generated."
      
      Exit Sub
    ErrMsg:
      Msg = "Error # " & Str(Err.Number) & " was generated by " _
        & Err.Source & Chr(13) & Err.Description
      MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
      End
    End Sub
    Last edited by Kenneth Hobson; 06-20-2016 at 12:12 PM.

  9. #9
    Registered User
    Join Date
    06-20-2016
    Location
    Sweden
    MS-Off Ver
    Windows 7
    Posts
    13

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    What is happening now is that the Macro only generates the first report, and does not generate the reports in the second, third (etc.) loops.

    As per your suggestion my code now looks like this:

    Sub generateReport()
          
        Dim PPT As PowerPoint.Application
        Set PPT = New PowerPoint.Application
        PPT.Visible = True
        
        Dim objReport As Presentation
        Dim objSlide As Slide
        Dim objShape As Object
         
        On Error GoTo TheEnd
    
        excelPath = Application.ActiveWorkbook.Path
        pptTemplateName = "Report.pptx"
        
        'THIS IS WHERE THE ERROR APPEARS IN THE SECOND LOOP'
        Set objReport = PPT.Presentations.Open(Filename:=excelPath & "\" & pptTemplateName)    
        
        'Defining presentation version'
        versionNumber = Sheets("Analysis").Range("D4").Value 
    
        'Inputting text to presentation from Excel'
        Dim i As Integer
        
        firstInputRow = 38
        inputLength = Sheets("Analysis").Range("D" & firstInputRow - 3).Value
        
        For i = 1 To inputLength
        
            slideNum = Sheets("Analysis").Range("B" & firstInputRow + i - 1).Value
            boxName = Sheets("Analysis").Range("C" & firstInputRow + i - 1).Value
            textInput = Sheets("Analysis").Range("D" & firstInputRow + i - 1).Value
          
            Set objSlide = objReport.Slides(slideNum)
            objSlide.Shapes(boxName).TextFrame.TextRange.Text = textInput
      
        Next i
    
        'Updating charts in presentation'
        inputLength = Sheets("Analysis").Range("J" & firstInputRow - 3).Value
          
        For i = 1 To inputLength
        
            slideNum = Sheets("Analysis").Range("I" & firstInputRow + i - 1).Value
            boxName = Sheets("Analysis").Range("J" & firstInputRow + i - 1).Value
            objReport.Slides(slideNum).Shapes(boxName).LinkFormat.Update
          
        Next i
        
        'Saving presentation as PDF'
        FileName2 = Replace(excelPath & "\" & versionNumber & " " & pptTemplateName, "pptx", "pdf")
        
        objReport.SaveAs FileName2, ppSaveAsPDF
        objReport.Close
    
        TheEnd:
    
        Set objShape = Nothing
        Set objSlide = Nothing
        Set objReport = Nothing
        
        PPT.Quit
        Set PPT = Nothing
        
    End Sub

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    Are you calling this sub from a loop in another sub?

    Does this work any better?
    Sub generateReport()
    
    Const ppSaveAsPDF = 32
    
    Dim PPT As Object  ' PowerPoint.Application
    Dim objReport As Object    ' Presentation
    Dim objSlide As Object    ' Slide
    Dim objShape As Object
    Dim excelPath As String
    Dim pptTemplateName As String
    Dim FileName2 As String
    Dim versionNumber As Long
    Dim firstInputRow As Long
    Dim inputLength As Long
    Dim slideNum As Long
    Dim boxName As Long
    Dim textInput As Long
    Dim i As Long
    
        Set PPT = CreateObject("PowerPoint.Application")
    
        PPT.Visible = True
    
        excelPath = Application.ActiveWorkbook.Path
        pptTemplateName = "Report.pptx"
    
        'THIS IS WHERE THE ERROR APPEARS IN THE SECOND LOOP'
        Set objReport = PPT.Presentations.Open(Filename:=excelPath & "\" & pptTemplateName)
    
        'Defining presentation version'
        versionNumber = Sheets("Analysis").Range("D4").Value
    
        'Inputting text to presentation from Excel'
    
    
        firstInputRow = 38
        inputLength = Sheets("Analysis").Range("D" & firstInputRow - 3).Value
    
        For i = 1 To inputLength
    
            slideNum = Sheets("Analysis").Range("B" & firstInputRow + i - 1).Value
            boxName = Sheets("Analysis").Range("C" & firstInputRow + i - 1).Value
            textInput = Sheets("Analysis").Range("D" & firstInputRow + i - 1).Value
    
            Set objSlide = objReport.Slides(slideNum)
            objSlide.Shapes(boxName).TextFrame.TextRange.Text = textInput
    
        Next i
    
        'Updating charts in presentation'
        inputLength = Sheets("Analysis").Range("J" & firstInputRow - 3).Value
    
        For i = 1 To inputLength
    
            slideNum = Sheets("Analysis").Range("I" & firstInputRow + i - 1).Value
            boxName = Sheets("Analysis").Range("J" & firstInputRow + i - 1).Value
            objReport.Slides(slideNum).Shapes(boxName).LinkFormat.Update
    
        Next i
    
        'Saving presentation as PDF'
        FileName2 = Replace(excelPath & "\" & versionNumber & " " & pptTemplateName, "pptx", "pdf")
    
        objReport.SaveAs FileName2, ppSaveAsPDF
        objReport.Close
    
        Set objShape = Nothing
        Set objSlide = Nothing
        Set objReport = Nothing
    
        PPT.Quit
        Set PPT = Nothing
    
    End Sub
    If posting code please use code tags, see here.

  11. #11
    Registered User
    Join Date
    06-20-2016
    Location
    Sweden
    MS-Off Ver
    Windows 7
    Posts
    13

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    A tweaked version of your suggestion worked. I think one of the issues was that I had not defined all variables.

    One issue I have now is that I can run the code line by line, but when I run the whole code - which should generate e.g. 20 different presentations, it sometimes stops in the middle of the code and generates an error.

    I'm not sure if error handling is the issue, but is seems weird to me that the code works fine line by line, but sometimes stops in the middle when i run long loops. Could it have to do with the amount of memory my excel can handle? Any solutions to this?

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    How are you running the code multiple times?

    Within a loop?

    Calling it from within a loop in another sub?

  13. #13
    Registered User
    Join Date
    06-20-2016
    Location
    Sweden
    MS-Off Ver
    Windows 7
    Posts
    13

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    I do this within a loop.

    The algorithm is as follows:
    1. Open PPT
    2. Enter loop
    3. Insert text and update charts
    4. Create PDF of current version of PPT
    5. Next iteration in the loop

  14. #14
    Registered User
    Join Date
    06-20-2016
    Location
    Sweden
    MS-Off Ver
    Windows 7
    Posts
    13

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    VBA sometimes gives me an error after a couple of loops. I'm thinking an issues could be that the code is taking to much processor memory when opening, saving an closing several PPTs and PDFs. Could a solution be to build in 1 sec stops as the code runs?

  15. #15
    Registered User
    Join Date
    06-20-2016
    Location
    Sweden
    MS-Off Ver
    Windows 7
    Posts
    13

    Re: Method 'Open' of object 'Presentations' failed when running a VBA loop

    I solved this by building in an error handler which pauses the program for 3 sec and then reruns the loop.

+ 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. Method 'Open of object 'Workbooks'Failed
    By elmnas in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-16-2015, 07:40 PM
  2. [SOLVED] method 'open text' of object 'workbooks' failed
    By tina in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-23-2013, 11:53 AM
  3. method 'open text' of object 'workbooks' failed
    By richieniel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2013, 10:42 AM
  4. [SOLVED] Method 'open' of object 'workbooks' failed
    By crunchKH in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-01-2013, 09:13 PM
  5. [SOLVED] Method 'Range' of object '_Global' failed error message on running userform when another
    By sivakumar123 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-01-2012, 11:39 AM
  6. Open method of object failed error
    By maw230 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-11-2011, 09:19 AM
  7. Method 'Open' of object 'Workbooks' failed from within browser window
    By ramiro in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-07-2008, 12:08 AM

Tags for this Thread

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