title: Powerpoint VBA function return not working
body: <p>This is driving me mad: I have a sub and a function in a powerpoint vba. </p>
  
  <p>The sub starts by allowing me to select a dir. The function, called from the sub, finds a file in the dir. I want it as a function outside of the sub, as I will need to use it multiple times. </p>
  
  <p>The sub is still under development, so doesn't do much, but works. The function works too if I give it something to do - like open the found file (ie uncomment that line in my code below) - but I can't for the life of me get it to return the filePath to the sub. Please help!</p>
  
  <p>The sub:</p>
  
  <pre><code>Sub ManagementSummaryMerge()
  
     Dim folderPath As String
  
     'select dir
     Dim FldrPicker As FileDialog
     Set pptApp = CreateObject("PowerPoint.Application")
     pptApp.Visible = True
  
  
     'Retrieve Target Folder Path From User
     Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
  
     With FldrPicker
        .Title = "Select A Target Folder"
        .AllowMultiSelect = False
  
        If .Show &lt;&gt; -1 Then GoTo NextCode
        folderPath = .SelectedItems(1) &amp; "\"
     End With
  
     'In Case of Cancel
     NextCode:
     folderPath = folderPath
     If folderPath = "" Then GoTo EndOfSub
  
     'set _Main &lt;= string I want to look for
     Dim v As String
     v = "_Main"
  
     Dim fullFilePathIWantToSet As String
  
     'set value of fullFilePathIWantToSet from findFile function
     fullFilePathIWantToSet = findFile(folderPath, v) 
  
     'when I test, this MsgBox appears, but blank
     MsgBox fullFilePathIWantToSet
  
     'If I can get this working properly, I want to be able to do something like this:
  
     'objFSO.CopyFile fullFilePathIWantToSet, duplicateFilePath
     'Presentations.Open (duplicateFilePath)                            
     'numSlides = ActivePresentation.Slides.Count
     'etc
  
  
     EndOfSub:
     'let the sub end
  
  End Sub
  </code></pre>
  
  <p>The function:</p>
  
  <pre><code>Function findFile(ByRef folderPath As String, ByVal v As String) As String
  
      Dim fileName As String
      Dim fullFilePath As String
      Dim duplicateFilePath As String
      Dim numFolders As Long
      Dim numSlides As Integer
  
      Dim folders() As String
      Dim i As Long
  
      If Right(folderPath, 1) &lt;&gt; "\" Then folderPath = folderPath &amp; "\"
      ileName = Dir(folderPath &amp; "*.*", vbDirectory)
  
      While Len(fileName) &lt;&gt; 0
  
          If Left(fileName, 1) &lt;&gt; "." Then
  
              fullFilePath = folderPath &amp; fileName
              duplicateFilePath = folderPath &amp; "duplicate " &amp; fileName
  
              If (GetAttr(fullFilePath) And vbDirectory) = vbDirectory Then
                  ReDim Preserve folders(0 To numFolders) As String
                  folders(numFolders) = fullFilePath
                  numFolders = numFolders + 1
              Else
  
                  'if true, the it matches the string we are looking for
                  If InStr(10, fullFilePath, v) &gt; 0 Then
  
                      'if true, then it isn't in a dir called P/previous, which I want to avoid
                      If InStr(1, fullFilePath, "evious") &lt; 1 Then
  
                          Set objFSO = CreateObject("Scripting.FileSystemObject")
                          Set f = objFSO.GetFile(fullFilePath)
  
                          'If true, then it isn't one of those funny duplicate files that microsoft makes, that has the ~ at the beginning of the file name
                          If f.Size &gt; 5000 Then GoTo ReturnSettings
  
                              ' if we're here then we have found the one single file that we want! Go ahead and do our thing
  
                              findFile = fullFilePath
                              Exit Function
  
                          End If                      
                      End If                 
                  End If                        
              End If     
          End If
  
          fileName = Dir()
  
      Wend
  
      For i = 0 To numFolders - 1
  
          findFile folders(i), v
  
      Next i
  
  End Function
  </code></pre>
  
  <p>I'm a total VBA noob, so have just pva glued this together from what I can find online. Is it not working because of the findFile loop returning an array of one instead of a string? I thought the 'Exit Function' call would do away with that issue.</p>
  
  <p>Please excuse the recursive if statements - the people that I am doing this for don't have a totally standard way of storing their ppts, but this hones down on the ppt I want. When the sub is complete, it will itself loop through 130 sub dirs of the selected dir, and within each of those sub dirs it will grab various slides from six different ppts and merge them into one, ie consolidate data from 780 ppts into 130 - something I definitely want to automate!</p>
  
  <p>This is my first question posted on stack Overflow, so I hope I have posed it clearly and correctly. I have searched extensively for a solution to this. I hope the solution pops out to you! Many thanks in advance.</p>