+ Responder ao Tópico



  1. #1

    Unhappy Exportando e-mails do OutLook para Excel

    Olá a todos sou novo no Forum...

    Estou trabalhando num código em VB para exportar e-mails do Outlook para o excel, mas ele não esta exportando aquelas menssagens de confirmação de leitura e da erro "error 13" já estou trabalhando nele há 2 semanas e ainda não consegui eliminar este erro, alguém poderia me ajudar? Estou enviando as screens do erro.



    Abraço...
    Miniaturas de Anexos Miniaturas de Anexos Clique na imagem para uma versão maior

Nome:	         error13-debug.jpg
Visualizações:	437
Tamanho: 	88,0 KB
ID:      	11730   Clique na imagem para uma versão maior

Nome:	         error13.jpg
Visualizações:	343
Tamanho: 	77,1 KB
ID:      	11731  


  2. #2

    Padrão Re: Exportando e-mails do OutLook para Excel

    Sub ExportToExcel()

    Dim appExcel As Excel.Application
    Dim wkb As Excel.Workbook
    Dim wks As Excel.Worksheet
    Dim rng As Excel.Range
    Dim strSheet As String
    Dim strPath As String
    Dim intRowCounter As Integer
    Dim intColumnCounter As Integer
    Dim msg As Outlook.MailItem
    Dim nms As Outlook.NameSpace
    Dim fld As Outlook.MAPIFolder
    Dim itm As Object

    strSheet = "Outlookmsgs.xls"
    strPath = "C:\"
    strSheet = strPath & strSheet
    Debug.Print strSheet 'Select export folder
    Set nms = Application.GetNamespace("MAPI")
    Set fld = nms.PickFolder 'Handle potential errors with Select Folder dialog box.
    If fld Is Nothing Then
    MsgBox "There are no mail messages to export", vbOKOnly, _
    "Error"
    Exit Sub
    ElseIf fld.DefaultItemType <> olMailItem Then
    MsgBox "There are no mail messages to export", vbOKOnly, _
    "Error"
    Exit Sub
    ElseIf fld.Items.Count = 0 Then
    MsgBox "There are no mail messages to export", vbOKOnly, _
    "Error"
    Exit Sub
    End If 'Open and activate Excel workbook.
    Set appExcel = CreateObject("Excel.Application")
    appExcel.Workbooks.Open (strSheet)
    Set wkb = appExcel.ActiveWorkbook
    Set wks = wkb.Sheets(1)
    wks.Activate
    appExcel.Application.Visible = True 'Copy field items in mail folder.

    intRowCounter = 2
    For Each itm In fld.Items
    intColumnCounter = 1
    Set msg = itm
    intRowCounter = intRowCounter + 1
    Set rng = wks.Cells(intRowCounter, intColumnCounter)
    rng.Value = msg.To
    intColumnCounter = intColumnCounter + 1
    Set rng = wks.Cells(intRowCounter, intColumnCounter)
    rng.Value = msg.SenderEmailAddress
    intColumnCounter = intColumnCounter + 1
    Set rng = wks.Cells(intRowCounter, intColumnCounter)
    rng.Value = msg.Subject
    intColumnCounter = intColumnCounter + 1
    Set rng = wks.Cells(intRowCounter, intColumnCounter)
    rng.Value = msg.SentOn
    intColumnCounter = intColumnCounter + 1
    Set rng = wks.Cells(intRowCounter, intColumnCounter)
    rng.Value = msg.ReceivedTime
    Next itm


    Set appExcel = Nothing

    Set wkb = Nothing
    Set wks = Nothing
    Set rng = Nothing
    Set msg = Nothing
    Set nms = Nothing
    Set fld = Nothing
    Set itm = Nothing

    Exit Sub

    Set appExcel = Nothing

    Set wkb = Nothing
    Set wks = Nothing
    Set rng = Nothing
    Set msg = Nothing
    Set nms = Nothing
    Set fld = Nothing
    Set itm = Nothing

    End Sub