Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

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

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
TJWheelz68
Asked:
TJWheelz68
  • 2
  • 2
2 Solutions
 
baburajpanickerCommented:
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
 
Chris BottomleyCommented:
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
 
baburajpanickerCommented:
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
 
Chris BottomleyCommented:
Hello TJWheelz68,

Any update?

Chris
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now