VBA运行时错误';13';
我正在为Outlook组装一个VBA宏,但遇到了一个“运行时错误'13”:类型不匹配。我对VBA没有太多经验,因此我真的需要一些帮助。我正在尝试将Outlook邮件主题和附件名称保存到文本文件中。当到达“xlWB.close”时,我收到了错误13消息VBA运行时错误';13';,vba,outlook,runtime,Vba,Outlook,Runtime,我正在为Outlook组装一个VBA宏,但遇到了一个“运行时错误'13”:类型不匹配。我对VBA没有太多经验,因此我真的需要一些帮助。我正在尝试将Outlook邮件主题和附件名称保存到文本文件中。当到达“xlWB.close”时,我收到了错误13消息 选项显式 子LogToExcel() 将我设置为Outlook.MailItem 将xlApp作为对象 作为对象的Dim xlWB 将图纸作为对象 变暗vText、vText2、vText3、vText4、vText5作为变型 暗计数等于长 Dim
选项显式
子LogToExcel()
将我设置为Outlook.MailItem
将xlApp作为对象
作为对象的Dim xlWB
将图纸作为对象
变暗vText、vText2、vText3、vText4、vText5作为变型
暗计数等于长
Dim bx以布尔形式开始
Dim enviro As字符串
将strPath设置为字符串
将mfolder设置为文件夹
作为附件的Dim oAtt
朦胧如弦
作为字符串的Dim strMail
将项目设置为项目
enviro=CStr(环境(“用户档案”))
'工作簿的路径
strPath=enviro&“\Desktop\outlook\u log.xlsx”
出错时继续下一步
Set xlApp=GetObject(,“Excel.Application”)
如果错误为0,则
Application.StatusBar=“正在打开Excel源,请稍候。。。"
设置xlApp=CreateObject(“Excel.Application”)
bXStarted=True
如果结束
错误转到0
'打开工作簿以输入数据
设置xlWB=xlApp.Workbooks.Open(strPath)
设置xlSheet=xlWB.Sheets(“Sheet1”)
设置mfolder=Application.ActiveExplorer.CurrentFolder
设置selItems=mfolder.Items
对于selItems中的每个项目
strAtt=“”
strMail=“”
如果m.Attachments.Count>0,则
对于每种类型的附件
strAtt=oAtt.FileName&“;“&斯特拉特
下一个月
其他的
strAtt=“无附件”
如果结束
'查找工作表的下一个空行
rCount=xlSheet.Range(“B”和xlSheet.Rows.Count)。结束(-4162)。行
rCount=rCount+1
vText=olItem.SenderName
vText2=olItem.ReceivedTime
vText3=一个单独的主题
vText4=strAtt
vText5=mfolder.Name
xlSheet.Range(“B”和rCount)=vText
xlSheet.Range(“c”&rCount)=vText2
xlSheet.Range(“d”和rCount)=vText3
xlSheet.Range(“e”&rCount)=vText4
xlSheet.Range(“f”&rCount)=vText5
下一个
xlWB.关闭
如果BX启动,那么
xlApp.退出
如果结束
设置xlApp=Nothing
设置xlWB=Nothing
Set xlSheet=无
端接头
13是类型不匹配,这是因为olItem被定义为Outlook.MailItem,但mFolder.Items也可以包含其他内容(如Outlook.MeetingItems)。快速更改代码可能是:
Option Explicit
Sub LogToExcel()
Dim olItem As Outlook.MailItem
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim vText, vText2, vText3, vText4, vText5 As Variant
Dim rCount As Long
Dim bXStarted As Boolean
Dim enviro As String
Dim strPath As String
Dim mfolder As Folder
Dim oAtt As Attachment
Dim strAtt As String
Dim strMail As String
Dim selItems As Items
Dim vItem
enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
strPath = enviro & "\Desktop\outlook_log.xlsx"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Sheet1")
Set mfolder = Application.ActiveExplorer.CurrentFolder
Set selItems = mfolder.Items
For Each vItem In selItems
If TypeOf vItem Is Outlook.MailItem Then
Set olItem = vItem
strAtt = ""
strMail = ""
If olItem.Attachments.Count > 0 Then
For Each oAtt In olItem.Attachments
strAtt = oAtt.FileName & "; " & strAtt
Next oAtt
Else
strAtt = "No Attachments"
End If
'Find the next empty line of the worksheet
rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(-4162).Row
rCount = rCount + 1
vText = olItem.SenderName
vText2 = olItem.ReceivedTime
vText3 = olItem.Subject
vText4 = strAtt
vText5 = mfolder.Name
xlSheet.Range("B" & rCount) = vText
xlSheet.Range("c" & rCount) = vText2
xlSheet.Range("d" & rCount) = vText3
xlSheet.Range("e" & rCount) = vText4
xlSheet.Range("f" & rCount) = vText5
End If
Next vItem
xlWB.Close
If bXStarted Then
xlApp.Quit
End If
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
End Sub
选项显式
子LogToExcel()
将我设置为Outlook.MailItem
将xlApp作为对象
作为对象的Dim xlWB
将图纸作为对象
变暗vText、vText2、vText3、vText4、vText5作为变型
暗计数等于长
Dim bx以布尔形式开始
Dim enviro As字符串
将strPath设置为字符串
将mfolder设置为文件夹
作为附件的Dim oAtt
朦胧如弦
作为字符串的Dim strMail
将项目设置为项目
暗黄
enviro=CStr(环境(“用户档案”))
'工作簿的路径
strPath=enviro&“\Desktop\outlook\u log.xlsx”
出错时继续下一步
Set xlApp=GetObject(,“Excel.Application”)
如果错误为0,则
Application.StatusBar=“正在打开Excel源,请稍候。。。"
设置xlApp=CreateObject(“Excel.Application”)
bXStarted=True
如果结束
错误转到0
'打开工作簿以输入数据
设置xlWB=xlApp.Workbooks.Open(strPath)
设置xlSheet=xlWB.Sheets(“Sheet1”)
设置mfolder=Application.ActiveExplorer.CurrentFolder
设置selItems=mfolder.Items
对于selItems中的每个vItem
如果vItem的类型为Outlook.MailItem,则
设置m=vItem
strAtt=“”
strMail=“”
如果m.Attachments.Count>0,则
对于每种类型的附件
strAtt=oAtt.FileName&“;“&斯特拉特
下一个月
其他的
strAtt=“无附件”
如果结束
'查找工作表的下一个空行
rCount=xlSheet.Range(“B”和xlSheet.Rows.Count)。结束(-4162)。行
rCount=rCount+1
vText=olItem.SenderName
vText2=olItem.ReceivedTime
vText3=一个单独的主题
vText4=strAtt
vText5=mfolder.Name
xlSheet.Range(“B”和rCount)=vText
xlSheet.Range(“c”&rCount)=vText2
xlSheet.Range(“d”和rCount)=vText3
xlSheet.Range(“e”&rCount)=vText4
xlSheet.Range(“f”&rCount)=vText5
如果结束
下一个维特姆
xlWB.关闭
如果BX启动,那么
xlApp.退出
如果结束
设置xlApp=Nothing
设置xlWB=Nothing
Set xlSheet=无
端接头
Option Explicit
Sub LogToExcel()
Dim olItem As Outlook.MailItem
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim vText, vText2, vText3, vText4, vText5 As Variant
Dim rCount As Long
Dim bXStarted As Boolean
Dim enviro As String
Dim strPath As String
Dim mfolder As Folder
Dim oAtt As Attachment
Dim strAtt As String
Dim strMail As String
Dim selItems As Items
Dim vItem
enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
strPath = enviro & "\Desktop\outlook_log.xlsx"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Sheet1")
Set mfolder = Application.ActiveExplorer.CurrentFolder
Set selItems = mfolder.Items
For Each vItem In selItems
If TypeOf vItem Is Outlook.MailItem Then
Set olItem = vItem
strAtt = ""
strMail = ""
If olItem.Attachments.Count > 0 Then
For Each oAtt In olItem.Attachments
strAtt = oAtt.FileName & "; " & strAtt
Next oAtt
Else
strAtt = "No Attachments"
End If
'Find the next empty line of the worksheet
rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(-4162).Row
rCount = rCount + 1
vText = olItem.SenderName
vText2 = olItem.ReceivedTime
vText3 = olItem.Subject
vText4 = strAtt
vText5 = mfolder.Name
xlSheet.Range("B" & rCount) = vText
xlSheet.Range("c" & rCount) = vText2
xlSheet.Range("d" & rCount) = vText3
xlSheet.Range("e" & rCount) = vText4
xlSheet.Range("f" & rCount) = vText5
End If
Next vItem
xlWB.Close
If bXStarted Then
xlApp.Quit
End If
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
End Sub