none
Powerpoint 2016 Automation when logged into Office 365 VSTO OneDrive RRS feed

  • Question

  • I'm having trouble generating thumbnails in PowerPoint 2016 with VSTO. I'm using the following code to loop through a folder of pptx files and export the first slide to jpg:

    Public Function FolderExists(strFolder As String) As Boolean
        FolderExists = IO.Directory.Exists(strFolder)
    End Function
    
    Public Sub CreateDirectoryIfNotExists(ByVal NewDirectory As String)
        Try
            ' Checking the existence of directory
            If Not FolderExists(NewDirectory) Then
                'If No any such directory then creates the new one
                IO.Directory.CreateDirectory(NewDirectory)
            Else
            End If
        Catch ex As IOException
        End Try
    End Sub
    
    Public Function FolderDeleteIfExists(strPath As String) As Boolean
        Try
            If FolderExists(strPath) Then
                System.IO.Directory.Delete(strPath, True)
            End If
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    
    Sub ClearFolder(strDirectory As String)
        If Not FolderExists(strDirectory) Then
            Exit Sub
        End If
        Dim s As String
        For Each s In System.IO.Directory.GetFiles(strDirectory)
            System.IO.File.Delete(s)
        Next s
    End Sub
    
    Sub ThumbnailGenTest()
    
        Try
            Dim strPath As String = InputBox("Provide repository folder")
            If strPath = "" Then Return
            Dim dir As New IO.DirectoryInfo(strPath)
            Dim strPath_thumbnails As String = IO.Path.Combine(strPath, "Thumbnails")
            Dim diar1 As IO.FileInfo() = dir.GetFiles("*.pptx")
    
            ClearFolder(strPath_thumbnails)
            CreateDirectoryIfNotExists(strPath_thumbnails)
            Dim pres As PowerPoint.Presentation = Nothing
            Dim sld As PowerPoint.Slide = Nothing
            'list the names of all files in the specified directory
            For Each dra As IO.FileInfo In diar1
                pres = Globals.ThisAddIn.Application.Presentations.Open(dra.FullName, True, False, False)
                Dim strFileName As String = strPath_thumbnails & "\" & dra.Name.Replace(".pptx", ".jpg")
                sld = pres.Slides(1)
                sld.Export(strFileName, "jpg")
    
                If sld IsNot Nothing Then sld = Nothing
    
                If pres IsNot Nothing Then
                    pres.Close()
                    pres = Nothing
                End If
            Next
    
            MsgBox("Thumbnails rebuilt", vbInformation, "Test")
        Catch ex As Exception
            MsgBox("There was an error - " & ex.Message, vbInformation, "Error")
        End Try
    End Sub

    After processing anywhere from 100 to 1000 slides the code hangs on pres.Close() - no error, just hangs permanently.

    I've discovered it only happens in 2013 or 2016 when I'm logged into Office 365 in PowerPoint - possibly it's connected to OneDrive.

    I've tried several fixes:

    • System.Threading.Thread.Sleep
    • System.Windows.Forms.Application.DoEvents
    • WaitForPendingFinalizers
    • ReleaseComObject

    I've tried disabling OneDrive using the registry or group policy. Nothing seems to help except logging off the account in PowerPoint or disconnecting the internet.

    Has anyone got any ideas what might be causing it, potential fixes or ways of disabling temporarily Office 365/OneDrive.

    Any ideas appreciated.

    Saturday, June 30, 2018 10:14 AM

All replies

  • Hello cheeseblock,

    I tried to create a simply ppt file with about 200 slides and do the test in Office 2016. I failed to reproduce your issue and I'm sure I logged into Office 365.

    I would suggest you do below test to narrow down the reason range of your issue.

    Log Off Office 365 to do this work. I'm wondering if it is related to logging in Office 365

    Use an another simple Presentation to do the test. I tried to simply to copy a slide with a simple shape to 200 slides. I would suggest you do this work too.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 2, 2018 3:03 AM
  • Hi Terry,

    Thanks for taking a look. I tried with a simple slide with just one shape - I copied it 1000 times into a folder - and it managed to get through 807 slides before hanging. Normally with more complex slides it's quicker to hang.

    I've tried it many times with Office 365 logged off and never get the hang. It's only when I'm logged on.

    Thanks

    Monday, July 2, 2018 11:32 AM
  • Hello,

    Looks like it depends on the memory available on the machine. I'd suggest releasing underlying COM objects instantly to get files released correctly. 

    You can find the definitive answer in the Systematically Releasing Objects article in MSDN (the official source). It states the following:

    All COM add-ins should systematically release their references to COM objects when they are no longer needed. Failing to systematically release reference to objects can prevent Microsoft Office applications from shutting down properly.

    Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Office object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object.

    Also you may consider using the GC methods for swiping the heap instead - an uncontrolled way of releasing COM objects. Don't forget to call them twice, for example:

    GC.Collect
    GC.WaitForPendingFinalizers
    GC.Collect
    GC.WaitForPendingFinalizers

    As for me, I prefer using the ReleaseComObject methods inplace.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, July 2, 2018 3:33 PM
  • Thanks for the suggestion Eugene. I've tried using ReleaseComObject and WaitForPendingFinalizers and it doesn't seem to prevent the hang. The profiler doesn't seem to suggest large memory use and the moment it hangs varies from test to test - sometimes after 100 files sometimes around 1000.

    The only factor that seems to have an effect is going offline or logging out of Office 365 - which then seems to prevent the hang.

    Monday, July 2, 2018 5:38 PM
  • Hi Terry,

    Did you have a chance to try and replicate this issue? I'm wondering whether you also get the problem with more complex slides, whilst logging in? I'm stuck on what might be causing the issue.

    If you don't get the issue, I wondered if you wouldn't mind sharing your test sln so I can compare it to mine and see if there are any differences.

    Appreciate your help

    Dan

    Wednesday, July 4, 2018 7:53 AM
  • Hello cheeseblock,

    Here is the project and ppt file I used for testing. I copied the file to 900+ and the project spend much time to export them all but not hang on Power Point.

    https://1drv.ms/u/s!ArC0gnwxLv5qhyUIi8aLwtlbdjMf

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 5, 2018 7:34 AM
  • Hi Terry,

    Thanks for taking the time to look at the issue. I tried with your code and presentation. I too didn't get the hang.

    However I believe the problem occurs with more complex slides. Although your presentation is large, the slides themselves were simple. So it would take a while to open the file, but the export process would be fast. I think that the export process doesn't fully complete before PowerPoint is trying to close the presentation.

    I've created a more complex presentation here:

    https://1drv.ms/p/s!AlnmMN_fnS8kdT4FsC1IPiJ8nNk

    This has a single complex slide. For me this caused a hang.

    If you have time, would you mind trying it there.

    Many thanks

    Thursday, July 5, 2018 11:14 AM
  • Hello cheeseblock,

    I have copied your presentation 1000 times and after 20 minutes executing, my project successfully export all of the pictures. Did it fail to work for you? Have you tried to test it on another PC?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 6, 2018 7:35 AM
  • Hi Terry,

    I tested with that presentation and it failed after 6 and 92 slides. Generally I've run the test on about 6 different PCs and a number of clients that use the addin are also experiencing it. I've built the addin in both VS 2013 and 2017 to see if that helps. I consistently get the problem - though when it occurs it seems random - it has occurred over 1000 slides before.

    If you are not seeing it, but I get it with the sln you sent - it would perhaps suggest it's something to do with my build process - perhaps a different version of a library. I'll take a look at your sln and compare against mine.

    At the moment it's a bit of a showstopper for our addin - as it makes the software unusable.

    Do you have any other thoughts on what might be happening or what I could try?

    Thanks

    Friday, July 6, 2018 8:00 AM
  • Hi Terry,

    I compared our two projects and there don't appear to be any differences (apart from different GUIDs and the names). 

    Let me know if you have any other thoughts.

    Thanks

    Friday, July 6, 2018 4:05 PM
  • Hi Terry,

    Hope you are well. I'm still at a loss how to resolve this issue. I've been trying to get direct assistance from Microsoft - but I can't seem to find a department who will help with the issue. Have you got any thoughts on the best way to approach microsoft? Or how I might get this resolved - it's a real showstopper for our addin.

    Thanks

    Dan

    Friday, August 17, 2018 4:37 PM