通过VBA保存XLS文件

时间:2013-12-19 08:33:15

标签: excel vba excel-vba

我创建了一个宏,允许XLS文件向所需人员发送电子邮件并附加XLS文件。

如何将文件保存到某个位置?

我有--- ThisWorkbook.SaveAs "testsave" & Format(Now, "dd-mm-yy") & ".xls"

但我希望将文档保存到我D:以外的内部网页上。

我不想创建新的保存,我希望它覆盖现有的文档。

现在保存新文档而不是覆盖原始文档的完整代码

Sub sendemail()

'Save the form to the default location with todays date
Workbook.Save "HolidayReq" & Format(Now, "dd-mm-yy") & ".xls"

'Create the email and attach the form to it
Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
    .To = "[""]"
    .cc = ""
    .BCC = ""
    .Subject = "New Holiday Request on " & Format(Now(), "dd/mm/yyyy") & " by " & Range("C2") & ""
    .Body = ""        .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

1 个答案:

答案 0 :(得分:1)

您应该使用Application.Save代替SaveAs。

这将覆盖原始文档

Application.ThisWorkbook.Save

如果要将文件保存到其他路径,则可以附加路径并将原始文件的名称与SaveAs一起使用

Application.DisplayAlerts = False
Application.ThisWorkbook.SaveAs "C:\Temp\" & ThisWorkbook.Name
Application.DisplayAlerts = True

(如果您没有禁用警报,那么如果该文件已存在,您将收到提示。)


如果要将文件另存为其他类型,则可以使用“另存为”,但指定文件类型。

Application.ThisWorkbook.SaveAs ThisWorkbook.Path & "\test.html", xlHtml

Here is a list of the various Excel FileType constants