title: VBA Function to return the value only when File can be opened without any error
body: <p>I'm writing an Excel VBA code to Check how many files in a folder are corrupt.
  A folder named as 'Folder' in 'E' drive has these pdf files. </p>
  
  <p>In my workbook; Column A of <strong>Sheet1</strong> has fileNames from this folder.
  I have a code which loops through the filesNames from column A and opens those.</p>
  
  <p><strong>My objective is</strong> : If the file can be opened; then don't print anything in the adjacent cell (of column B) else print as 'Corrupt'.</p>
  
  <p>But, when I run this VBA code; as each time loop goes to the Function OpenPDFPage(), it do not print anything in the adjacent cell of column B.
  (I want to print it only when the file is corrupt and I get message box saying "There was an error opening this document. The file is damaged and could not be repaired")</p>
  
  <p>Can I know what change I've to make to the Function OpenPDFPage() so that, when there is a corrupt file (or the file which can't be opened) in folder; then only code will print "corrupt" in the adjacent cell of column B.</p>
  
  <p>The code is as below:</p>
  
  <pre><code>Option Explicit
  
  Function OpenPDFPage(PDFPath As String) As Boolean
  
      On Error GoTo Error_OpenPDFPage
      ThisWorkbook.FollowHyperlink PDFPath, NewWindow:=True
      OpenPDFPage = True
  
      Exit_OpenPDFPage:
      Exit Function
  
      Error_OpenPDFPage:
      MsgBox Err &amp; ": " &amp; Err.Description
      OpenPDFPage = False
      Resume Exit_OpenPDFPage
  
  End Function
  
  Sub Test()
  
          Dim MyFolder As String
          Dim filename As Range
          Dim MyFile As String
          Dim lastRow As Long
          lastRow = Sheets("Sheet1").UsedRange.Rows.Count
  
          MyFolder = "E:\Folder"
          For Each filename In Worksheets("Sheet1").Range("A2:A" &amp; lastRow)
  
          MyFile = MyFolder &amp; "\" &amp; filename
  
          If OpenPDFPage(MyFile) = True Then
          'Do Nothing
          Else
          filename.Offset(0, 1).Value = "Corrupt"
          End If              
  
          Next
  End Sub
  </code></pre>