Consulting

Results 1 to 1 of 1

Thread: Help exporting attachments

  1. #1
    VBAX Newbie
    Joined
    Jul 2008
    Posts
    1
    Location

    Help exporting attachments

    I have created a VBA script that runs in outlook. It reads the emails in the inbox and exports the data to a database. I have been able to get it to note whether an attachment is present but I cannot seem to get it to attach the file to the database record either in the database (last resort due to size) or save the file to the hard drive and attach a link to the file in the database. Below is the code I have so far and any help would be appreciated. Also anyone know any good resources for learning VBA development and in particular in Office? Thanks

    Sub ExportMailByFolder()
      'Export specified fields from each mail
      'item in selected folder.
      Dim ns As Outlook.NameSpace
      Dim objFolder As Outlook.MAPIFolder
      Set ns = GetNamespace("MAPI")
      Set objFolder = ns.PickFolder
      Dim adoConn As ADODB.Connection
      Dim adoRS As ADODB.Recordset
      Dim intCounter As Integer
      Set adoConn = CreateObject("ADODB.Connection")
      Set adoRS = CreateObject("ADODB.Recordset")
      'DSN and target file must exist.
      adoConn.Open "DSN=OutlookData;"
      adoRS.Open "SELECT * FROM email", adoConn, _
           adOpenDynamic, adLockOptimistic
      'Cycle through selected folder.
      For intCounter = objFolder.Items.Count To 1 Step -1
       With objFolder.Items(intCounter)
       'Copy property value to corresponding fields
       'in target file.
        If .Class = olMail Then
          adoRS.AddNew
          adoRS("Subject") = .Subject
          adoRS("Body") = .Body
          adoRS("FromName") = .SenderName
          adoRS("ToName") = .To
          adoRS("FromAddress") = .SenderEmailAddress
          adoRS("FromType") = .SenderEmailType
          adoRS("CCName") = .CC
          adoRS("BCCName") = .BCC
          adoRS("Importance") = .Importance
          adoRS("Sensitivity") = .Sensitivity
    adoRS("Attachments") = .Attachment
          adoRS.Update
         End If
        End With
       Next
      adoRS.Close
      Set adoRS = Nothing
      Set adoConn = Nothing
      Set ns = Nothing
      Set objFolder = Nothing
    End Sub
    Last edited by adlb1300; 07-22-2008 at 05:28 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •