Results 1 to 10 of 10

Thread: Outlook Macro failing when writing large body

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    6

    Outlook Macro failing when writing large body

    Hi all.

    I set out to do something I though was pretty simple but seems to have weird edge cases I cant explain.

    All I want is a macro that writes an email out to a text file. Heres what I have so far.

    Code:
    Sub BBot(MyMail As MailItem)
    
        Dim FSO As New FileSystemObject
        Dim ts As TextStream
        Dim subject As String
        Dim strID As String
        Dim olNS As Outlook.NameSpace
        Dim oMail As Outlook.MailItem
        
        strID = MyMail.EntryID
        Set olNS = Application.GetNamespace("MAPI")
        Set oMail = olNS.GetItemFromID(strID)
        Set ts = FSO.OpenTextFile("C:\test\email.txt", ForWriting, True)
        
        ts.Write (oMail.subject & vbNewLine)
        ts.Write (oMail.Body)
        ts.Close
        
        Set ts = Nothing
        Set FSO = Nothing
        Set oMail = Nothing
        Set olNS = Nothing
        
    End Sub
    And for the most part it works well, however when a large email comes through (2-3MB) I get "invalid procedure call or argument" on the line where it writes the body. ( ts.Write (oMail.Body) )

    Any ideas? Am I going about this the wrong way?

    Thanks in advance.

  2. #2
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Outlook Macro failing when writing large body

    it may be the type of omail.body

    try doing something stringy with it to see what errors that gives!

    try to stick it into a string concatination and write that out!

    here to guide

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    6

    Re: Outlook Macro failing when writing large body

    Quote Originally Posted by incidentals View Post
    it may be the type of omail.body

    try doing something stringy with it to see what errors that gives!

    try to stick it into a string concatination and write that out!

    here to guide

    I'm not sure exactly what you mean here so I just tried combining the writes into a single call:
    ts.Write (oMail.subject & vbNewLine & oMail.Body)

    Unfortunately, that gave me the same error.

  4. #4
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Outlook Macro failing when writing large body

    the omail.body may more than just a string, it could be some collecton of stuff, so you need to work out what it is and then what you can do with it.

    try viewing it in the immediate window

    to see what it looks like or if you can see anything?

    here to help

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    6

    Re: Outlook Macro failing when writing large body

    Thanks for the help.

    I just popped open the locals window and oMail.Body is listed as "Type: String" with data in it.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    6

    Re: Outlook Macro failing when writing large body

    I also just did a "Debug.Print oMail.Body" in the immediate window, and the body was printed there...

    This seems odd...

  7. #7
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Outlook Macro failing when writing large body

    stranger and stranger...

    lets try something odd

    put the ole.mailbody into a separate string and use the string

    its a "try casting approach" that sometimes works for no good reason!

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    6

    Re: Outlook Macro failing when writing large body

    I fixed this my changing the write method to the following:

    vb Code:
    1. temp = vbNewLine & oMail.subject & vbNewLine & oMail.Body
    2.  
    3.     Dim File As String
    4.     File = "C:\test\email.txt"
    5.     fnum = FreeFile()
    6.     Open File For Output As fnum
    7.     Write #fnum, temp
    8.     Close #fnum
    The same email that would error before, now writes successfully.

    Very odd.

  9. #9
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Outlook Macro failing when writing large body

    good to see a fix for this strangeness, must be an oddity like a null-terminated string, anyway does that mean youv'e finished with this problem for now?

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    6

    Re: Outlook Macro failing when writing large body

    Yes, script is working as intended now.

    Thanks for the help.

Posting Permissions

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



Click Here to Expand Forum to Full Width