Solved

Printing E-mail Attachments When Selecting E-mail

Posted on 2004-08-26
6
196 Views
Last Modified: 2010-04-08
Hello,

I receive around 20 - 30 e-mails a day, that contain an attachment that I always print out.  Well, doing it as much as I do, it's now a pain - opening e-mail > double clicking file > print, then onto the next e-mail.  Currently, someone has given me a code that I put into Outlook, that when I select e-mails and click a button it will automatically save the attatchments to a specified folder.

Is there some kinda of code inplace, that would act like that in a way?  Meaning, I select all the specific e-mails that I want to deal with, then maybe hit a button then they all start printing out?

Before I had saved all the attachments to the folder, then selected all, then choose print all.  But when I do that, the computer gets crazy and chokes up.  So I'm kinda hoping if what I want is possible & won't choke up my computer.

Thanks,
Jay
0
Comment
Question by:xp310
  • 3
  • 3
6 Comments
 
LVL 13

Accepted Solution

by:
stefri earned 500 total points
ID: 11922279
Try the following code.
Select the mails with attachments and run the macro printAtt
It will
1 - create a temp folder
2 - save the attachments
3 - print it according the extension (doc, xls, ppt, txt, pdf)
     when printing pdf, you may be have to change the name of executable to be run in line
     cmdline = "Acrobat.exe...."
4 - delete the temporary folder and its content

Private Sub printAtt()
Dim objAttFld As MAPIFolder
Dim objInbox As MAPIFolder
Dim objNS As NameSpace
Dim strProgExt As String
Dim objAtt As Attachment
Dim intPos As Integer
Dim i As Integer
Dim strExt As String
Dim fso As Object
Dim tempFolder As Object, myTempFolder As Object
Dim tempName As String
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection

 
Dim olDocApp As Object
Dim olDoc As Object
Dim olXlsApp As Object
Dim olXls As Object
Dim olPptApp As Object
Dim olPpt As Object
Dim itm As Object
Dim wshShell As Object
Dim cmdLine As String
Dim strRun As String

Set myOlExp = Application.ActiveExplorer
Set myOlSel = myOlExp.Selection
 
  On Error Resume Next
  If myOlSel.Count > 0 Then
  Set fso = Application.CreateObject("Scripting.FileSystemObject")
        If Err.Number <> 0 Then
            Err.clear
            MsgBox "Need to have WSH installed on your machine. Sorry.", vbOK, "Error"
            Set fso = Nothing
            Exit Sub
        Else
        Set tempFolder = fso.GetSpecialFolder(2) 'TempFolder
        tempName = tempFolder & "\" & fso.GetTempName
        Set myTempFolder = fso.CreateFolder(tempName)
        For Each itm In myOlSel
          ' itm.PrintOut '------------------ Uncomment if you want to print the selected mail also
          For Each objAtt In itm.Attachments
            intPos = InStrRev(objAtt.FileName, ".")
            strExt = LCase(Mid(objAtt.FileName, intPos + 1))
            Select Case strExt
                Case "doc"
                    objAtt.SaveAsFile myTempFolder & "\" & objAtt.DisplayName
                    Set olDocApp = Application.CreateObject("Word.Application")
                    Set olDoc = olDocApp.Documents.Open(myTempFolder & "\" & objAtt.DisplayName)
                    olDoc.PrintOut
                    olDoc.Close 0
                    olDocApp.Quit
                    Set olDocApp = Nothing
                Case "xls"
                    objAtt.SaveAsFile myTempFolder & "\" & objAtt.DisplayName
                    Set olXlsApp = Application.CreateObject("Excel.Application")
                    Set olXls = olXlsApp.Workbooks.Open(myTempFolder & "\" & objAtt.DisplayName)
                    olXls.PrintOut
                    olXls.Close 0
                    Set olXls = Nothing
                    olXlsApp.Quit
                    Set olXlsApp = Nothing
                Case "ppt"
                    objAtt.SaveAsFile myTempFolder & "\" & objAtt.DisplayName
                    Set olPptApp = Application.CreateObject("Powerpoint.Application")
                    Set olPpt = olPptApp.Presentations.Open(myTempFolder & "\" & objAtt.DisplayName)
                    olPpt.PrintOut
                    olPpt.Close 0
                    Set olPpt = Nothing
                    olPptApp.Quit
                    Set olPptApp = Nothing

                Case "pdf"
                    objAtt.SaveAsFile myTempFolder & "\" & objAtt.DisplayName
   
                    Set wshShell = Application.CreateObject("Wscript.Shell")
                    cmdLine = "Acrobat.exe /p /h " & myTempFolder & "\" & objAtt.DisplayName
                    strRun = wshShell.Run(cmdLine, 1, True)
                    wshShell.Quit
                    Set wshShell = Nothing
                   
               Case "txt"
                    objAtt.SaveAsFile myTempFolder & "\" & objAtt.DisplayName
   
                    Set wshShell = Application.CreateObject("Wscript.Shell")
                    cmdLine = "notepad.exe /p " & myTempFolder & "\" & objAtt.DisplayName
                    strRun = wshShell.Run(cmdLine, 1, True)
                    wshShell.Quit
                    Set wshShell = Nothing
                   
                Case Else
                End Select
                Set objAtt = Nothing
            Next
           
        Next
        Set itm = Nothing
        Set tempFolder = Nothing
      fso.DeleteFolder myTempFolder
      Set fso = Nothing
    End If
End If

End Sub
Hope you'll like it
Stefri
0
 

Author Comment

by:xp310
ID: 11943537
Stefri,

I've just tested out the code which did work.  But I did have two problems.  My first problem is that I can't run it from the macro list.  The name of this code 'printAtt' isn't listed.  I can only run this from the VBA project area.

Also, when I do run it, I get a message saying that if I close out Word, all pending print jobs will be cancelled.  I selected no that I didn't want it to close.  But in doing that, I have to click no for every attachment it's printing.  Is there a way to avoid this?

Jay
0
 
LVL 13

Expert Comment

by:stefri
ID: 11946643
a) subsititue Private to Public
    Private Sub printAtt() -> Public Sub printAtt()
b)                    olDoc.PrintOut
                    Do Until olDocApp.Application.BackgroundPrintingStatus = 0
                    Loop
                    olDoc.Close 0
                    olDocApp.Quit
                    Set olDocApp = Nothing

should do the trick (did it with office 10: word 2002, ol2002)
Stefri
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:xp310
ID: 11956683
Stefri,

It took me a bit to figure out exactly where I was placing Step B.  But I figured it out.  It works great.  Thank you so much.  I appreciate it.

Jay
0
 

Author Comment

by:xp310
ID: 11956848
After further testing this out...

Step B actually freezes up Outlook.  If I leave out step B, and continue to hit the "No" in closing out of Word, then I'm fine.  I guess I can live with those popups.  It's still easier, then dealing with opening all these up seperatly.

Jay
0
 
LVL 13

Expert Comment

by:stefri
ID: 11960915
Oops:
Do Until olDocApp.Application.BackgroundPrintingStatus = 0
should be Do Until olDocApp.BackgroundPrintingStatus = 0
I do not understand why OL freezes: I tested it with Office 10 without problem on a mail having 5 word attachments

Stefri

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
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…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
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: …

707 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

15 Experts available now in Live!

Get 1:1 Help Now