Export mail from Outlook to Location on C:


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Export mail from Outlook to Location on C:

  1. #1
    Join Date
    Dec 2008
    Posts
    7

    Question Export mail from Outlook to Location on C:

    Hi Guys,

    I'm trying to write a Macro that can export mail with the received date today from a personal folder called WSP JOBLOG

    It will then take these message(s) and export them to a folder on my C drive located here in:

    C:\Documents and Settings\DentJo\My Documents\WSP_JOBLOG

    The need to be saved to this location in .txt format, I have tried a few things but keep getting runtime errors, anyone done something like this?

    Thanks in advance

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Welcome to DevX
    Quote Originally Posted by flicky_long View Post
    I have tried a few things but keep getting runtime errors
    Post what you have tried, and specify what the runtime errors are along with what piece of code is causing them.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Dec 2008
    Posts
    7

    Question What Ive Tried

    Heres the code that I have, this should in theory export all the messages in the folder WSP JOBLOG to the specified folder and call them the email message subject + .txt, saving them in .txt format, I was then going to modify this to give me just those for today (when I figure out how!!)
    Code:
    Sub ExportAll()
    
    Dim Message As MailItem
    
    Set myNameSpace = Application.GetNamespace("MAPI")
    Set myInbox = myNameSpace.Folders("Personal Folders").Folders("WSP JOBLOG")
    
    For Each Message In myInbox.Items
    Message.SaveAs "C:\Documents and Settings\DentJo\My Documents\WSP_JOBLOG\" & Message.Subject & ".txt", olTXT
    Next Message
    
    End Sub
    Can someone take a look
    Last edited by Hack; 12-05-2008 at 12:35 PM. Reason: Added Code Tags

  4. #4
    Join Date
    Dec 2008
    Posts
    7

    Runtime error

    oh and that gives me the following runtime error:

    'The Operation Failed'

    Very usefuly

  5. #5
    Join Date
    Dec 2008
    Posts
    7
    Are there no VB experts out there to help??!!

  6. #6
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    The only reason this would fail is if the Path parameter is an invalid windows file name and path
    First make sure the destination folder exsists
    Second make sure there are no invalid characters in the subject
    -you must remove them if there are.

  7. #7
    Join Date
    Dec 2008
    Posts
    7
    Well the folder definitely exists, what do you mean by invalid characters?

    The only other thing I can think would be that for some reason outlook doesn't have the rights to write to this destination (it's a work machine), do you think this could be a possibility.

    Thanks for your help.

  8. #8
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    If you can create a text file, manually, in the destination location from your machine then Outlook can also!
    Try it, if it works then it's not a rights issue.

    Windows does not like these characters in it's file and folder names:
    < > " : ? / | \ *

    You Can Fix it like this:
    Code:
      Dim FileName As String
      'create the file name
      FileName = Message.Subject & ".txt"
      'convert invalid characters to an underscore
      FileName = Replace(FileName, "<", "_")
      FileName = Replace(FileName, ">", "_")
      FileName = Replace(FileName, "/", "_")
      FileName = Replace(FileName, "?", "_")
      FileName = Replace(FileName, "\", "_")
      FileName = Replace(FileName, "|", "_")
      FileName = Replace(FileName, ":", "_")
      FileName = Replace(FileName, "*", "_")
      'make the invalid double quote a single quote
      FileName = Replace(FileName, """", "'") 
      'Now Add the Path
      FileName = "C:\Documents and Settings\DentJo\My Documents\WSP_JOBLOG\" & FileName
      'Now save it
      Message.SaveAs FileName , olTXT

  9. #9
    Join Date
    Dec 2008
    Posts
    7
    Thanks so much Ron!

    Thats taken so much head banging to crack, I now have:

    Dim Message As MailItem

    Set myNameSpace = Application.GetNamespace("MAPI")
    Set myInbox = myNameSpace.Folders("Personal Folders").Folders("WSP_JOBLOG")

    For Each Message In myInbox.Items

    Dim FileName As String
    'create the file name
    FileName = Message.Subject & ".txt"
    'convert invalid characters to an underscore
    FileName = Replace(FileName, "<", "_")
    FileName = Replace(FileName, ">", "_")
    FileName = Replace(FileName, "/", "_")
    FileName = Replace(FileName, "?", "_")
    FileName = Replace(FileName, "\", "_")
    FileName = Replace(FileName, "|", "_")
    FileName = Replace(FileName, ":", "_")
    FileName = Replace(FileName, "*", "_")
    'make the invalid double quote a single quote
    FileName = Replace(FileName, """", "'")
    'Now Add the Path
    FileName = "C:\Documents and Settings\DentJo\My Documents\WSP_JOBLOG\" & FileName
    'Now save it
    Message.SaveAs FileName, olTXT

    Next Message

    All I want to do now is just tweak this code so that it only exports the job logs that are received 'today' any idea how I might go about doing this?

    Thanks!

    Jonny

  10. #10
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    Sure Thing! Here is the whole routine:
    Code:
    Public Sub ExportTodaysLogs()
      Dim myNameSpace As NameSpace
      Dim myInbox As MAPIFolder
      Dim Message As MailItem
      Dim FileName As String
      Dim Received As Date
      
      Set myNameSpace = Application.GetNamespace("MAPI")
      Set myInbox = myNameSpace.Folders("Personal Folders").Folders("WSP_JOBLOG")
      
      For Each Message In myInbox.items
        'Remove Time Part of Date Time Value
        Received = Fix(Message.ReceivedTime)
    
        'If Item Was Received Today then Export It
        If Received = Date Then
          'create the file name      
          FileName = Message.Subject & ".txt"
          'convert invalid characters to an underscore
          FileName = Replace(FileName, "<", "_")
          FileName = Replace(FileName, ">", "_")
          FileName = Replace(FileName, "/", "_")
          FileName = Replace(FileName, "?", "_")
          FileName = Replace(FileName, "\", "_")
          FileName = Replace(FileName, "|", "_")
          FileName = Replace(FileName, ":", "_")
          FileName = Replace(FileName, "*", "_")
          'make the invalid double quote a single quote
          FileName = Replace(FileName, """", "'")
    
          'Now Add the Path
          FileName = "C:\Documents and Settings\DentJo\My Documents\WSP_JOBLOG\" & FileName
    
          'If this routine is run more than once in the same day
          'then some files may already exsist, so don't export those
          If Dir(FileName) = "" Then
            'File was not found so it is ok to Save it
            Message.SaveAs FileName, olTXT
          End If
        End If
      Next Message
      
      'Clean up objects
      Set myNameSpace = Nothing
      Set myInbox = Nothing
      Set Message = Nothing
    
    End Sub

  11. #11
    Join Date
    Dec 2008
    Posts
    7
    Thats a Gem, Thanks so much for your help! Really hard to find somebody who knows about outlook vb!

Similar Threads

  1. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  2. SQL 7.0 Mail and Outlook client
    By pchak in forum Database
    Replies: 0
    Last Post: 05-03-2002, 05:36 PM
  3. Outlook - retrieve highlighted mail item
    By James Coulter in forum VB Classic
    Replies: 2
    Last Post: 12-12-2001, 07:19 AM
  4. Outlook - retrieve highlighted mail item
    By James Coulter in forum VB Classic
    Replies: 0
    Last Post: 12-12-2001, 04:50 AM
  5. Export from Exchange 5.5 or Import from Outlook 2000
    By Abdul Majid Khan in forum Enterprise
    Replies: 0
    Last Post: 05-14-2001, 07:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center