title: Add filenames to an array and pass it to a sorting function as a string argument
body: <p>The goal is to provide a folder choosing dialogue to read file names and paste them into the open Word document with the file names being the title (above the picture). This is to ease step by step documentations in Word with a style of &quot;1. Do this&quot;, &quot;2. Do that&quot; .... &quot;10. And then that&quot;, &quot;11. And then this&quot; (with it being sorted wrong, i.e. 1, 10, 11, 13, 2, 3, 4, 5, 6, 7, 8, 9 without the sorting function).</p>
  <p>I can't overcome the type mismatch error, that the following VBA code generates (it seems to be the error of String vs. Array type):</p>
  <p>Function:</p>
  <pre class="lang-vb prettyprint-override"><code>Function QuickSortNaturalNum(strArray() As String, intBottom As Integer, intTop As Integer)
  Dim strPivot As String, strTemp As String
  Dim intBottomTemp As Integer, intTopTemp As Integer
  
      intBottomTemp = intBottom
      intTopTemp = intTop
  
      strPivot = strArray((intBottom + intTop) \ 2)
  
      Do While (intBottomTemp &lt;= intTopTemp)
          ' &lt; comparison of the values is a descending sort
          Do While (CompareNaturalNum(strArray(intBottomTemp), strPivot) &lt; 0 And intBottomTemp &lt; intTop)
              intBottomTemp = intBottomTemp + 1
          Loop
          Do While (CompareNaturalNum(strPivot, strArray(intTopTemp)) &lt; 0 And intTopTemp &gt; intBottom)
              intTopTemp = intTopTemp - 1
          Loop
          If intBottomTemp &lt; intTopTemp Then
              strTemp = strArray(intBottomTemp)
              strArray(intBottomTemp) = strArray(intTopTemp)
              strArray(intTopTemp) = strTemp
          End If
          If intBottomTemp &lt;= intTopTemp Then
              intBottomTemp = intBottomTemp + 1
              intTopTemp = intTopTemp - 1
          End If
      Loop
  
      'the function calls itself until everything is in good order
      If (intBottom &lt; intTopTemp) Then QuickSortNaturalNum strArray, intBottom, intTopTemp
      If (intBottomTemp &lt; intTop) Then QuickSortNaturalNum strArray, intBottomTemp, intTop
  End Function
  </code></pre>
  <p>Sub:</p>
  <pre class="lang-vb prettyprint-override"><code>Sub PicWithCaption()
      Dim xFileDialog As FileDialog
      Dim xPath, xFile, xFileNameOnly As String
      Dim xFileNameOnlySorted, xFileNameOnlyUnsorted As Variant
      Dim xFileNameOnlyUnsortedAsString As String
      Dim i, k, l As Integer
      
      l = 1
      m = 100
      
      On Error Resume Next
      
      Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
      
      If xFileDialog.Show = -1 Then
          xPath = xFileDialog.SelectedItems.Item(i)
          If xPath &lt;&gt; &quot;&quot; Then
              xFile = Dir(xPath &amp; &quot;\*.*&quot;)
              For i = 0 To 100
                  Do While xFile &lt;&gt; &quot;&quot;
                      xFileNameOnly = Left(xFile, Len(xFile) - 4)
                      xFileNameOnlyUnsorted(i) = xFileNameOnly
                      ReDim Preserve xFileNameOnlyUnsorted(0 To i) As Variant
                      xFileNameOnlyUnsorted(i) = xFileNameOnlyUnsorted(i).Value
                  Loop
              Next i
              xFileNameOnlySorted = Module1.QuickSortNaturalNum(xFileNameOnlyUnsorted, l, m)
              For xFileNameOnlySorted(k) = 1 To 100
                  If UCase(Right(xFileNameOnlySorted(k), 3)) = &quot;PNG&quot; Or _
                    UCase(Right(xFileNameOnlySorted(k), 3)) = &quot;TIF&quot; Or _
                    UCase(Right(xFileNameOnlySorted(k), 3)) = &quot;JPG&quot; Or _
                    UCase(Right(xFileNameOnlySorted(k), 3)) = &quot;GIF&quot; Or _
                    UCase(Right(xFileNameOnlySorted(k), 3)) = &quot;BMP&quot; Then
                      With Selection
                          .Text = xFileNameOnlySorted(k)
                          .MoveDown wdLine
                          .InlineShapes.AddPicture xPath &amp; &quot;\&quot; &amp; xFile, False, True
                          .InsertAfter vbCrLf
                          .MoveDown wdLine
                      End With
                  End If
              Next xFileNameOnlySorted(k)
              xFile = Dir()
          End If
      End If
  End Sub
  </code></pre>