Automatically Printing/Saving Emails/Attachments in Outlook

AID: 2924
  • Status: Published

12733 points

  • ByBlueDevilFan
  • TypeGeneral
  • Posted on2010-04-22 at 05:14:56
Awards
  • Community Pick
  • Experts Exchange Approved
Automatically Printing/Saving Emails/Attachments in Outlook

Issue.  One of the more frequent Outlook questions I see here on EE is "How can I automatically save or print an email and/or its attachments?"  There are a number of variations to the basic question.  For example, "How can I save attachments to a specific file system folder?" or "How can I print a message and all its attachments to a specific printer?" or "How can I print a specific type of attachment?" or "My organization places a strict limit on mailbox size.  How can I save attachments to the file system and insert a link to them in the original message thereby saving space in my mailbox?".

Background.  Outlook’s rules wizard does include the "print it" action which will print the message, but will not print the attachments.  If you manually print an item, then you can elect to print the attachments too, but only to the default printer.  The rules wizard does not include an action for saving a message or attachment to the file system, nor saving and replacing attachments with hyperlinks to them.  

Solution.  The solution is to use a bit of scripting, a macro, and a rule to perform the desired actions.  The rule is triggered when an item meeting a given condition arrives.  The rule calls the macro which performs the actual work.  

Until now I’ve handled each question requesting one of these capabilities individually.  That is, I wrote a custom macro to address each author’s specific needs.  If the author wanted to print a given attachment, then I produced a macro that would do just that.  If instead they wanted to save and remove attachments replacing them with hyperlinks in the message itself, then I wrote a macro for that alone.  

Recently I saw another of these questions and decided it was time to put together a macro that would handle almost any of these situations.  This macro has the ability to perform any combination of the following actions:

  a.  Print the message.
  b.  Print the message to a specific printer.
  c.  Save the message to a specific folder in the file system.
  d.  Save the message in a specific format.
  e.  Print all attachments.
  f.  Print only certain types of attachments (e.g. all .doc, .wks, .pdf).
  g.  Print attachments to a specific printer.
  h.  Save attachments to a specific folder in the file system.
  i.  Remove (save and delete) attachments replacing each with a hyperlink to the saved attachment.
      The hyperlinks are inserted at the bottom of the message.

Requirements.  Microsoft Outlook.  The macro should work with any version of Outlook from 2000 on, but I’ve only tested it with 2007.  The instructions assume that you are using Outlook 2007.

Instructions.  Follow these instructions to use this solution.

1

Add the Macro to Outlook

  a.  Start Outlook.
  b.  Click Tools > Macro  > Visual Basic Editor.
  c.  If not already expanded, expand Microsoft Office Outlook Objects.
  d.  If not already expanded, expand Modules.
  e.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module
       by right-clicking Modules and selecting Insert  > Module.
  f.  Copy the code below and paste it into the right-hand pane of Outlook's VB Editor window.
  g.  Edit the code as needed.  I included comments wherever something needs to or can change.
  h.  Click the diskette icon on the toolbar to save the changes.

Public Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" _
        (ByVal lpAppName As String, ByVal lpKeyName As String, _
        ByVal lpDefault As String, ByVal lpReturnedString As String, _
        ByVal nSize As Long) As Long

Private Declare Function ShellExecute Lib "shell32.dll" _
  Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
  ByVal lpFile As String, ByVal lpParameters As String, _
  ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub MessageAndAttachmentProcessor(Item As Outlook.MailItem, _
    Optional bolPrintMsg As Boolean, _
    Optional bolSaveMsg As Boolean, _
    Optional bolPrintAtt As Boolean, _
    Optional bolSaveAtt As Boolean, _
    Optional bolInsertLink As Boolean, _
    Optional strAttFileTypes As String, _
    Optional strFolderPath As String, _
    Optional varMsgFormat As OlSaveAsType, _
    Optional strPrinter As String)
   
    Dim olkAttachment As Outlook.Attachment, _
        objFSO As FileSystemObject, _
        strMyPath As String, _
        strExtension As String, _
        strFileName As String, _
        strOriginalPrinter As String, _
        strLinkText As String, _
        strRootFolder As String, _
        strTempFolder As String, _
        varFileType As Variant, _
        intCount As Integer, _
        intIndex As Integer, _
        arrFileTypes As Variant

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    strTempFolder = Environ("TEMP") & "\"
    
    If strAttFileTypes = "" Then
        arrFileTypes = Array("*")
    Else
        arrFileTypes = Split(strAttFileTypes, ",")
    End If
    
    If bolPrintMsg Or bolPrintAtt Then
        If strPrinter <> "" Then
            strOriginalPrinter = GetDefaultPrinter()
            SetDefaultPrinter strPrinter
        End If
    End If
    
    If bolSaveMsg Or bolSaveAtt Then
        If strFolderPath = "" Then
            strRootFolder = Environ("USERPROFILE") & "\My Documents\"
        Else
            strRootFolder = strFolderPath & IIf(Right(strFolderPath, 1) = "\", "", "\")
        End If
    End If
    
    If bolSaveMsg Then
        Select Case varMsgFormat
            Case olHTML
                strExtension = ".html"
            Case olMSG
                strExtension = ".msg"
            Case olRTF
                strExtension = ".rtf"
            Case olDoc
                strExtension = ".doc"
            Case olTXT
                strExtension = ".txt"
            Case Else
                strExtension = ".msg"
        End Select
        Item.SaveAs strRootFolder & RemoveIllegalCharacters(Item.Subject) & strExtension, IIf(varMsgFormat <> 0, varMsgFormat, olMSG)
    End If
        
    For intIndex = Item.Attachments.count To 1 Step -1
        Set olkAttachment = Item.Attachments.Item(intIndex)
        'Print the attachments if requested'
        If bolPrintAtt Then
            If olkAttachment.Type <> olEmbeddeditem Then
                For Each strFileType In arrFileTypes
                    If (strFileType = "*") Or (LCase(objFSO.GetExtensionName(olkAttachment.FileName)) = LCase(strFileType)) Then
                        olkAttachment.SaveAsFile strTempFolder & olkAttachment.FileName
                        ShellExecute 0&, "print", strTempFolder & olkAttachment.FileName, 0&, 0&, 0&
                    End If
                Next
            End If
        End If
        'Save the attachments if requested'
        If bolSaveAtt Then
            strFileName = olkAttachment.FileName
            intCount = 0
            Do While True
                strMyPath = strRootFolder & strFileName
                If objFSO.FileExists(strMyPath) Then
                    intCount = intCount + 1
                    strFileName = "Copy (" & intCount & ") of " & olkAttachment.FileName
                Else
                    Exit Do
                End If
            Loop
            olkAttachment.SaveAsFile strMyPath
            If bolInsertLink Then
                If Item.BodyFormat = olFormatHTML Then
                    strLinkText = strLinkText & "<a href=""file://" & strMyPath & """>" & olkAttachment.FileName & "</a><br>"
                Else
                    strLinkText = strLinkText & strMyPath & vbCrLf
                End If
                olkAttachment.Delete
            End If
        End If
    Next
    
    If bolPrintMsg Then
        Item.PrintOut
    End If
    
    If bolPrintMsg Or bolPrintAtt Then
        If strOriginalPrinter <> "" Then
            SetDefaultPrinter strOriginalPrinter
        End If
    End If
    
    If bolInsertLink Then
        If Item.BodyFormat = olFormatHTML Then
            Item.HTMLBody = Item.HTMLBody & "<br><br>Removed Attachments<br><br>" & strLinkText
        Else
            Item.Body = Item.Body & vbCrLf & vbCrLf & "Removed Attachments" & vbCrLf & vbCrLf & strLinkText
        End If
        Item.Save
    End If

    Set objFSO = Nothing
    Set olkAttachment = Nothing
End Sub

Function GetDefaultPrinter() As String
    Dim strPrinter As String, _
        intReturn As Integer
    strPrinter = Space(255)
    intReturn = GetProfileString("Windows", ByVal "device", "", strPrinter, Len(strPrinter))
    If intReturn Then
        strPrinter = UCase(Left(strPrinter, InStr(strPrinter, ",") - 1))
    End If
    GetDefaultPrinter = strPrinter
End Function

Function RemoveIllegalCharacters(strValue As String) As String
    ' Purpose: Remove characters that cannot be in a filename from a string.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    RemoveIllegalCharacters = strValue
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "<", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ">", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ":", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, Chr(34), "'")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "/", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "\", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "|", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "?", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "*", "")
End Function

Sub SetDefaultPrinter(strPrinterName As String)
    Dim objNet As Object
    Set objNet = CreateObject("Wscript.Network")
    objNet.SetDefaultPrinter strPrinterName
    Set objNet = Nothing
End Sub
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:

Select allOpen in new window



2

Create a Subroutine That Calls the Macro

We can’t call the macro directly from a rule since the macro requires parameters to tell it what actions to take and we can’t pass those parameters from a rule.  Instead you need to create a subroutine of your own that calls the macro and tells it which actions you want it to perform for the given message.  You can create as many subroutines as you need.  Typically you’ll have one subroutine for each set of actions you want to perform.  

For example, assume that you receive messages pertaining to a project you’re working on, we’ll call it Project X, and you receive a daily message from accounting that includes a rather large spreadsheet attachment.  You want to automatically print and save the Project X messages, while for the accounting messages you want to remove the attached spreadsheet and replace it with a hyperlink.  Since the actions are required are different you’d need to create a subroutine for each.  You can create your subroutine(s) in the same module with the other macro code or you can place it in another module.  The decision is yours.  

To create a subroutine:

  a.  Start Outlook.
  b.  Click Tools > Macro  > Visual Basic Editor.
  c.  If not already expanded, expand Microsoft Office Outlook Objects.
  d.  If not already expanded, expand Modules.
  e.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by
       right-clicking Modules and selecting Insert > Module.
  f.  Copy the code below and paste it into the right-hand pane of Outlook's VB Editor window.
  g.  Edit the code.  At a minimum you must give the subroutine a unique name and you must
       set the parameters.  
  h.  Click the diskette icon on the toolbar to save the changes.

Parameters.
The macro takes a maximum of nine parameters.  In the code below these are represented as P1 through P9.  The parameters are positional (i.e. they must appear in the sequence given).

P1.  Print the message.
Tells the macro to print the email.  Valid values are True or False.

P2.  Save the message.
Tells the macro to save the email to the file system.  Valid values are True or False.

P3.  Print the attachments.
Tells the macro to print the attachments.  Valid values are True or False.

P4.  Save the attachments.
Tells the macro to save the attachments.  Valid values are True or False.

P5.  Remove attachments.
Tells the macro to remove the attachments and insert hyperlinks to  them at the bottom of the message.  Valid values are True or False.

P6. Attachment types.
Tells the macro what attachment types to save/print.  The macro will only process attachments that match the file types.  This parameter is a comma separated list of file extensions.  For example, to only process Word documents (both 2007 and earlier) you’d set this parameter to "doc,docx".

P7. Target file system folder.
This tells the macro which file system folder to save the message and/or attachments to.  Valid values are any existing file system folder, including network shares and UNC paths.  If you’ve told the macro to save the message and/or attachments and you fail to specify this parameter, then the macro will save the items to your My Documents folder.

P8. Message save format.
Tells the macro what format to save the message in (assuming that you are saving the message).  You will be prompted with a list of valid values when you enter this parameter.

P9. Printer name.
Tells the macro what printer to print the message and/or attachments to.  This allows you to print to any printer, not just the default printer.  Valid values include the name of any printer that appears in your list of printers.  If you’ve told the macro to print the message and/or attachments and you don’t specify a printer, then the macro will print them to your default printer.

All the parameters are optional so you can omit those that you don’t need.

‘Change MySubroutineName to a unique name on the next line’
Sub MySubroutineName(Item As Outlook MailItem)
        MessageAndAttachmentProcessor Item, P1, P2, P3, P4, P5, P6, P7, P8, P9
End Sub
                                    
1:
2:
3:
4:

Select allOpen in new window



Examples of Usage


  • You want to print all messages to the default printer.
    MessageAndAttachmentProcessor Item, True

  • You want to print all messages to a non-default printer named "HP Deskjet 3320".
    MessageAndAttachmentProcessor Item, True, , , ,,,,,"HP Deskjet 3320"

  • You want to save all attachments to a folder on your C: drive named "Project X".
    MessageAndAttachmentProcessor Item,,,,True,,"C:\Project X"

  • You want to remove all PDF attachments to a folder on your C: drive named "Accounting" and insert a hyperlink to the saved attachment.
    MessageAndAttachmentProcessor Item,,,,True,True,"pdf","C:\Accounting"

  • You want to print all Word documents to the default printer.
    MessageAndAttachmentProcessor Item,,,True,,"doc,docx"

3

Configure Security

  a.  Click Tools > Trust Center.
  b.  Click Macro Security.
  c.  Set Macro Security to Warnings for all macros.
  d.  Click OK.
  e.  Close Outlook.
  f.  Start Outlook.


4

Create a Rule that Triggers the Subroutine

  a.  Click Tools > Rules and Alerts.
  b.  Click the New Rule button.
  c.  Under "Start from a blank rule" select Check messages when they arrive.
  d.  Click the Next button.
  e.  Select a condition for the messages you want to process.  If you want the macro to run against
       all messages, then don’t select a condition.  Outlook will display a dialog-box warning  you
       that "This rule will be applied to every message you receive.  Is this correct?".   Click Yes.
  f.  Click the Next button.
  g.  Place a check in the box next to run a script.
  h.  In the lower pane click the underlined a script.  Select your subroutine as the script to run.
  i.   Work your way through the rest of the Rules Wizard.



Links to Other BlueDevilFan Articles

1. Creating Linked Notes in Outlook 2007
2. Extending Outlook Rules via Scripting
3. Importing and Exporting Outlook 2007 Categories
4. Outlook 2007 Corporate Categories System
5. Avoiding Blank Subject Lines in Outlook
6. Never Again Forget to Add that Attachment to your Outlook Email
7. Enhancing Outlook 2007 Meeting Reminders
Asked On
2010-04-22 at 05:14:56ID2924
Tags

Outlook

,

macro

,

message

,

attachment

,

print

,

save

Topic

Outlook Groupware Software

Views
10984

Comments

Expert Comment

by: bromy2004 on 2010-04-24 at 18:01:53ID: 13767

Its got my Yes.

one point,

P3.  Print the attachments.
Tells the macro to print the attachments.  Valid values are True or False.

P4.  Save the attachments.
Tells the macro to print the attachments.  Valid values are True or False.

i think P4's Comment is supposed to say Save

Author Comment

by: BlueDevilFan on 2010-04-24 at 18:11:01ID: 13768

@bromy2004:  Oops!  Thanks for pointing that out.  I'll fix it.

Expert Comment

by: dbishop1234 on 2010-06-15 at 10:46:39ID: 15782

For a newbie - have you already answered questions about printing to either specific printer. I tried the code above, but got lost in the process. Thanks

Author Comment

by: BlueDevilFan on 2010-06-15 at 13:39:19ID: 15792

Hi, dbishop1234.

I'm not sure what you mean by "already answered questions".  I haven't been asked any questions about printing to a specific printer.  Do you have a question about that?  Where did you get lost at in the process?

Expert Comment

by: hjvesch on 2010-10-20 at 06:34:25ID: 20645

Hi BlueDevilFan,

I don't know what I am doing wrong but I keep errors using your scripts. I followed your guide, but when I execute the macro the line Sub Test(Item As Outlook MailItem)
and I receive an error User defined type not defined.

Thanks, hein

Author Comment

by: BlueDevilFan on 2010-10-21 at 02:53:04ID: 20667

Hi, hjvesch.

Can you post your code so I can see what you're doing?

Expert Comment

by: hjvesch on 2010-10-21 at 02:58:31ID: 20668

Hi Hi BlueDevilFan,

I attached my code.

Thank you in advance for your feedback.

Hein
Public Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" _
        (ByVal lpAppName As String, ByVal lpKeyName As String, _
        ByVal lpDefault As String, ByVal lpReturnedString As String, _
        ByVal nSize As Long) As Long

Private Declare Function ShellExecute Lib "shell32.dll" _
  Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
  ByVal lpFile As String, ByVal lpParameters As String, _
  ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub MessageAndAttachmentProcessor(Item As Outlook.MailItem, _
    Optional bolPrintMsg As Boolean, _
    Optional bolSaveMsg As Boolean, _
    Optional bolPrintAtt As Boolean, _
    Optional bolSaveAtt As Boolean, _
    Optional bolInsertLink As Boolean, _
    Optional strAttFileTypes As String, _
    Optional strFolderPath As String, _
    Optional varMsgFormat As OlkSaveAsType, _
    Optional strPrinter As String)
   
    Dim olkAttachment As Outlook.Attachment, _
        objFSO As FileSystemObject, _
        strMyPath As String, _
        strExtension As String, _
        strFileName As String, _
        strOriginalPrinter As String, _
        strLinkText As String, _
        strRootFolder As String, _
        strTempFolder As String, _
        varFileType As Variant, _
        intCount As Integer, _
        intIndex As Integer, _
        arrFileTypes As Variant

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    strTempFolder = Environ("TEMP") & "\"
    
    If strAttFileTypes = "" Then
        arrFileTypes = Array("*")
    Else
        arrFileTypes = Split(strAttFileTypes, ",")
    End If
    
    If bolPrintMsg Or bolPrintAtt Then
        If strPrinter <> "" Then
            strOriginalPrinter = GetDefaultPrinter()
            SetDefaultPrinter strPrinter
        End If
    End If
    
    If bolSaveMsg Or bolSaveAtt Then
        If strFolderPath = "" Then
            strRootFolder = Environ("USERPROFILE") & "\My Documents\"
        Else
            strRootFolder = strFolderPath & IIf(Right(strFolderPath, 1) = "\", "", "\")
        End If
    End If
    
    If bolSaveMsg Then
        Select Case varMsgFormat
            Case olHTML
                strExtension = ".html"
            Case olMSG
                strExtension = ".msg"
            Case olRTF
                strExtension = ".rtf"
            Case olDoc
                strExtension = ".doc"
            Case olTXT
                strExtension = ".txt"
            Case Else
                strExtension = ".msg"
        End Select
        Item.SaveAs strRootFolder & RemoveIllegalCharacters(Item.Subject) & strExtension, IIf(varMsgFormat <> 0, varMsgFormat, olMSG)
    End If
        
    For intIndex = Item.Attachments.Count To 1 Step -1
        Set olkAttachment = Item.Attachments.Item(intIndex)
        'Print the attachments if requested'
        If bolPrintAtt Then
            If olkAttachment.Type <> olEmbeddeditem Then
                For Each strFileType In arrFileTypes
                    If (strFileType = "*") Or (LCase(objFSO.GetExtensionName(olkAttachment.FileName)) = LCase(strFileType)) Then
                        olkAttachment.SaveAsFile strTempFolder & olkAttachment.FileName
                        ShellExecute 0&, "print", strTempFolder & olkAttachment.FileName, 0&, 0&, 0&
                    End If
                Next
            End If
        End If
        'Save the attachments if requested'
        If bolSaveAtt Then
            strFileName = olkAttachment.FileName
            intCount = 0
            Do While True
                strMyPath = strRootFolder & strFileName
                If objFSO.FileExists(strMyPath) Then
                    intCount = intCount + 1
                    strFileName = "Copy (" & intCount & ") of " & olkAttachment.FileName
                Else
                    Exit Do
                End If
            Loop
            olkAttachment.SaveAsFile strMyPath
            If bolInsertLink Then
                If Item.BodyFormat = olFormatHTML Then
                    strLinkText = strLinkText & "<a href=""file://" & strMyPath & """>" & olkAttachment.FileName & "</a><br>"
                Else
                    strLinkText = strLinkText & strMyPath & vbCrLf
                End If
                olkAttachment.Delete
            End If
        End If
    Next
    
    If bolPrintMsg Then
        Item.PrintOut
    End If
    
    If bolPrintMsg Or bolPrintAtt Then
        If strOriginalPrinter <> "" Then
            SetDefaultPrinter strOriginalPrinter
        End If
    End If
    
    If bolInsertLink Then
        If Item.BodyFormat = olFormatHTML Then
            Item.HTMLBody = Item.HTMLBody & "<br><br>Removed Attachments<br><br>" & strLinkText
        Else
            Item.Body = Item.Body & vbCrLf & vbCrLf & "Removed Attachments" & vbCrLf & vbCrLf & strLinkText
        End If
        Item.Save
    End If

    Set objFSO = Nothing
    Set olkAttachment = Nothing
End Sub

Function GetDefaultPrinter() As String
    Dim strPrinter As String, _
        intReturn As Integer
    strPrinter = Space(255)
    intReturn = GetProfileString("Windows", ByVal "device", "", strPrinter, Len(strPrinter))
    If intReturn Then
        strPrinter = UCase(Left(strPrinter, InStr(strPrinter, ",") - 1))
    End If
    GetDefaultPrinter = strPrinter
End Function

Function RemoveIllegalCharacters(strValue As String) As String
    ' Purpose: Remove characters that cannot be in a filename from a string.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    RemoveIllegalCharacters = strValue
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "<", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ">", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ":", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, Chr(34), "'")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "/", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "\", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "|", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "?", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "*", "")
End Function

Sub SetDefaultPrinter(strPrinterName As String)
    Dim objNet As Object
    Set objNet = CreateObject("Wscript.Network")
    objNet.SetDefaultPrinter strPrinterName
    Set objNet = Nothing
End Sub

                                        
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:

Select allOpen in new window

Author Comment

by: BlueDevilFan on 2010-10-22 at 06:51:58ID: 20707

That's the article code.  Where's the code you wrote that calls the article code?

Expert Comment

by: hjvesch on 2010-10-27 at 05:38:39ID: 20877

Please find attached the code to calls the article code.
Sub opslaanmail(Item As Outlook MailItem)
        MessageAndAttachmentProcessor Item, True
End Sub

                                        
1:
2:
3:

Select allOpen in new window

Author Comment

by: BlueDevilFan on 2010-10-27 at 05:49:29ID: 20878

This line

Sub opslaanmail(Item As Outlook MailItem)

is the problem.  It should be

Sub opslaanmail(Item As Outlook.MailItem)

Expert Comment

by: hjvesch on 2010-10-27 at 06:07:18ID: 20880

Thank you for your prompt reply.

I made the adjustment and when I try to execute the opslaanmail macro, outlook starts visual basic with this error message: compile error user-defined type not defined and lines 11 till 20 of the first script are marked.

Author Comment

by: BlueDevilFan on 2010-10-27 at 06:15:35ID: 20881

What version of Outlook are you using?

Expert Comment

by: hjvesch on 2010-10-27 at 06:23:51ID: 20882

I'm using outlook 2007 (dutch version)

I already checked the references (see image)
references.JPG
  • 40 KB
  • reference
reference

    Author Comment

    by: BlueDevilFan on 2010-10-27 at 06:31:04ID: 20883

    This line

        Optional varMsgFormat As OlkSaveAsType, _

    should read

        Optional varMsgFormat As OlSaveAsType, _
     

    Expert Comment

    by: hjvesch on 2010-10-27 at 07:55:11ID: 20887

    Hi BlueDevilFan,

    I made the adjustment and now I get an compile error ser-defined type not defined and this line is marked
    , _
            objFSO As FileSystemObject

    I can not find the error.

    Thanks, Hein

    Author Comment

    by: BlueDevilFan on 2010-10-27 at 07:59:12ID: 20888

    Change FileSystemObject to Object.

    Expert Comment

    by: hjvesch on 2010-10-27 at 08:06:26ID: 20889

    It works! thanks

    Author Comment

    by: BlueDevilFan on 2010-10-27 at 08:07:40ID: 20890

    You're welcome.

    Expert Comment

    by: fl160 on 2011-03-04 at 18:09:59ID: 24222

    I am a novice in programming and am in urgent need of sequentially printing 900+ Outlook 2007 emails in a specific folder. I also need each email's attachments (including Word, Excel, Tiff, JPG, etc.) to print after  each email.  I have Acrobat Pro and want to print the emails to PDF. I can set the default printer to PDF Printer. I have tried but was unable to change to code for these purposes.  I would very much appreciate if you can modify this code to fit my needs and email it to me or post it here.  If possible, it would be nice for the code to name the output files sequentially as 1.pdf, 2.pdf, etc.

    Thank you very much.

    Author Comment

    by: BlueDevilFan on 2011-03-04 at 18:18:30ID: 24223

    Hi, fl160.

    Would you mind opening a question for this?  This article really isn't the place to address this.

    Expert Comment

    by: phl1331 on 2011-03-21 at 12:20:34ID: 24887

    Blue Devil,
    This is exactly what I was looking for...however, I have never created a macro for outlook just excel.  I must be missing just one thing.  I added the code you had in the original article with module 1 as the article code and module 2 as the parameter code.  I then modified it for what I wanted it to do: print attachments (jpg, jpeg, png) to a specific printer (HP Photosmart D110 series).  Then I set the rule to do this only when coming from a specific email address and run this macro.

    Nothing happened.  So I changed it to the most simple code...I copied hjvesch's parameter code and article code just to see if it was my code or something else, then i removed the rule that said a specific person and have it running the macro from all emails...still nothing.

    I even tried putting the parameter code above the article code, as you said it could be in the same module, but it just created a bunch of different macros.

    Thanks for the help.  Here is my code.
    Sub PrintAttach(Item As Outlook.MailItem)
            MessageAndAttachmentProcessor Item, , , True, , , "png,jpg,jpeg,pdf", , , "HP Photosmart D110 series"
    End Sub
                                            
    1:
    2:
    3:
    

    Select allOpen in new window

    Public Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" _
            (ByVal lpAppName As String, ByVal lpKeyName As String, _
            ByVal lpDefault As String, ByVal lpReturnedString As String, _
            ByVal nSize As Long) As Long
     
    Private Declare Function ShellExecute Lib "shell32.dll" _
      Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
      ByVal lpFile As String, ByVal lpParameters As String, _
      ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
     
    Sub MessageAndAttachmentProcessor(Item As Outlook.MailItem, _
        Optional bolPrintMsg As Boolean, _
        Optional bolSaveMsg As Boolean, _
        Optional bolPrintAtt As Boolean, _
        Optional bolSaveAtt As Boolean, _
        Optional bolInsertLink As Boolean, _
        Optional strAttFileTypes As String, _
        Optional strFolderPath As String, _
        Optional varMsgFormat As OlSaveAsType, _
        Optional strPrinter As String)
        
        Dim olkAttachment As Outlook.Attachment, _
            objFSO As Object, _
            strMyPath As String, _
            strExtension As String, _
            strFileName As String, _
            strOriginalPrinter As String, _
            strLinkText As String, _
            strRootFolder As String, _
            strTempFolder As String, _
            varFileType As Variant, _
            intCount As Integer, _
            intIndex As Integer, _
            arrFileTypes As Variant
     
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        strTempFolder = Environ("TEMP") & "\"
         
        If strAttFileTypes = "" Then
            arrFileTypes = Array("*")
        Else
            arrFileTypes = Split(strAttFileTypes, ",")
        End If
         
        If bolPrintMsg Or bolPrintAtt Then
            If strPrinter <> "" Then
                strOriginalPrinter = GetDefaultPrinter()
                SetDefaultPrinter strPrinter
            End If
        End If
         
        If bolSaveMsg Or bolSaveAtt Then
            If strFolderPath = "" Then
                strRootFolder = Environ("USERPROFILE") & "\My Documents\"
            Else
                strRootFolder = strFolderPath & IIf(Right(strFolderPath, 1) = "\", "", "\")
            End If
        End If
         
        If bolSaveMsg Then
            Select Case varMsgFormat
                Case olHTML
                    strExtension = ".html"
                Case olMSG
                    strExtension = ".msg"
                Case olRTF
                    strExtension = ".rtf"
                Case olDoc
                    strExtension = ".doc"
                Case olTXT
                    strExtension = ".txt"
                Case Else
                    strExtension = ".msg"
            End Select
            Item.SaveAs strRootFolder & RemoveIllegalCharacters(Item.Subject) & strExtension, IIf(varMsgFormat <> 0, varMsgFormat, olMSG)
        End If
             
        For intIndex = Item.Attachments.Count To 1 Step -1
            Set olkAttachment = Item.Attachments.Item(intIndex)
            'Print the attachments if requested'
            If bolPrintAtt Then
                If olkAttachment.Type <> olEmbeddeditem Then
                    For Each strFileType In arrFileTypes
                        If (strFileType = "*") Or (LCase(objFSO.GetExtensionName(olkAttachment.FileName)) = LCase(strFileType)) Then
                            olkAttachment.SaveAsFile strTempFolder & olkAttachment.FileName
                            ShellExecute 0&, "print", strTempFolder & olkAttachment.FileName, 0&, 0&, 0&
                        End If
                    Next
                End If
            End If
            'Save the attachments if requested'
            If bolSaveAtt Then
                strFileName = olkAttachment.FileName
                intCount = 0
                Do While True
                    strMyPath = strRootFolder & strFileName
                    If objFSO.FileExists(strMyPath) Then
                        intCount = intCount + 1
                        strFileName = "Copy (" & intCount & ") of " & olkAttachment.FileName
                    Else
                        Exit Do
                    End If
                Loop
                olkAttachment.SaveAsFile strMyPath
                If bolInsertLink Then
                    If Item.BodyFormat = olFormatHTML Then
                        strLinkText = strLinkText & "<a href=""file://" & strMyPath & """>" & olkAttachment.FileName & "</a><br>"
                    Else
                        strLinkText = strLinkText & strMyPath & vbCrLf
                    End If
                    olkAttachment.Delete
                End If
            End If
        Next
         
        If bolPrintMsg Then
            Item.PrintOut
        End If
         
        If bolPrintMsg Or bolPrintAtt Then
            If strOriginalPrinter <> "" Then
                SetDefaultPrinter strOriginalPrinter
            End If
        End If
         
        If bolInsertLink Then
            If Item.BodyFormat = olFormatHTML Then
                Item.HTMLBody = Item.HTMLBody & "<br><br>Removed Attachments<br><br>" & strLinkText
            Else
                Item.Body = Item.Body & vbCrLf & vbCrLf & "Removed Attachments" & vbCrLf & vbCrLf & strLinkText
            End If
            Item.Save
        End If
     
        Set objFSO = Nothing
        Set olkAttachment = Nothing
    End Sub
     
    Function GetDefaultPrinter() As String
        Dim strPrinter As String, _
            intReturn As Integer
        strPrinter = Space(255)
        intReturn = GetProfileString("Windows", ByVal "device", "", strPrinter, Len(strPrinter))
        If intReturn Then
            strPrinter = UCase(Left(strPrinter, InStr(strPrinter, ",") - 1))
        End If
        GetDefaultPrinter = strPrinter
    End Function
     
    Function RemoveIllegalCharacters(strValue As String) As String
        ' Purpose: Remove characters that cannot be in a filename from a string.'
        ' Written: 4/24/2009'
        ' Author:  BlueDevilFan'
        ' Outlook: All versions'
        RemoveIllegalCharacters = strValue
        RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "<", "")
        RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ">", "")
        RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ":", "")
        RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, Chr(34), "'")
        RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "/", "")
        RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "\", "")
        RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "|", "")
        RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "?", "")
        RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "*", "")
    End Function
     
    Sub SetDefaultPrinter(strPrinterName As String)
        Dim objNet As Object
        Set objNet = CreateObject("Wscript.Network")
        objNet.SetDefaultPrinter strPrinterName
        Set objNet = Nothing
    End Sub
                                            
    1:
    2:
    3:
    4:
    5:
    6:
    7:
    8:
    9:
    10:
    11:
    12:
    13:
    14:
    15:
    16:
    17:
    18:
    19:
    20:
    21:
    22:
    23:
    24:
    25:
    26:
    27:
    28:
    29:
    30:
    31:
    32:
    33:
    34:
    35:
    36:
    37:
    38:
    39:
    40:
    41:
    42:
    43:
    44:
    45:
    46:
    47:
    48:
    49:
    50:
    51:
    52:
    53:
    54:
    55:
    56:
    57:
    58:
    59:
    60:
    61:
    62:
    63:
    64:
    65:
    66:
    67:
    68:
    69:
    70:
    71:
    72:
    73:
    74:
    75:
    76:
    77:
    78:
    79:
    80:
    81:
    82:
    83:
    84:
    85:
    86:
    87:
    88:
    89:
    90:
    91:
    92:
    93:
    94:
    95:
    96:
    97:
    98:
    99:
    100:
    101:
    102:
    103:
    104:
    105:
    106:
    107:
    108:
    109:
    110:
    111:
    112:
    113:
    114:
    115:
    116:
    117:
    118:
    119:
    120:
    121:
    122:
    123:
    124:
    125:
    126:
    127:
    128:
    129:
    130:
    131:
    132:
    133:
    134:
    135:
    136:
    137:
    138:
    139:
    140:
    141:
    142:
    143:
    144:
    145:
    146:
    147:
    148:
    149:
    150:
    151:
    152:
    153:
    154:
    155:
    156:
    157:
    158:
    159:
    160:
    161:
    162:
    163:
    164:
    165:
    166:
    167:
    168:
    169:
    170:
    171:
    172:
    

    Select allOpen in new window

    Author Comment

    by: BlueDevilFan on 2011-03-21 at 12:30:00ID: 24888

    What version of Outlook are you using?  Are you able to run any macros?

    Expert Comment

    by: phl1331 on 2011-03-21 at 12:32:25ID: 24889

    Outlook 2007...whats a simple test to try a macro?

    Expert Comment

    by: phl1331 on 2011-03-21 at 12:35:07ID: 24890

    when i set up rules and select run a script it shows me the one option Project1.PrintAttach
    However, when I go Tools>Macro>Macros, it shows none listed

    Author Comment

    by: BlueDevilFan on 2011-03-21 at 12:35:56ID: 24891

    Add this code to that you already have.  Then try and run it by clicking Tools > Macro > Macros and selecting this macro by name.  If it works a dialog-box will pop up telling you that macros are enabled.
    Sub TestMacro()
        MsgBox "Macros are enabled."
    End Sub
                                            
    1:
    2:
    3:
    

    Select allOpen in new window

    Expert Comment

    by: phl1331 on 2011-03-21 at 12:39:00ID: 24892

    That worked and it brought me back to Msft Visual Edit...and said Macros in this project are disabled.

    Author Comment

    by: BlueDevilFan on 2011-03-21 at 12:42:40ID: 24894

    Ahh, so macros are disabled.  Add the code below to ThisOutlookSession, then close and restart Outlook.  WHen it restarts you should be asked if you want to enable macros.  Say yes.  You should then see a dialog saying that macros are enabled.  If so, then you should be ready to run the code from this article.
    Private Sub Application_Startup()
        MsgBox "Macros are enabled."
    End Sub
                                            
    1:
    2:
    3:
    

    Select allOpen in new window

    Expert Comment

    by: phl1331 on 2011-03-21 at 12:50:52ID: 24896

    Not getting any question about wanting to enable macros...let me make sure i understood you.

    I already had module 1 and module 2.  I created module 3 that has this in it
    "Sub TestMacro()
        MsgBox "Macros are enabled."
    End Sub"

    Then i expanded MOO where it shows ThisOutlookSession and in it I put
    "Private Sub Application_Startup()
        MsgBox "Macros are enabled."
    End Sub"

    I saved it closed it, closed outlook and reopened, it opened normal.

    Author Comment

    by: BlueDevilFan on 2011-03-21 at 12:54:07ID: 24897

    Did you follow the article instructions on configuring security?

    Expert Comment

    by: phl1331 on 2011-03-21 at 12:57:15ID: 24898

    Ah yes, after that last response I just reread it and remembered what happened at that step.

    My MacroSecurity settings are disabled (by company IT)...I cannot change and it currently is set at "Warnings for signed macros, unsigned disabled"

    Is there a way to sign this macro?

    Ill try contacting them to change my settings...

    Thanks for your help...ill let you know if I have other questions.

    Author Comment

    by: BlueDevilFan on 2011-03-21 at 13:08:23ID: 24901

    Yes, you can sign the macro.  Here's a Microsoft page with instructions on how to do that.  The instructions are for Outlook 2000, but the process is the same in 2007.

    http://msdn.microsoft.com/en-us/library/aa155754(office.10).aspx

    Expert Comment

    by: Ksquared_au on 2011-03-22 at 17:51:24ID: 24962

    This is a fine piece of work.
    Wondering if there was a way to identify those that HAVNT been printed. eg mark the ones printed as read, making the other in need of manual intervention, or moving the printed ones to a 'Done' folder.

    Great work BDF !

    Author Comment

    by: BlueDevilFan on 2011-03-23 at 02:19:52ID: 24985

    Thanks, Ksquared_au!

    Yes, the code can be modified to mark the processed items read or to move them to a folder.  Let me know which you prefer and I'll explain how to make the change to accomplish that.

    Expert Comment

    by: Ksquared_au on 2011-03-23 at 02:35:04ID: 24986

    Would be sweet if we could do both!
    Cheers

    Expert Comment

    by: phl1331 on 2011-03-25 at 06:57:32ID: 25112

    BlueDevilFan,

    Sorry about the game last night.

    Okay, so I can't run macros, can't selfcert, cant do anything--its all on lockdown. However, I can make a rule to say "run Script". Is there a way to make a script that says print all attachments with .jpg or .png to non-default printer?

    Author Comment

    by: BlueDevilFan on 2011-03-25 at 08:06:53ID: 25114

    Thanks.  Me too.  :-(

    If you can't enable macros, then I'm afraid this isn't going to work.  A script is a macro and as you've already determined it isn't able to run.  The only solution I can think of would be to re-write this as an external program that interfaces with Outlook.  

    Expert Comment

    by: Ksquared_au on 2011-03-27 at 15:33:43ID: 25139

    Sorry to nag BDF......got some news for me??

    Author Comment

    by: BlueDevilFan on 2011-03-28 at 12:21:29ID: 25173

    Ksquared_au,

    Thanks for the nag.  I have a bad memory and this had slipped my mind.  Replace the main routine (i.e. MessageAndAttachmentProcessor) with the version below and add the other sub (i.e. OpenOutlookFolder) to the code you already have.  There's a comment line toward the bottom of MessageAndAttachmentProcessor that denotes the two lines of additional code required to do this.  As noted there you have to insert the path to the target folder.  In case you aren't familiar with folder paths in Outlook, here's an explanation of how they work.

    A folder path in Outlook is essentially the same as a folder path in the file system.  The one difference being that Outlook folder paths do not include a drive letter.  The path to a folder is a list of all the folders from the root to the target folder with each folder name separated from the preceding folder name by a backslash (i.e. \).  Consider the following folder structure:

    Mailbox - Doe, John
        - Calendar
        - Inbox
        - Tasks
    Personal Folders
        + Marketing
            + Proposals
            + Reviews
        + Projects
            + Project 1
            + Project 2

    The path to "Inbox" is "Mailbox - Doe, John\Inbox".
    The path to "Reviews" is "Personal Folders\Marketing\Reviews".

    The path to "Project 1" is "Personal Folders\Projects\Project 1".

    Sub MessageAndAttachmentProcessor(Item As Outlook.MailItem, _
        Optional bolPrintMsg As Boolean, _
        Optional bolSaveMsg As Boolean, _
        Optional bolPrintAtt As Boolean, _
        Optional bolSaveAtt As Boolean, _
        Optional bolInsertLink As Boolean, _
        Optional strAttFileTypes As String, _
        Optional strFolderPath As String, _
        Optional varMsgFormat As OlSaveAsType, _
        Optional strPrinter As String)
       
        Dim olkAttachment As Outlook.Attachment, _
            objFSO As FileSystemObject, _
            strMyPath As String, _
            strExtension As String, _
            strFileName As String, _
            strOriginalPrinter As String, _
            strLinkText As String, _
            strRootFolder As String, _
            strTempFolder As String, _
            varFileType As Variant, _
            intCount As Integer, _
            intIndex As Integer, _
            arrFileTypes As Variant
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        strTempFolder = Environ("TEMP") & "\"
        
        If strAttFileTypes = "" Then
            arrFileTypes = Array("*")
        Else
            arrFileTypes = Split(strAttFileTypes, ",")
        End If
        
        If bolPrintMsg Or bolPrintAtt Then
            If strPrinter <> "" Then
                strOriginalPrinter = GetDefaultPrinter()
                SetDefaultPrinter strPrinter
            End If
        End If
        
        If bolSaveMsg Or bolSaveAtt Then
            If strFolderPath = "" Then
                strRootFolder = Environ("USERPROFILE") & "\My Documents\"
            Else
                strRootFolder = strFolderPath & IIf(Right(strFolderPath, 1) = "\", "", "\")
            End If
        End If
        
        If bolSaveMsg Then
            Select Case varMsgFormat
                Case olHTML
                    strExtension = ".html"
                Case olMSG
                    strExtension = ".msg"
                Case olRTF
                    strExtension = ".rtf"
                Case olDoc
                    strExtension = ".doc"
                Case olTXT
                    strExtension = ".txt"
                Case Else
                    strExtension = ".msg"
            End Select
            Item.SaveAs strRootFolder & RemoveIllegalCharacters(Item.Subject) & strExtension, IIf(varMsgFormat <> 0, varMsgFormat, olMSG)
        End If
            
        For intIndex = Item.Attachments.count To 1 Step -1
            Set olkAttachment = Item.Attachments.Item(intIndex)
            'Print the attachments if requested'
            If bolPrintAtt Then
                If olkAttachment.Type <> olEmbeddeditem Then
                    For Each strFileType In arrFileTypes
                        If (strFileType = "*") Or (LCase(objFSO.GetExtensionName(olkAttachment.FileName)) = LCase(strFileType)) Then
                            olkAttachment.SaveAsFile strTempFolder & olkAttachment.FileName
                            ShellExecute 0&, "print", strTempFolder & olkAttachment.FileName, 0&, 0&, 0&
                        End If
                    Next
                End If
            End If
            'Save the attachments if requested'
            If bolSaveAtt Then
                strFileName = olkAttachment.FileName
                intCount = 0
                Do While True
                    strMyPath = strRootFolder & strFileName
                    If objFSO.FileExists(strMyPath) Then
                        intCount = intCount + 1
                        strFileName = "Copy (" & intCount & ") of " & olkAttachment.FileName
                    Else
                        Exit Do
                    End If
                Loop
                olkAttachment.SaveAsFile strMyPath
                If bolInsertLink Then
                    If Item.BodyFormat = olFormatHTML Then
                        strLinkText = strLinkText & "<a href=""file://" & strMyPath & """>" & olkAttachment.FileName & "</a><br>"
                    Else
                        strLinkText = strLinkText & strMyPath & vbCrLf
                    End If
                    olkAttachment.Delete
                End If
            End If
        Next
        
        If bolPrintMsg Then
            Item.PrintOut
        End If
        
        If bolPrintMsg Or bolPrintAtt Then
            If strOriginalPrinter <> "" Then
                SetDefaultPrinter strOriginalPrinter
            End If
        End If
        
        If bolInsertLink Then
            If Item.BodyFormat = olFormatHTML Then
                Item.HTMLBody = Item.HTMLBody & "<br><br>Removed Attachments<br><br>" & strLinkText
            Else
                Item.Body = Item.Body & vbCrLf & vbCrLf & "Removed Attachments" & vbCrLf & vbCrLf & strLinkText
            End If
            Item.Save
        End If
    
        'Addition for Ksquared_au'
        Item.UnRead = False
        'You must edit the path on the next line'
        Item.Move OpenOutlookFolder("Path_to_the_target_folder")
    
        Set objFSO = Nothing
        Set olkAttachment = Nothing
    End Sub
    
    
    Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
        ' Purpose: Opens an Outlook folder from a folder path.'
        ' Written: 4/24/2009'
        ' Author:  BlueDevilFan'
        ' Outlook: All versions'
        Dim arrFolders As Variant, _
            varFolder As Variant, _
            bolBeyondRoot As Boolean
        On Error Resume Next
        If strFolderPath = "" Then
            Set OpenOutlookFolder = Nothing
        Else
            Do While Left(strFolderPath, 1) = "\"
                strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
            Loop
            arrFolders = Split(strFolderPath, "\")
            For Each varFolder In arrFolders
                Select Case bolBeyondRoot
                    Case False
                        Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                        bolBeyondRoot = True
                    Case True
                        Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
                End Select
                If Err.Number <> 0 Then
                    Set OpenOutlookFolder = Nothing
                    Exit For
                End If
            Next
        End If
        On Error GoTo 0
    End Function
                                            
    1:
    2:
    3:
    4:
    5:
    6:
    7:
    8:
    9:
    10:
    11:
    12:
    13:
    14:
    15:
    16:
    17:
    18:
    19:
    20:
    21:
    22:
    23:
    24:
    25:
    26:
    27:
    28:
    29:
    30:
    31:
    32:
    33:
    34:
    35:
    36:
    37:
    38:
    39:
    40:
    41:
    42:
    43:
    44:
    45:
    46:
    47:
    48:
    49:
    50:
    51:
    52:
    53:
    54:
    55:
    56:
    57:
    58:
    59:
    60:
    61:
    62:
    63:
    64:
    65:
    66:
    67:
    68:
    69:
    70:
    71:
    72:
    73:
    74:
    75:
    76:
    77:
    78:
    79:
    80:
    81:
    82:
    83:
    84:
    85:
    86:
    87:
    88:
    89:
    90:
    91:
    92:
    93:
    94:
    95:
    96:
    97:
    98:
    99:
    100:
    101:
    102:
    103:
    104:
    105:
    106:
    107:
    108:
    109:
    110:
    111:
    112:
    113:
    114:
    115:
    116:
    117:
    118:
    119:
    120:
    121:
    122:
    123:
    124:
    125:
    126:
    127:
    128:
    129:
    130:
    131:
    132:
    133:
    134:
    135:
    136:
    137:
    138:
    139:
    140:
    141:
    142:
    143:
    144:
    145:
    146:
    147:
    148:
    149:
    150:
    151:
    152:
    153:
    154:
    155:
    156:
    157:
    158:
    159:
    160:
    161:
    162:
    163:
    164:
    165:
    166:
    

    Select allOpen in new window

    Expert Comment

    by: phl1331 on 2011-03-28 at 15:21:46ID: 25176

    Blue Devil fan, the options in form of an msi add-in is very limited..

    is there a way to convert this to an add-in for outlook? Im guessing it would have to be changed to an exe then to an msi...any direction as to someone who could do this for me?

    Expert Comment

    by: Ksquared_au on 2011-03-28 at 15:43:19ID: 25177

    Once again BDF...you are the man! Respect!

    Author Comment

    by: BlueDevilFan on 2011-03-28 at 18:50:00ID: 25188

    phl1331,

    An MSI is an installer, not an executable.  Outlook add-ins aren't executables either.  Or at least not when built as a COM add-in (perhaps those written newer tools are).  They are DLLs.  

    Yes, this could be re-written as an add-in.  Before thinking about having this done, do the restrictions on your computer permit you to install an add-in?  I'm thinking that if your security/system admin folks have restricted the use of macros, then they are likely to have restricted the use of add-ins too.

    Author Comment

    by: BlueDevilFan on 2011-03-28 at 18:51:09ID: 25189

    Ksquared_au,

    Thanks and you're welcome.  If there's anything else I can do, you know where to find me.

    Expert Comment

    by: phl1331 on 2011-03-28 at 18:55:52ID: 25190

    Bluedevil, I have been able to install certain add-ins (ones with msi installer) such as blueprint and autoprint but the issue on those is that they open the image attachments but dont print....the open with microsoft picture viewer and can't print bc it tries to use the photo printer wizard.  microsoft picture viewer isn't even the default for photo editing but somehow it opens with it.  

    Long story short, yes i can add certain add-ins

    Expert Comment

    by: phl1331 on 2011-03-28 at 18:59:54ID: 25191

    however, i can't install mapilabs print tools add-in

    Author Comment

    by: BlueDevilFan on 2011-03-30 at 05:14:43ID: 25265

    The people I know that do development work do large scale projects.  I doubt that they'd be interested in something this size and if they were I expect the price would be high (one time development).  There are sites like RentACoder.com where you could possibly find someone to do this.  My concern there is that you'd pay to have something developed and then discover that you can't install it.  It's unclear why your system allows some add-ins to be installed but not others.

    Expert Comment

    by: jayelbird on 2012-03-29 at 08:24:07ID: 47810

    Do you know how to change the code to be able to save as a pdf for the message save format?

    Author Comment

    by: BlueDevilFan on 2012-04-04 at 03:10:56ID: 48759

    There are a couple of ways to do it, but neither of them are particularly good.  First, the code could be modified to save the messages to Word format, then open them in Word and save them to PDF.  Second, the code can be modified to change the default printer to "Adobe PDF" or a third-party PDF print driver followed by issuing the print command.  The problem with the former is that the process will be kind of slow.  The latter suffers from being prompted for a file name for each message printed.

    Expert Comment

    by: bdonmez on 2012-04-18 at 05:43:34ID: 50626

    Thanks for the article.
    How to print without bringing up the Printer Dialog for html attachments?

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top Outlook Experts

    1. apache09

      663,644

      Sage

      2,168 points yesterday

      Profile
      Rank: Genius
    2. alanhardisty

      170,946

      Guru

      0 points yesterday

      Profile
      Rank: Genius
    3. demazter

      131,854

      Master

      0 points yesterday

      Profile
      Rank: Genius
    4. chris_bottomley

      109,375

      Master

      2,800 points yesterday

      Profile
      Rank: Genius
    5. thinkpads_user

      95,624

      Master

      750 points yesterday

      Profile
      Rank: Genius
    6. Rajkumar-MCITP

      89,780

      Master

      0 points yesterday

      Profile
      Rank: Guru
    7. l33tf0b

      83,091

      Master

      0 points yesterday

      Profile
      Rank: Wizard
    8. BlueDevilFan

      73,191

      Master

      50 points yesterday

      Profile
      Rank: Savant
    9. jjmck

      66,336

      Master

      0 points yesterday

      Profile
      Rank: Genius
    10. Neilsr

      61,466

      Master

      0 points yesterday

      Profile
      Rank: Genius
    11. amitkulshrestha

      61,377

      Master

      0 points yesterday

      Profile
      Rank: Genius
    12. jcimarron

      49,232

      0 points yesterday

      Profile
      Rank: Genius
    13. ve3ofa

      46,002

      0 points yesterday

      Profile
      Rank: Genius
    14. dlmille

      45,200

      0 points yesterday

      Profile
      Rank: Genius
    15. akicute555

      44,979

      10 points yesterday

      Profile
      Rank: Wizard
    16. Anuroopsundd

      44,529

      0 points yesterday

      Profile
      Rank: Sage
    17. HendrikWiese

      40,896

      2,000 points yesterday

      Profile
      Rank: Sage
    18. Exchange_Geek

      37,449

      0 points yesterday

      Profile
      Rank: Sage
    19. jordannet

      36,757

      0 points yesterday

      Profile
      Rank: Wizard
    20. acbrown2010

      34,652

      0 points yesterday

      Profile
      Rank: Genius
    21. diverseit

      34,600

      0 points yesterday

      Profile
      Rank: Guru
    22. WORKS2011

      32,775

      0 points yesterday

      Profile
      Rank: Guru
    23. e_aravind

      31,941

      0 points yesterday

      Profile
      Rank: Genius
    24. JBlond

      31,700

      0 points yesterday

      Profile
      Rank: Sage
    25. limjianan

      30,910

      0 points yesterday

      Profile
      Rank: Genius

    Hall Of Fame