VBA用于解析电子邮件正文的电子邮件正文,写入Excel


0

我有一个要求,我需要一个函数来遍历Outlook(2010)文件夹中的所有电子邮件,并从电子邮件正文中获取电子邮件地址。电子邮件来自Inbox \ Online Applicants \ TEST CB FOLDER

身体中只有一个电子邮件地址。然后,应将此电子邮件写入email_output.xls桌面上的excel文件。

这个论坛帖子我发现并略微修改了最终的宏以尽可能地满足我的需求(只有粗略的VBA知识):

Option Explicit 
Sub badAddress() 
    Dim olApp As Outlook.Application 
    Dim olNS As Outlook.NameSpace 
    Dim olFolder As Outlook.MAPIFolder 
    Dim Item As Object 
    Dim regEx As Object 
    Dim olMatches As Object 
    Dim strBody As String 
    Dim bcount As String 
    Dim badAddresses As Variant 
    Dim i As Long 
    Dim xlApp As Object 'Excel.Application
    Dim xlwkbk As Object 'Excel.Workbook
    Dim xlwksht As Object 'Excel.Worksheet
    Dim xlRng As Object 'Excel.Range
    Set olApp = Outlook.Application 
    Set olNS = olApp.GetNamespace("MAPI") 
    Set olFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("Online Applicants").Folders("TEST CB FOLDER")
    Set regEx = CreateObject("VBScript.RegExp") 
     'define regular expression
    regEx.Pattern = "\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b" 
    regEx.IgnoreCase = True 
    regEx.Multiline = True 
     ' set up size of variant
    bcount = olFolder.Items.Count 
    ReDim badAddresses(1 To bcount) As String 
     ' initialize variant position counter
    i = 0
    ' parse each message in the folder holding the bounced emails
    For Each Item In olFolder.Items 
        i = i + 1 
        strBody = olFolder.Items(i).Body 
        Set olMatches = regEx.Execute(strBody) 
        If olMatches.Count >= 1 Then 
            badAddresses(i) = olMatches(0) 
            Item.UnRead = False 
        End If 
    Next Item
     ' write everything to Excel
    Set xlApp = GetExcelApp 
    If xlApp Is Nothing Then GoTo ExitProc 
    If Not IsFileOpen(Environ("USERPROFILE") & "\Desktop\email_output.xls") Then 
    Set xlwkbk = xlApp.workbooks.Open(Environ("USERPROFILE") & "\Desktop\email_output.xls") 
    End If      
    Set xlwksht = xlwkbk.Sheets(1) 
    Set xlRng = xlwksht.Range("A1") 
    xlApp.ScreenUpdating = False 
    xlRng.Value = "Bounced email addresses" 
    ' resize version
    xlRng.Offset(1, 0).Resize(UBound(badAddresses) + 1).Value = xlApp.Transpose(badAddresses) 
    xlApp.Visible = True 
    xlApp.ScreenUpdating = True 
ExitProc: 
    Set xlRng = Nothing 
    Set xlwksht = Nothing 
    Set xlwkbk = Nothing 
    Set xlApp = Nothing 
    Set olFolder = Nothing 
    Set olNS = Nothing 
    Set olApp = Nothing 
    Set badAddresses = Nothing 
End Sub 
Function GetExcelApp() As Object 
     ' always create new instance
    On Error Resume Next 
    Set GetExcelApp = CreateObject("Excel.Application") 
    On Error GoTo 0 
End Function 
Function IsFileOpen(FileName As String) 
    Dim iFilenum As Long 
    Dim iErr As Long      
    On Error Resume Next 
    iFilenum = FreeFile() 
    Open FileName For Input Lock Read As #iFilenum 
    Close iFilenum 
    iErr = Err 
    On Error GoTo 0      
    Select Case iErr 
    Case 0: IsFileOpen = False 
    Case 70: IsFileOpen = True 
    Case Else: Error iErr 
    End Select      
End Function 

在完成我可以管理的一些其他错误之后,错误object variable or with block variable not set发生在Set xlwksht = xlwkbk.Sheets(1)(第46行)。变量似乎被正确分配,电子表格在桌面上确实存在,名称正确。

Answers:


1

xlwkbk不保证设置:您只在文件不是(未打开)的情况下设置对象。你需要一个“else子句”。

而不是否定FileIsOpen()测试,只需直接使用结果。如:

If FileIsOpen() then
   'Do stuff for when file is open, such as test for the proper worksheet being active
   set worksheet to active sheet
else
   'Open the worksheet like you have in example
   set worksheet by opening worksheet
endif

抱歉,但不是`If Not IsFileOpen(Environ(“USERPROFILE”)&“\ Desktop \ email_output.xls”)然后设置xlwkbk = xlApp.workbooks.Open(Environ(“USERPROFILE”)&“\ Desktop \ email_output。 xls“)结束如果`处理这种可能性?如果是这样,就Else EndProc足够了吗?
JaredT 2016年

“如果”是一个分叉。如果对象只在fork的一侧设置,那么它有可能永远不会被设置。紧跟在“if”块之后的行抛出错误。由于只引用了两个对象,并且当时正在设置其中一个对象,因此我们必须总结未设置的对象变量xlwkbk。在执行期间,if Not (IsFileOpen())必须评估以true正确设置工作簿对象。如果没有(例如当文件打开时{ Not True = False})那么它永远不会被设置。
Yorik 2016年

我知道了。我还有一些学习要做的事情。原来关闭Excel使宏工作按预期工作(可能是次优的)。谢谢(你的)信息!
JaredT 2016年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.