Frage

I have this code, it all works fine, but I cannot figure out how to get it to keep the original filename:

Set app = CreateObject("Excel.Application")
app.Visible = False
app.DisplayAlerts = False

Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("F:\Billing_Common\autoemail").Files
  If LCase(fso.GetExtensionName(f)) = "xlsx" Then
    Set wb = app.Workbooks.Open(f.Path)
filename = f
wb.SaveAs "F:\Billing_Common\autoemail\filename.xls", -4143
wb.Close SaveChanges=True

  End if
Next

app.Quit
Set app = Nothing
Set fso = Nothing

Any help would be much appreciated!

Thanks

War es hilfreich?

Lösung

You mean you want to keep the basename and just change the extension? That can be achieved like this:

newname = fso.BuildPath(wb.Path, fso.GetBaseName(wb.Name) & ".xls")
wb.SaveAs newname, -4143

If you want to delete the old file afterwards, you can do it like this:

f.Delete True

I'd do this after closing the workbook, just to be on the safe side.


BTW, I already told you that

wb.Close SaveChanges=True

doesn't do what you seem to think it does. Use just

wb.Close True

without the parameter name.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top