Djh006
Djh006

Reputation: 67

How to print invoices automatically when multiple attachments are named the same?

My macro prints invoices automatically but when multiple attachments are named the same, it prints only the first one multiple times.

Sub LSPrint(Item As Outlook.MailItem)
    On Error Resume Next
    Dim oFS As FileSystemObject
    Dim sTempFolder As String
    Set oFS = New FileSystemObject

    sTempFolder = oFS.GetSpecialFolder(TemporaryFolder)

    cTmpFld = sTempFolder & "\OETMP" & Format(Now, "yyyymmddhhmmss")
    MkDir (cTmpFld)

    Dim oAtt As Attachment
    For Each oAtt In Item.Attachments
    FileName = oAtt.FileName
    FullFile = cTmpFld & "\" & FileName

    oAtt.SaveAsFile (FullFile)

    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.NameSpace(0)
    Set objFolderItem = objFolder.ParseName(FullFile)
    objFolderItem.InvokeVerbEx ("print")

    Next oAtt

    If Not oFS Is Nothing Then Set oFS = Nothing
    If Not objFolder Is Nothing Then Set objFolder = Nothing
    If Not objFolderItem Is Nothing Then Set objFolderItem = Nothing
    If Not objShell Is Nothing Then Set objShell = Nothing
OError:
    If Err <> 0 Then
        MsgBox Err.Number & " - " & Err.Description
        Err.Clear
    End If
    Exit Sub
End Sub

How do I print multiple attachments if they are named the same?

Possibly renaming the files and then printing those.

Upvotes: 1

Views: 206

Answers (2)

0m3r
0m3r

Reputation: 12499

Try changing the following line

FileName = oAtt.FileName

To

FileName = oAtt.FileName & Format(Now, " yyyymmddhhmmssms")

Upvotes: 0

urdearboy
urdearboy

Reputation: 14590

As you stated, created a naming convention to force unique file names among the attachments. This is done here with variable i

Dim oAtt As Attachment
Dim i as Long: i = 1

For Each oAtt In Item.Attachments
    FileName = oAtt.FileName
    FullFile = cTmpFld & "\" & FileName & i  '<-- add unique identifier
    oAtt.SaveAsFile (FullFile)

    '.... More stuff

    i = i + 1  '<-- increment to next unique identifier
Next oAtt

Upvotes: 2

Related Questions