How can print all mails in a folder without any space wastage.

Posted on 2009-02-20
Last Modified: 2012-05-06
How can print all mails in a folder without any space wastage.
All set to one size and get them all one after the other in the least blank spaces.

Question by:bsharath
    LVL 50

    Accepted Solution

    this version uses a regex to strip out all blank rows

    Option Explicit
    Sub PrintMe()
        Dim ns As Outlook.NameSpace, oFolder As Outlook.MAPIFolder, targetFolderItems, oMessage As MailItem
        Dim MyFile As String
        Set ns = Application.GetNamespace("MAPI")
        Set oFolder = ns.PickFolder
        Set targetFolderItems = oFolder.Items
        MyFile = "C:\MyFile.txt"
        Open MyFile For Output As #1
        For Each oMessage In targetFolderItems
            Print #1, oMessage.SentOn
            Print #1, KillBlanks(oMessage)
            Print #1, String(100, "*")
            Print #1, vbNewLine
        Next oMessage
        Close #1
        UseWdAgain (MyFile)
    End Sub
    Sub UseWdAgain(sTextFile As String)
        Dim WdApp As Object
        Set WdApp = CreateObject("Word.Application")
        WdApp.Visible = False
        WdApp.ScreenUpdating = True
        WdApp.documents.Open FileName:="C:\MyFile.txt"
        WdApp.Selection.Font.Size = 9
        Set WdApp = Nothing
    End Sub
    Function KillBlanks(ByVal oMessage) As String
        Dim regex, RegM, tempstr As String
        Set regex = CreateObject("vbscript.regexp")
        With regex
            .Global = True
            .multiline = True
            .Pattern = "[\n\r]"
            tempstr = .Replace(oMessage.Body, "@@")
            .Pattern = "@@(@@)*"
            KillBlanks = .Replace(tempstr, Chr(10))
        End With
    End Function

    Open in new window

    LVL 11

    Author Comment

    Thank U..... Worked perfect
    LVL 50

    Expert Comment

    by:Dave Brett
    thx Sharath :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
    Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now