+ Reply to Thread
Results 1 to 4 of 4

External files cause errors, how do I skip them?

Hybrid View

  1. #1
    Registered User
    Join Date
    11-07-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 365
    Posts
    71

    Question External files cause errors, how do I skip them?

    Hello Excel experts!

    I am working on a PowerPoint file lister, but some of these pptx files are bad. They cannot be opened, and therefore cause errors in my code. I cannot seem to find a method to say "I this line errors, just skip further down to here". All error handling solution I can see are general for the Sub and is not specialised for errors in a single line. I am looking for the VBA equivalent of IFERROR().

    The line in my code that sometimes errors is:
    Set myPresentation = PowerPointApp.Presentations.Open(oFile, ReadOnly:=msoTrue, WithWindow:=msoFalse) 'Problem is here
    I have checked the files when it happens, and these files cannot be opened. My code is otherwise working well.

    Here is my full Sub for reference:

    Sub Files_with_Unicode_Chars()
    Dim FileSysObj As Object      'Scripting.FileSystemObject
    Dim oFolder As Object      'Scripting.Folder
    Dim oFile As Object
    Dim vCurrDir As String, vCrit As String
    
    Dim DestinationPPT As String
    Dim PowerPointApp As PowerPoint.Application
    Dim myPresentation As PowerPoint.Presentation
    Dim Slidecount As Long
    
        Set FileSysObj = CreateObject("Scripting.FileSystemObject")
        
        Set PowerPointApp = CreateObject("PowerPoint.Application")
    
        vCurrDir = "C:\Users\b017646\Downloads\Alle ppt filer"
        vCrit = ".pptx"
    
        Set oFolder = FileSysObj.GetFolder(vCurrDir)
        For Each oFile In oFolder.Files
            With oFile
                Select Case Mid(.Name, InStrRev(.Name, "."))
                Case vCrit
                                    
                    Set myPresentation = PowerPointApp.Presentations.Open(oFile, ReadOnly:=msoTrue, WithWindow:=msoFalse) 'Problem is here
    
    Rem If file is bad, it causes and error. Would like to get around this somehow
    
                    'Ark1.Cells(Row, 1).Value = oFile
                    'Ark1.Cells(Row, 2).Value = myPresentation.Slides.Count
                    'Ark1.Cells(Row, 3).Value = oFile.DateLastModified
                    
                    Debug.Print oFile; myPresentation.Slides.Count; oFile.DateLastModified
        
        End Select: End With: Next
    
    End Sub
    I hope You can help


    GregersDK

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: External files cause errors, how do I skip them?

    Hi there,

    This is untested, but perhaps modifying your code as follows might help:

    
        For Each oFile In oFolder.Files
            With oFile
                Select Case Mid(.Name, InStrRev(.Name, "."))
                Case vCrit
    
                    On Error Resume Next
                        Set myPresentation = Nothing
                        Set myPresentation = PowerPointApp.Presentations.Open(oFile, ReadOnly:=msoTrue, WithWindow:=msoFalse)
                    On Error GoTo 0
    
                    If Not myPresentation Is Nothing Then
    
                        Ark1.Cells(Row, 1).Value = oFile
                        Ark1.Cells(Row, 2).Value = myPresentation.Slides.Count
                        Ark1.Cells(Row, 3).Value = oFile.DateLastModified
                    
                        Debug.Print oFile; myPresentation.Slides.Count; oFile.DateLastModified
    
                    End If
    
        End Select: End With: Next

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    11-07-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 365
    Posts
    71

    Re: External files cause errors, how do I skip them?

    That totally worked, thank you!

    I don't totally understand it, but I have a general gap in my VBA knowledge when it comes to errors.

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: External files cause errors, how do I skip them?

    Hi again,

    Many thanks for your feedback - glad I was able to help.

    Ok, we accept the fact that the following statement may fail occasionally (e.g. if the target file is corrupted),

    
    Set myPresentation = PowerPointApp.Presentations.Open(oFile, ReadOnly:=msoTrue, WithWindow:=msoFalse)
    so we temporarily suspend Excel's normal error handling (i.e. Excel won't bother to generate an error message when it "discovers" that it can't open the target file.)

    When Excel can't open the target file it won't be able to assign any value to the object variable "myPresentation", so its value will remain as the "Nothing" set in the preceding statement.

    We immediately re-enable Excel's normal error handling (On Error GoTo 0) so that any further errors will be handled in the normal way.

    We continue with processing only if the target file was opened successfully, i.e. if the object variable "myPresentation" does NOT have a value of Nothing.

    Regards,

    Greg M

+ 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. [SOLVED] External Links result in #VALUE errors when external file is closed
    By ziggyztz in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-24-2019, 04:41 PM
  2. [SOLVED] Skip columns that do not contain a value or are errors
    By jeroenft in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-01-2018, 08:37 AM
  3. Macro to ignore/skip blank cells/errors during copy
    By jontherev in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-06-2015, 12:52 PM
  4. Sumproduct keeps giving me a problem, how can I formulate to skip blanks/errors
    By deadlyliquidxxx in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-12-2015, 06:52 PM
  5. [SOLVED] PivotItems in a PivotTable position -- Skip if PivotItem errors
    By bcn1988 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-28-2013, 10:03 AM
  6. Skip Cell When Referencing External Sheet
    By Centity in forum Excel General
    Replies: 2
    Last Post: 12-12-2011, 05:07 AM
  7. [SOLVED] External Web Query Errors
    By tokyo4tokyo in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-24-2006, 11:20 PM

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