1

I've found the following Outlook VBA script which batch prints all the email attachments in a sub-folder:

Public Sub PrintAttachments()
Dim Inbox As MAPIFolder
Dim Item As MailItem
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer

Set Inbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders.Item("Batch Prints")

For Each Item In Inbox.Items
    For Each Atmt In Item.Attachments
        ' all attachments are first saved in the temp folder C:\Temp.  Be sure to create this folder.
        FileName = "C:\Temp\" & Atmt.FileName
        Atmt.SaveAsFile FileName
        ' please change the program folder accordingly if the Acrobat Reader is not installed on drive C:
        Shell """C:\Program Files\Adobe\Reader 8.0\Reader\acrord32.exe"" /h /p """ + FileName + """", vbHide
    Next

    Item.Delete  'remove this line if you don't want the email to be deleted automatically
Next

Set Inbox = Nothing
End Sub

Source: http://www.howtogeek.com/howto/microsoft-office/batch-print-pdf-attachments-in-outlook/

My question is: Is it possible to convert this script to 64 bit as installing 32 bit Office is not an option.

I found PtrSafe but that only seems to be relevant when it comes to .dll declarations.

Office version: 2016 64 bit

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Spagett
  • 147
  • 4
  • 16
  • 1
    What happens when you run it within 64bit Outlook? In general vanilla VBA should be portable between the 32/64 versions. – Alex K. Aug 25 '16 at 10:42
  • Interesting. I get the following error: "Run-time error '-2147221233 (8004010f)': The attempted operation failed. An object could not be found. In the debugger it has highlighted: `Set Inbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders.Item("Batch Prints")`. I have verified that both the Batch Prints sub-folder exists as well as the Temp folder. – Spagett Aug 25 '16 at 10:50
  • 1
    Is "Batch Prints" at the same level as "Inbox"? You are starting with Inbox, then going up to the parent PST file then down to "Batch Prints". Is this correct? – Tony Dallimore Aug 25 '16 at 11:09
  • 1
    It is possible to go up and down the hierarchy using the Parent and Folders properties but this is not always convenient. At the very bottom of this answer [How to copy Outlook mail message into excel using VBA or Macros](http://stackoverflow.com/a/12146315/973283) is a routine that will return a reference to any folder in any accessible PST file. This might be more convenient. I wrote this routine years ago for Outlook 2003 (pre-32-bit?) and it still works on my current 64-bit, Windows 10 laptop with Outlook 365. – Tony Dallimore Aug 25 '16 at 11:23
  • 1
    Thanks Tony, that question alone solved my issue. "Batch Prints" was created as a sub-folder in the inbox and not besides inbox. I changed the code to `Set Inbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Batch Prints")` and it's now working correctly. Thanks! – Spagett Aug 25 '16 at 12:25

1 Answers1

0

Yes if your folder is subfolder then you set your code to

Set Inbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders.("Batch Prints")

If it's multiple subfolder then

Folders.("Folder name").Folders.("Batch Prints")

To use the macro with 64-bit Outlook, you need to add PtrSafe to Declare:

Compatibility Between the 32-bit and 64-bit Versions of Office 2010

0m3r
  • 12,286
  • 15
  • 35
  • 71