Lets try this again with your own code down here where I have more room. Try the following and read my comments above. I think you'll find it works:
'I'm adding this line. I'm assuming you have it in your code, but just to be certain...
Dim WB As Excel.Workbook
'This line opens your tab delimeted text file.
Set WB = Workbooks.OpenText(Filename:=folder + file, DataType:=xlDelimited, Tab:=True
If Right(file, 3) = "txt" Or Right(file, 3) = "xls" Then
'This section turns off alerts, saves the workbook opened in the previous step as xlsx and turns alerts back on.
Application.DisplayAlerts = False
WB.SaveAs filename:=folder + milestone + "_" + loadtype + "_" + Left(file, Len(file) - 4) + "_" + metricDate + "_.xlsx", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
Else
'Again, this section saves the workbook opened in the previous step as xlsx.
Application.DisplayAlerts = False
WB.SaveAs filename:=folder + milestone + "_" + loadtype + "_" + Left(file, Len(file) - 5) + "_" + metricDate + "_.xlsx", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
End If
WB.Close
I'm also debating whether or not you really need that If statement. It looks like you're doing the exact same thing and naming the workbook using the same convention. You might not need it. I left it because you didn't ask about it specifically. You could skip it and just save the workbook I think.
Edit: You need the If statement to pick the method you use to open your workbook...
'I'm adding this line. I'm assuming you have it in your code, but just to be certain...
Dim WB As Excel.Workbook
If Right(file, 3) = "txt" then
'This line opens your tab delimeted text file.
Set WB = Workbooks.OpenText(Filename:=folder + file, DataType:=xlDelimited, Tab:=True
Else
'This line opens your xls and xlsx books
Set WB = Workbooks.Open(folder + file) 'no additional parameters should be needed
End If
Application.DisplayAlerts = False
WB.SaveAs filename:=folder + milestone + "_" + loadtype + "_" + Left(file, Len(file) - 4) + "_" + metricDate + "_.xlsx", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
WB.Close
If you're iterating over a number of these input workbooks you may want to do
Set WB = Nothing
just to be safe.
Edit: I'll let my shame hang out up there... the OpenText
method does not return an object, so you'll have to set the WB
object using Set WB = Workbooks(file)
after you've opened it assuming file
is the full filename including extension. My bad on that one.