Você está na página 1de 3

Option Explicit #Const blDevMode = False Sub GerarLista() #If blDevMode Then Dim regX As VBScript_RegExp_55.

RegExp Dim regCollection As VBScript_RegExp_55.MatchCollection Dim regMatch As VBScript_RegExp_55.Match Set regX = New RegExp Dim wb As Excel.Workbook Dim ws As Excel.Worksheet Dim appExcel As Excel.Application #Else Dim regX As Object Dim regCollection As Object Dim regMatch As Object Set regX = CreateObject("VBScript.RegExp") Dim wb As Object Dim ws As Object Dim appExcel As Object #End If Dim Dim Dim Dim Dim Dim Dim Dim sBody As String r As Long c As Long olNS As Outlook.NameSpace olFolder As Outlook.Folder mi As Outlook.MailItem regSubMatch As Variant sErro As String

On Error Resume Next Set appExcel = GetObject(, "Excel.Application") If appExcel Is Nothing Then Err.Clear Set appExcel = CreateObject("Excel.Application") If Err.Number > 0 Then MsgBox "No possvel criar uma Aplicao Excel!" _ , vbCritical _ , "Erro!" GoTo Sair End If End If On Error GoTo 0 appExcel.Visible = True Set wb = appExcel.Workbooks.Add With wb 'Apaga Planilha de Resumo, se existir On Error Resume Next Application.DisplayAlerts = False .Sheets("Resumo").Delete Application.DisplayAlerts = True On Error GoTo Erro 'Cria Planilha de Resumo Set ws = .Sheets.Add

'Cria cabealhos With ws .Name = "Resumo" .Range("A1") = "Nome" .Range("B1") = "Cidade" .Range("C1") = "Dados do Email" .Range("D1") = "Descrio" .Range("E1") = "Idade" .Range("F1") = "E-mail do Responsvel" End With End With With regX .MultiLine = True .Global = False .IgnoreCase = False .Pattern = "Nome:(.+)[\s\S]+Cidade:(.+)[\s\S]+Dados do Email:(.+)[\s\S]+ Descrio:(.+)[\s\S]+Idade:(.+)[\s\S]+E-mail do Responsvel:(.+)" End With Set olNS = Application.GetNamespace("MAPI") 'Troque os valores abaixo para pesquisar a pasta desejada: Set olFolder = olNS.Folders("felipebenza@hotmail.com").Folders("Caixa de Ent rada") r = 2 For Each mi In olFolder.Items sBody = mi.Body Set regCollection = regX.Execute(sBody) If regCollection.Count > 0 Then For Each regMatch In regCollection c = 1 For Each regSubMatch In regMatch.SubMatches ws.Cells(r, c) = Trim(regSubMatch) c = c + 1 Next regSubMatch r = r + 1 Next regMatch End If DoEvents Next mi Sair: Exit Sub Erro: Select Case Err.Number Case -2147221233 sErro = "Especifique, no cdigo, uma pasta vlida do Outlook!" Case Else sErro = "Erro nmero " & Err.Number & "." & vbNewLine & _ "Descrio: " & Err.Description End Select MsgBox sErro, vbCritical GoTo Sair End Sub

#If blDevMode Then Private Function rLast(rng As Excel.Range) As Long #Else Private Function rLast(rng As Object) As Long #End If With rng rLast = .Find(What:="*" _ , After:=.Cells(1) _ , SearchDirection:=xlPrevious _ , SearchOrder:=xlByColumns _ , LookIn:=xlFormulas).Row End With End Function

Você também pode gostar