gpt4 book ai didi

excel - VBA 错误运行时 13 错误类型不匹配找不到它发生的位置

转载 作者:行者123 更新时间:2023-12-04 22:06:59 25 4
gpt4 key购买 nike

我一直在寻找几个小时试图找到如何修复我的代码,但问题是我不知道错误来自哪里!请帮忙!

我不断收到“运行时错误 13 类型不匹配”

Sub Mail_Every_Worksheet()

Dim sh As Worksheet
Dim wb As Workbook
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object

TempFilePath = Environ$("temp") & "\"

If Val(Application.Version) < 12 Then
FileExtStr = ".xlsm": FileFormatNum = 52
End If

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set OutApp = CreateObject("Outlook.Application")

For Each sh In ThisWorkbook.Worksheets
If sh.Range("D9").Value Like "?*@?*.?*" Then

sh.Copy
Set wb = ActiveWorkbook

TempFileName = "Sheet " & sh.Name & " of " _
& ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

Set OutMail = OutApp.CreateItem(0)

With wb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum

On Error Resume Next
With OutMail
.to = sh.Range("D9").Value
.CC = ""
.BCC = ""
.Subject = "WEEKLY BOOKING REPORT " & sh.Range("K3").Value
.Body = "Hi " & sh.Range("D8").Value & vbNewLine & "Please find attached our updated weekly booking report." & vbNewLine & "If I can be of further assistance please do not hesitate to contact me."
.Attachments.Add wb.FullName
.Display 'or use .Send
End With
On Error GoTo 0

.Close savechanges:=False
End With

Set OutMail = Nothing

Kill TempFilePath & TempFileName & FileExtStr

End If
Next sh

Set OutApp = Nothing

With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub

最佳答案

在您最后一次 Dim 之后语句(或在整个 Dim block 之前),输入如下内容:

On Error Goto ErrHandler

然后在程序的底部(紧接在 End Sub 之前),输入如下内容:
ErrHandler:
If Err.Number <> 0 Then
Stop 'DO NOT LEAVE THAT IN PRODUCTION CODE!!!
Resume 'pressing F8 when "Stop" is highlighted will take you to the error line.
End If

请注意,这严格来说是一个“调试”错误处理程序——如果您在生产中有这样的代码,并且遇到错误,VBA IDE 将被调出并显示给您的用户进行调试。如果你离开 Resume在那里,你会有一个很好的无限循环。

还有你的 On Error Goto 0将使错误处理程序无效,因此将其替换为 On Error Goto ErrHandler .

关于excel - VBA 错误运行时 13 错误类型不匹配找不到它发生的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19149019/

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