Results 1 to 5 of 5

vba didnt save all email into specific folder

Threaded View

  1. #1
    Registered User
    Join Date
    07-17-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    48

    Lightbulb vba didnt save all email into specific folder

    Anyone could help me to have a look on this coding as i run it didnt save all the email and will prompt out error 51 or application close

    Sub Workbook_Open()
      Dim oMail As Outlook.MailItem
      
        Dim OutApp As Outlook.Application
        Set OutApp = CreateObject("Outlook.Application")
      Dim objItem As Object
      Dim sPath As String
      Dim dtDate As Date
      Dim sName As String
      Dim enviro As String
      Dim TrimString As String
       enviro = CStr(Environ("USERPROFILE"))
       'For Each objItem In ActiveExplorer.Selection
    
        Set OutMail = OutApp.CreateItem(0)
        Set myNameSpace = OutApp.GetNamespace("MAPI")
        Set myfolders = myNameSpace.Folders
        n = 1
        Do Until myfolders.Item(n) = "Cargoflow GENERAL"
            n = n + 1
        Loop
        Set myfolder = myfolders.Item(n)
        'Level 1 Folder
        Set myfolder2 = myfolder.Folders("Inbox")
        'Level 2 Folder
        
       
       For Each Item In myfolder2.Items
            'Set oMail = objItem
            'Items = Items.Restrict("[Unread] = true")
            
    
            dtDate = Item.ReceivedTime
            itst = Item.SentOn
            Date_Test = Now()
            If itst <= Date_Test Then
                 sName = Item.Subject
                 ReplaceCharsForFileName sName, ""
                'sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
                'vbUseSystem) & Format(dtDate, "-hhnnss", _
                'vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".msg"
                
                TrimString = LTrim(sName)
                sName = TrimString
                
                sPath = "D:\DailyEmail\"
                Debug.Print sPath & sName
    
                Item.SaveAs sPath & sName & ".msg", olMSG
            End If
     
        Next
    
    
    
    
        
        
    End Sub
    
    Private Sub ReplaceCharsForFileName(sName As String, _
      sChr As String _
    )
      sName = Replace(sName, "Chr(34)", sChr)
      sName = Replace(sName, "!", sChr)
      sName = Replace(sName, "@", sChr)
      sName = Replace(sName, "#", sChr)
      sName = Replace(sName, "$", sChr)
      sName = Replace(sName, "%", sChr)
      sName = Replace(sName, "^", sChr)
      sName = Replace(sName, "&", sChr)
      sName = Replace(sName, "*", sChr)
      sName = Replace(sName, "(", sChr)
      sName = Replace(sName, ")", sChr)
      sName = Replace(sName, "=", sChr)
      sName = Replace(sName, "+", sChr)
      sName = Replace(sName, "|", sChr)
      sName = Replace(sName, "[", sChr)
      sName = Replace(sName, "]", sChr)
      sName = Replace(sName, "{", sChr)
      sName = Replace(sName, "}", sChr)
      sName = Replace(sName, "`", sChr)
      sName = Replace(sName, "'", sChr)
      sName = Replace(sName, ";", sChr)
      sName = Replace(sName, ":", sChr)
      sName = Replace(sName, ">", sChr)
      sName = Replace(sName, "<", sChr)
      sName = Replace(sName, "?", sChr)
      sName = Replace(sName, "/", sChr)
    End Sub
    Last edited by gingank; 07-02-2014 at 03:37 AM. Reason: Code

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Button to save workbook, with value in specific cell (H5), in a specific folder
    By markeeny in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-31-2014, 01:15 PM
  2. Create new folder (on hard drive), and save email to it.
    By ashleyuk1984 in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 03-14-2013, 04:10 AM
  3. [SOLVED] Create folder, save as pdf, and email file
    By af_lel in forum Excel Programming / VBA / Macros
    Replies: 31
    Last Post: 02-05-2013, 07:35 AM
  4. [SOLVED] Macro to save workbook to specific folder with a specific name, then kill/disable itself.
    By jonvanwyk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-18-2012, 03:50 PM
  5. [SOLVED] can i recover a file i didnt save nor have i closed out?
    By collin in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-12-2005, 03:10 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1