Microsoft Excel VBA运行时错误5

时间:2016-12-07 09:55:23

标签: vba excel-vba excel

我是Excel VBA的新手。我正在使用Outlook自动化从Outlook电子邮件正文中获取数据,生成报告并发送给团队。我刚刚开始尝试从Outlook电子邮件正文中获取数据并导出到Excel的一些方法。我尝试在电子邮件正文中提取特定数据以导出到Excel工作表,似乎我一直在运行运行时错误5

vText = Trim(M.SubMatches(1))

E.g。

Sub GetData()
    Dim folders As Outlook.folders
    Dim folder As Outlook.MAPIFolder
    Dim iRow As Integer
    Dim Pst_Folder_Name As String
    Dim MailboxName As String
    Dim subFolderName As String
    Dim xlSheet As Object
    Dim Reg As Object
    Dim sText As String
    Dim vText As Variant
    Dim M As Object
    Dim M1 As Object

    MailboxName = "nms@gmail.com"

    Pst_Folder_Name = "My Folders"

    subFolderName = "HTD Ticketing System"

    Set folder = Outlook.Session.folders(MailboxName).folders(Pst_Folder_Name).folders(subFolderName)
    If folder = "" Then
        MsgBox "Invalid Data in Input"
        GoTo end_lbl1:
    End If

    Sheets(1).Activate

    For iRow = 1 To folder.Items.Count
        Sheets(1).Cells(iRow, 1).Select

        sText = folder.Items.Item(iRow).Body

        Set Reg = CreateObject("VBScript.RegExp")

        With Reg
            .Pattern = "(http.)"
        End With

        If Reg.Test(sText) Then
            Set M1 = Reg.Execute(sText)
            For Each M In M1
                vText = Trim(M.SubMatches(1))
            Next
        End If

        Sheets(1).Cells(iRow, 2) = vText

        Set M = Nothing
        Set M1 = Nothing
        Set Reg = Nothing
    Next iRow

    end_lbl1:

End Sub

由于

Niisa

1 个答案:

答案 0 :(得分:3)

SubMatches集合为零索引,因此只需更改为:

vText = Trim(M.SubMatches(0))

目前,只有你想要的所有内容Reg.Global = True才能获得第一场比赛。