Home > Blockchain >  VBA Outlook automatically print to email attachment error '438': Object doesn't suppo
VBA Outlook automatically print to email attachment error '438': Object doesn't suppo

Time:10-22

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

CodePudding user response:

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.

CodePudding user response:

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
  • Related