Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-01-2020, 12:25 PM
NightWalker NightWalker is offline MailItem Object explanation Windows 10 MailItem Object explanation Office 2016
Novice
MailItem Object explanation
 
Join Date: Apr 2020
Posts: 3
NightWalker is on a distinguished road
Default MailItem Object explanation

I found the following code could someone explain what it is doing. I am trying to save all the attachments in an email folder to a location on my hard drive then move the email and the attachment to a subfolder. I'm trying to do this is in Outlook 2016. I dont understand the MailItem Object very well. Thank you in advance. --Walker







Code:

 For Each Item In olSubFolder.Items
        For Each Attachment In Item.Attachments
            If Attachment.Type = 1 And (InStr(Attachment, "xlsx") Or InStr(Attachment, "xls")) > 0 Then
                FileName = networkFilepath & Attachment.FileName
                'Debug.Print FileName
                Debug.Print Attachment.FileName
'                Debug.Print Item
                Attachment.SaveAsFile FileName
                
            End If
       Next Attachment
Next Item
Reply With Quote
  #2  
Old 04-01-2020, 02:53 PM
NightWalker NightWalker is offline MailItem Object explanation Windows 10 MailItem Object explanation Office 2016
Novice
MailItem Object explanation
 
Join Date: Apr 2020
Posts: 3
NightWalker is on a distinguished road
Default

I have the save the attachments part working but when i try to move the email to a different folder i get a run-time error 13 type mismatch. here is the new code. Could anyone shed some light on this for me. Thank you.



Code:
    For Each Item In olSubFolder.Items
        Set rsOpen = db.OpenRecordset("tbl_EmailInfo")

        For Each Attachment In Item.Attachments
            If Attachment.Type = 1 And (InStr(Attachment, "xlsx") Or InStr(Attachment, "xls")) > 0 Then
                rsOpen.AddNew
                rsOpen![DateEmailRcvd] = Item.ReceivedTime
                rsOpen![AttachmentName] = Attachment.FileName
                rsOpen.Update
               
                FileName = networkFilepath & Attachment.FileName
                Debug.Print Attachment.FileName
                Attachment.SaveAsFile FileName
            End If
        Next Attachment

        If TypeOf Item Is Outlook.MailItem Or Item.Class = 43 Or TypeOf Item Is Outlook.ReportItem Then
        Item.Move (olDestFolder)
        End If
    Next Item
Reply With Quote
  #3  
Old 04-01-2020, 08:55 PM
gmayor's Avatar
gmayor gmayor is offline MailItem Object explanation Windows 10 MailItem Object explanation Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,153
gmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of light
Default

I don't think you can address ReportItem directly from VBA without third party software so the line should be
Code:
If TypeName(Item) = "MailItem" Then
If Item is classed as Object then this should go directly after For Each e.g.

Code:
For Each Item In olSubFolder.Items
    If TypeName(Item) = "MailItem" Then
        If Item.Attachments.Count > 0 then
           'Do stuff
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 04-02-2020, 01:26 PM
NightWalker NightWalker is offline MailItem Object explanation Windows 10 MailItem Object explanation Office 2016
Novice
MailItem Object explanation
 
Join Date: Apr 2020
Posts: 3
NightWalker is on a distinguished road
Default

It is classed as an object. What is TypeName? I will try that code. Thank you. --Walker
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
MailItem Object explanation Image and it's explanation need to stay together mohsen.amiri Drawing and Graphics 2 10-04-2017 04:10 AM
MailItem Object explanation Explanation for this formula Will Excel 7 11-26-2014 05:27 PM
Cannot access most of property of MailItem object on Windows Server 2008 64 bit tz900 Outlook 0 04-10-2012 12:55 PM
MailItem Object explanation Explanation for fields on the form hklein Word VBA 5 10-23-2011 04:10 AM
MailItem Inspectors cache CommandBars but Calendar do not? Hydrogen Outlook 0 03-04-2006 09:50 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 02:26 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft