我正在设置一个自动解决方案,将传入的邮件从 Outlook 导出到 Excel 文件中。
我在网上找到了几种解决方案,但出现编译错误。
我使用的是 Outlook 2016 和 Windows 8.1。
我以为是引用问题,但是我找到了FM20.DLL,还是不行。
我得到的错误:
Compile error: User-defined type not defined
在行 Dim objExcelApp As Excel.Application
Public WithEvents objMails As Outlook.Items
Private Sub Application_Startup()
Set objMails =
Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub objMails_ItemAdd(ByVal Item As Object)
Dim objMail As Outlook.MailItem
Dim strExcelFile As String
Dim objExcelApp As Excel.Application
Dim objExcelWorkBook As Excel.Workbook
Dim objExcelWorkSheet As Excel.Worksheet
Dim nNextEmptyRow As Integer
Dim strColumnB As String
Dim strColumnC As String
Dim strColumnD As String
Dim strColumnE As String
If Item.Class = olMail Then
Set objMail = Item
End If
'Specify the Excel file which you want to auto export the email list
'You can change it as per your case
strExcelFile = "H:\SF_Mail\Emails.xlsx"
'Get Access to the Excel file
On Error Resume Next
Set objExcelApp = GetObject(, "Excel.Application")
If Error <> 0 Then
Set objExcelApp = CreateObject("Excel.Application")
End If
Set objExcelWorkBook = objExcelApp.Workbooks.Open(strExcelFile)
Set objExcelWorkSheet = objExcelWorkBook.Sheets("Sheet1")
'Get the next empty row in the Excel worksheet
nNextEmptyRow = objExcelWorkSheet.Range("B" & objExcelWorkSheet.Rows.Count).End(xlUp).Row + 1
'Specify the corresponding values in the different columns
strColumnB = objMail.SenderName
strColumnC = objMail.SenderEmailAddress
strColumnD = objMail.Subject
strColumnE = objMail.ReceivedTime
'Add the vaules into the columns
objExcelWorkSheet.Range("A" & nNextEmptyRow) = nNextEmptyRow - 1
objExcelWorkSheet.Range("B" & nNextEmptyRow) = strColumnB
objExcelWorkSheet.Range("C" & nNextEmptyRow) = strColumnC
objExcelWorkSheet.Range("D" & nNextEmptyRow) = strColumnD
objExcelWorkSheet.Range("E" & nNextEmptyRow) = strColumnE
'Fit the columns from A to E
objExcelWorkSheet.Columns("A:E").AutoFit
'Save the changes and close the Excel file
objExcelWorkBook.Close SaveChanges:=True
End Sub
最佳答案
引用文献
这是缺少引用时出现的错误。
尝试在工具->引用
中添加:
Microsoft Excel [您的版本]对象库
Microsoft Outlook [您的版本]对象库
代码
尝试使用以下方法更改 Excel 应用
的初始化方式:
Dim objExcelApp As New Excel.Application
而不是:
Dim objExcelApp As Excel.Application
所以你的代码将如下所示:
Private Sub objMails_ItemAdd(ByVal Item As Object)
Dim objMail As Outlook.MailItem
Dim strExcelFile As String
Dim objExcelApp As New Excel.Application
Dim objExcelWorkBook As Excel.Workbook
Dim objExcelWorkSheet As Excel.Worksheet
Dim nNextEmptyRow As Integer
Dim strColumnB As String
Dim strColumnC As String
Dim strColumnD As String
Dim strColumnE As String
If Item.Class = olMail Then
Set objMail = Item
End If
'Specify the Excel file which you want to auto export the email list
'You can change it as per your case
strExcelFile = "H:\SF_Mail\Emails.xlsx"
'Get Access to the Excel file
Set objExcelWorkBook = objExcelApp.Workbooks.Open(strExcelFile)
Set objExcelWorkSheet = objExcelWorkBook.Sheets("Sheet1")
'Get the next empty row in the Excel worksheet
nNextEmptyRow = objExcelWorkSheet.Range("B" & objExcelWorkSheet.Rows.Count).End(xlUp).Row + 1
'Specify the corresponding values in the different columns
strColumnB = objMail.SenderName
strColumnC = objMail.SenderEmailAddress
strColumnD = objMail.Subject
strColumnE = objMail.ReceivedTime
'Add the vaules into the columns
objExcelWorkSheet.Range("A" & nNextEmptyRow) = nNextEmptyRow - 1
objExcelWorkSheet.Range("B" & nNextEmptyRow) = strColumnB
objExcelWorkSheet.Range("C" & nNextEmptyRow) = strColumnC
objExcelWorkSheet.Range("D" & nNextEmptyRow) = strColumnD
objExcelWorkSheet.Range("E" & nNextEmptyRow) = strColumnE
'Fit the columns from A to E
objExcelWorkSheet.Columns("A:E").AutoFit
'Save the changes and close the Excel file
objExcelWorkBook.Close SaveChanges:=True
objExcelApp.Quit 'Quit Excel application
End Sub
注释
通常,使用指令On Error Resume Next
不是一个好主意,因为它会抑制运行时执行时遇到的每个错误。但是,该规则有一些异常(exception),您可以查看@FunThomas 答案以获取澄清。
关于excel - 如何修复编译错误: User-defined type not defined when using Excel VBA from Outlook?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55454980/