I'm trying a different approach to something that I was working on the other day. At work, we use Outlook 2010 and receive emails with .XLSX attachments throughout the day. I'm trying to figure out how to use VBA in Outlook to check incoming emails for attachments, then if the attachment count is > 0, test the attachment and if it's a spreadsheet, update tblOutlookLog with the senders address book information. This is only my 2nd or third day experimenting with VBA outside of MS Access and I'm fumbling in the dark trying to figure out syntax. I've posted the code below from Outlook below. I get an error in the olInbox_ItemAdd(ByVal Item As Object) section at the .Subject line stating that it is an "invalid or unqualified reference". I apologize in advance in it's sloppy. Thank you for any assistance or direction.

Option Explicit

Private WithEvents InboxItems As Outlook.Items
    Dim olns As NameSpace
    Dim olInbox As MAPIFolder
    Dim olItem As Object
    Dim olAtmt As Attachment
    Dim db As DAO.Database
    Dim rst As DAO.Recordset

    Const strdbPath = "\\FMI-FS\Users\sharp-c\Desktop\"
    Const strdbName = "MSOutlook.accdb"
    Const strTableName = "tblOutlookLog"

Private Sub Application_Startup()
    Set olns = GetNamespace("MAPI")
    Set olInbox = olns.GetDefaultFolder(olFolderInbox).Items
    Set db = OpenDatabase(strdbPath & strdbName)
    Set rst = db.OpenRecordset(strTableName, dbOpenDynaset)
End Sub

Private Sub Application_Quit()
    On Error Resume Next
    rst.Close
    db.Close
    Set olns = Nothing
End Sub


Private Sub olInbox_ItemAdd(ByVal Item As Object)
    Dim olItem As Outlook.MailItem
    Dim olAtmt As Outlook.Attachment
    Dim strFoldername As String
    Dim strFilename As String
    Dim i As Integer
    i = 0

    For Each olItem In olInbox.Items
      For Each olAtmt In olItem.Attachments
        If olItem.olAtmt.Count > 0 Then
            If Right$(olAtmt.FileName, 5) = ".xlsx" Then
                strFilename = "\\FMI-FS\Users\sharp-c\Desktop\Test" & olAtmt.FileName
                olAtmt.SaveAsFile strFilename
                i = i + 1
                    rst.AddNew
                    rst!Subject = Left(.Subject, 255)
                    rst!Sender = .Sender
                    rst!FromAddress = .SenderEmailAddress
                    rst!Status = "Inbox"
                    rst!Logged = .ReceivedTime
                    rst!AttachmentPath = strFilename
                    Next
                    rst.Update
            End If

         Next olAtmt
         Next olItem

        Set olAtmt = Nothing
        Set olItem = Nothing
End Sub
有帮助吗?

解决方案

You need to prefix items with the object:

rst!Subject = Left(olItem.Subject, 255)

And so forth. I think you may have removed With at some stage.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top