Results 1 to 4 of 4

Thread: moving mails

  1. #1
    New Lounger
    Join Date
    Aug 2010
    Location
    brussel, brabant, belgium
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i try to move mail from one folder to another. But when i execute the code it only moves half of the mails. any ideas why that is
    here is my code:

    Sub SaveMessages()

    'Declaration
    Dim myItem As Object
    Dim myOlApp As New Outlook.Application
    Dim Fldr As Object
    Dim olTarget As Object



    Set olNS = myOlApp.GetNamespace("MAPI")
    Set Fldr = olNS.Folders("Mailbox - BELGIUMEBS").Folders("inbox").Folders("files")
    Set olTarget = olNS.Folders("Mailbox - BELGIUMEBS").Folders("inbox").Folders("replies")

    icount = 0

    For Each myItem In Fldr.Items
    icount = icount + 1
    myItem.Move olTarget
    Next myItem
    MsgBox icount

    Set myItem = Nothing
    Set myOlApp = Nothing

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,438
    Thanks
    3
    Thanked 125 Times in 118 Posts
    This looks to be caused by the loop elements being removed while the loop is executing. Every loop removes an item from the list but that moves the other elements up a position in the list and the first one that moves slides into a position that the loop has finished with - hence every second item escapes deletion. To avoid this problem you need to step through the list backwards.

    Have a look at this thread Objects not being deleted although there will be plenty of other threads on this forum with similar problem/solutions.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #3
    New Lounger
    Join Date
    Aug 2010
    Location
    brussel, brabant, belgium
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thx,
    tried it and it works

  5. #4
    New Lounger
    Join Date
    Oct 2010
    Location
    UK, Essex, Braintree
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mct View Post
    i try to move mail from one folder to another. But when i execute the code it only moves half of the mails. any ideas why that is
    here is my code:

    Sub SaveMessages()

    'Declaration
    Dim myItem As Object
    Dim myOlApp As New Outlook.Application
    Dim Fldr As Object
    Dim olTarget As Object



    Set olNS = myOlApp.GetNamespace("MAPI")
    Set Fldr = olNS.Folders("Mailbox - BELGIUMEBS").Folders("inbox").Folders("files")
    Set olTarget = olNS.Folders("Mailbox - BELGIUMEBS").Folders("inbox").Folders("replies")

    icount = 0

    For Each myItem In Fldr.Items
    icount = icount + 1
    myItem.Move olTarget
    Next myItem
    MsgBox icount

    Set myItem = Nothing
    Set myOlApp = Nothing

    hey,

    i have had major problems with emailing people and the only code i come up with which works and will go through a recordset correctly was this:

    Dim rst As DAO.Recordset
    Dim strAdrs As String
    Dim strBody As String
    Dim strSQL As String

    strSQL = "SELECT [Text] FROM 'a table or something' WHERE [Title] = 'what its grabing'"

    Set rst = Me.RecordsetClone

    rst.MoveFirst
    Do Until rst.EOF
    strAdrs = strAdrs & rst!'where you clients emails go' & " :"
    rst.MoveNext
    Loop
    strAdrs = Mid(strAdrs, 1)

    Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
    If Not (rst.BOF And rst.EOF) Then

    End If

    If Len(strAdrs) > 0 Then
    Debug.Print strAdrs
    Docmd.SendObject , , , , strAdrs, , subject, rst![Text] 'you letter is the "rst![text]" and will be put in the body of the message
    End If
    End Sub

    this line:
    strAdrs = strAdrs & rst!'where you clients emails go' & " :"

    where " :" is the : is what seprates the email and maybe diffrent depending on what program you use or sending it to, but the code is for outlook
    i really hope this helps you out if you have resolved the problem already, but this worked for me good luck

Posting Permissions

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