gpt4 book ai didi

vba - 通过Excel VBA作为电子邮件附件发送的文件始终损坏

转载 作者:行者123 更新时间:2023-12-03 07:49:52 24 4
gpt4 key购买 nike

我正在使用以下错误处理方法来保存当前打开的文件的副本,如果导致错误,则将其发送到我的电子邮件中。

Private Declare Function GetTempPath _
Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long

Private Const MAX_PATH As Long = 260

Sub MainSub()
Dim OutApp As Object, OutMail As Object
Dim wb As Workbook
On Error GoTo NotifyandRepair
Call Sub1
Call Sub2
Call Subn

Exit Sub
NotifyandRepair:
Set wb = ThisWorkbook

Application.DisplayAlerts = False
wb.SaveAs TempPath & "ErroringFile.xlsx", FileFormat:= _xlNormal,AccessMode:=xlExclusive,ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
Application.DisplayAlerts = True

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

With OutMail
.To = "name@company.com"
.Subject = "Error Occured - Error Number " & Err.Number
.Body = Err.Description
.Attachments.Add TempPath & "ErroringFile.xlsx"

.Send '~~> Change this to .Display for displaying the email
End With

Set OutApp = Nothing: Set OutMail = Nothing
End Sub

Function TempPath() As String
TempPath = String$(MAX_PATH, Chr$(0))
GetTempPath MAX_PATH, TempPath
TempPath = Replace(TempPath, Chr$(0), "")
End Function

它似乎工作正常。发生未处理的错误时,它将向我发送该文件的副本,并将其重命名为“ErroringFile.xlsx”。问题是文件总是损坏。

难道我做错了什么?

我该如何解决此问题,以便文件不会损坏?

最佳答案

您另存为错误的FileFormat。

参见XlFileFormat Enumeration,您应该将其另存为xlOpenXMLWorkbook

关于vba - 通过Excel VBA作为电子邮件附件发送的文件始终损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35713518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com