Solved

Can I write a script to just print attachments in Outlook 2003?

Posted on 2009-05-08
5
214 Views
Last Modified: 2012-05-06
I have multiple emails with attachments that I would like to have print automatically unattended when the email arrives.  I know I can print an email using a rule, but it does not print the attachment.  Any thoughts?

I have tried using the code put up by others on this site, but it seems to do nothing.  Maybe I'm not doing something right.
0
Comment
Question by:TJWheelz68
  • 2
  • 2
5 Comments
 

Expert Comment

by:baburajpanicker
ID: 24338291
you can write a small java (java mail) application ( 1 or 2 java files) which will download the attachments for all unread mails and send to the printer.
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 250 total points
ID: 24338316
Hello TJWheelz68,

Two stages really:

1. A script that will print the attachments ... i.e. as below which when placed in an ordinary module will in association with a rule print the files automatically on receipt.

2. Create a rule to run the script for every incoming email.

Let me know ...

Chris

Regards,
Chris
Function PrintAttachments(mai As mailitem)
Dim objFSO As Object
Dim objTempFolder As Object
Dim olkAttachment As Outlook.Attachment
Dim olkItem As Object
 
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTempFolder = objFSO.GetSpecialFolder(2)
    For Each olkItem In Application.ActiveExplorer.Selection
        For Each olkAttachment In olkItem.Attachments
                olkAttachment.SaveAsFile objTempFolder & "\" & olkAttachment.fileName
                ShellExecute 0&, "print", objTempFolder & "\" & olkAttachment.fileName, 0&, 0&, 0&
        Next
    Next
    
Set olkItem = Nothing
Set olkAttachment = Nothing
Set objTempFolder = Nothing
Set objFSO = Nothing
End Function

Open in new window

0
 

Assisted Solution

by:baburajpanicker
baburajpanicker earned 250 total points
ID: 24345075
import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class GetParts {
  public static void main (String args[])
      throws Exception {
    String host = args[0];
    String username = args[1];
    String password = args[2];

    // Get session
    Session session = Session.getInstance(
      new Properties(), null);

    // Get the store
    Store store = session.getStore("pop3");
    store.connect(host, username, password);

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    BufferedReader reader = new BufferedReader (
      new InputStreamReader(System.in));

    // Get directory
    Message message[] = folder.getMessages();
    for (int i=0, n=message.length; i<n; i++) {
       System.out.println(i + ": "
         + message[i].getFrom()[0]
         + "\t" + message[i].getSubject());

      System.out.println(
        "Do you want to get the content?
           [YES to read/QUIT to end]");
      String line = reader.readLine();
      if ("YES".equals(line)) {
        Object content = message[i].getContent();
        if (content instanceof Multipart) {
          handleMultipart((Multipart)content);
        } else {
          handlePart(message[i]);
        }
      } else if ("QUIT".equals(line)) {
        break;
      }
    }

    // Close connection
    folder.close(false);
    store.close();
  }
  public static void handleMultipart(Multipart multipart)
      throws MessagingException, IOException {
    for (int i=0, n=multipart.getCount(); i<n; i++) {
      handlePart(multipart.getBodyPart(i));
    }
  }
  public static void handlePart(Part part)
      throws MessagingException, IOException {
    String disposition = part.getDisposition();
    String contentType = part.getContentType();
    if (disposition == null) { // When just body
      System.out.println("Null: "  + contentType);
      // Check if plain
      if ((contentType.length() >= 10) &&
          (contentType.toLowerCase().substring(
           0, 10).equals("text/plain"))) {
        part.writeTo(System.out);
      } else { // Don't think this will happen
        System.out.println("Other body: " + contentType);
        part.writeTo(System.out);
      }
    } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
      System.out.println("Attachment: " + part.getFileName() +
        " : " + contentType);
      saveFile(part.getFileName(), part.getInputStream());
    } else if (disposition.equalsIgnoreCase(Part.INLINE)) {
      System.out.println("Inline: " +
        part.getFileName() +
        " : " + contentType);
      saveFile(part.getFileName(), part.getInputStream());
    } else {  // Should never happen
      System.out.println("Other: " + disposition);
    }
  }
  public static void saveFile(String filename,
      InputStream input) throws IOException {
    if (filename == null) {
      filename = File.createTempFile("xx", ".out").getName();
    }
    // Do no overwrite existing file
    File file = new File(filename);
    for (int i=0; file.exists(); i++) {
      file = new File(filename+i);
    }
    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    BufferedInputStream bis = new BufferedInputStream(input);
    int aByte;
    while ((aByte = bis.read()) != -1) {
      bos.write(aByte);
    }
    bos.flush();
    bos.close();
    bis.close();
  }
}
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24534860
Hello TJWheelz68,

Any update?

Chris
0

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.

Join & Write a Comment

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Resolve DNS query failed errors for Exchange
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

19 Experts available now in Live!

Get 1:1 Help Now