0

I want you to print the attachment of the incoming email. But it runs into an 438 error :( What could be wrong?

Code:

Sub AttachmentPrint(Item As Outlook.MailItem)
 
     On Error GoTo OError
     
     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
            FileType = LCase$(Right$(FileName, 4))
            FullFile = cTmpFld & "\" & FileName
             oAtt.SaveAsFile (FullFile)
             
             Select Case FileType
             Case ".doc", "docx", ".pdf", ".txt", ".jpg"
                 Set objShell = CreateObject("Shell.Application")
                 Set objFolder = objShell.NameSpace(0)
                 Set objFolderItem = objFolder.ParseName(FullFile)
                 objFolderItem.InvokeVebrEx ("Print")
                
             End Select
         
         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
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • What line throws the error? – BigBen Oct 20 '22 at 14:11
  • The way `On Error GoTo OError` is used here is for a non-programmer to advise the programmer. Since you are the programmer here remove `On Error GoTo OError` to see the line with the error. – niton Oct 20 '22 at 17:00
  • Would also recommend using `Option Explicit` in every module. There's typically no need in VBA to set object variables to `Nothing` - they will go out of scope when your code ends - https://stackoverflow.com/questions/19038350/when-should-an-excel-vba-variable-be-killed-or-set-to-nothing – Tim Williams Oct 20 '22 at 17:14

3 Answers3

0

Not all objects support all properties and methods. This error has the following cause and solution:

  • You specified a method or property that doesn't exist for this automation object. See the object's documentation for more information on the object and check the spellings of properties and methods.

To find out what property or method causes the issue I'd recommend removing the On Error GoTo OError line. So, you will be able to run the code and see what line exactly causes the problem.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

Typo in objFolderItem.InvokeVebrEx ("Print").

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Private Sub Test()
AttachmentPrint ActiveInspector.CurrentItem
End Sub

Sub AttachmentPrint(Item As MailItem)
    
    ' Reference Microsoft Scripting Runtime
    Dim oFS As FileSystemObject
    
    Dim sTempFolder As String
     
    Dim cTmpFld As String
    Dim fileName As String
    Dim FileType As String
    Dim FullFile As String
    
    Dim objShell As Object
    Dim objFolder As Object
    Dim objFolderItem As Object
     
    Set oFS = New FileSystemObject
    sTempFolder = oFS.GetSpecialFolder(TemporaryFolder)
    cTmpFld = sTempFolder & "\OETMP" & Format(Now, "yyyymmddhhmmss")
    
    ' You may delete this folder later
    Debug.Print cTmpFld
    
    MkDir cTmpFld
    
    Dim oAtt As Attachment
    
    For Each oAtt In Item.Attachments
    
        fileName = oAtt.fileName
        FileType = LCase$(Right$(fileName, 4))
        FullFile = cTmpFld & "\" & fileName
        oAtt.SaveAsFile FullFile
        
        Select Case FileType
        
        Case ".doc", "docx", ".pdf", ".txt", ".jpg"
            Set objShell = CreateObject("Shell.Application")
            Set objFolder = objShell.NameSpace(0)
            Set objFolderItem = objFolder.ParseName(FullFile)
            
            'objFolderItem.InvokeVebrEx ("Print")    ' <--- Typo ER438
            
            objFolderItem.InvokeVerbEx ("Print")
            
        End Select
        
    Next oAtt
    
    'https://stackoverflow.com/questions/19038350/when-should-an-excel-vba-variable-be-killed-or-set-to-nothing
    ' Not detrimental if memory is deallocated unnecessarily.
    ' You could decide to apply only when forced to do so.
    'Set oFS = Nothing
    'Set objFolder = Nothing
    'Set objFolderItem = Nothing
    'Set objShell = Nothing

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52
0

Thanks for the help, I rewrote it a bit and it works like this:

Sub Autoprint()


Dim objFileSystem As Object
Dim objSelection As Outlook.Selection
Dim objShell As Object
Dim objTempFolder As Object
Dim objTempFolderItem As Object
Dim oFS As FileSystemObject
Dim sTempFolder As String

 Set oFS = New FileSystemObject
 sTempFolder = "C:\temp"
 cTmpFld = sTempFolder & "\nyomtatas" & Format(Now, "yyyymmddhhmmss")
 MkDir (cTmpFld)
 
 Dim oAtt As Attachment
     For Each oAtt In Item.Attachments
        FileName = oAtt.FileName
        FileType = LCase$(Right$(FileName, 4))
        FullFile = cTmpFld & "\" & FileName
         oAtt.SaveAsFile (FullFile)
         
         Select Case FileType
         Case ".doc", "docx", ".pdf", ".txt", ".jpg"
               Set objShell = CreateObject("Shell.Application")
               Set objTempFolder = objShell.NameSpace(0)
               Set objTempFolderItem = objTempFolder.ParseName(FullFile)
               objTempFolderItem.InvokeVerbEx ("print")
           End Select

      Next oAtt
  End Sub