title: Batch renaming documents in VBA by first line, how to skip non-text?
body: <p>I have a folder full of Word Documents which has recently been "undeleted", while the contents of these files are viewable all metadata has been lost (Most importantly, original file names). I have found a VBA script that will run through a folder and rename any .doc files with the first line of its content. </p>
  
  <p>The scripts works just as expected with documents that contain only text, although many of the files I need renamed are headed with an image. When the script gets to these files it ends and only the files up to that point are renamed.</p>
  
  <p>I have very limited programming knownledge and know next to nothing about VBA but I imagine that I could skip the image with an if|else type statement and use the next line of text as the file name. My problem is I have no idea on how to accomplish this. Also, a method of removing any spaces which appear before the first line of text would be very helpful but is much less important. </p>
  
  <p>The script I am currently working with is as follows:</p>
  
  <pre><code>Public Sub BatchReNameFiles()
  
  Dim myFile As String
  Dim PathToUse As String
  Dim myDoc As Document
  Dim NewName As String
  Dim OldName As String
  Dim oRng As Range
  Dim i As Integer
  Dim j As Integer
  
  'Specify folder where files are located
  PathToUse = "C:\Test\"
  'Count files in folder
  OldName = Dir$(PathToUse &amp; "*.doc")
  While OldName &lt;&gt; ""
  i = i + 1
  OldName = Dir$()
  Wend
  'Rename files
  j = 0
  myFile = Dir$(PathToUse &amp; "*.doc")
  Do While myFile &lt;&gt; "" And j &lt; i
  j = j + 1
  Set myDoc = Documents.Open(FileName:=PathToUse &amp; myFile, Visible:=False)
  With myDoc
  OldName = .FullName
  Set oRng = .Words(1)
  oRng.End = .Words(min(9, .Words.Count - 1)).End
  NewName = Trim(oRng.Text) &amp; ".doc"
  NewName = Replace(NewName, "\", "")
  NewName = Replace(NewName, ":", "")
  NewName = Replace(NewName, """", "")
  NewName = Replace(NewName, vbCr, "")
  NewName = Replace(NewName, vbTab, "")
  .Close SaveChanges:=wdSaveChanges
  End With
  Name OldName As PathToUse &amp; NewName
  myFile = Dir$()
  Loop
  
  End Sub
  Private Function min(a As Long, b As Long)
  min = -((a &lt; b) * a + (a &gt;= b) * b)
  End Function
  </code></pre>
  
  <p>I take no credit for this script, I found it as is while browsing the web looking for a solution. If anyone has any insight into this problem I would greatly appreciate a response.</p>