VBA Outlook 自动打印到电子邮件附件错误“438”:对象不支持此属性或方法

问题描述 投票:0回答:3

我希望您打印收到的电子邮件的附件。但它遇到了 438 错误 :( 可能出了什么问题?

代码:

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
vba printing outlook office-automation shellexecute
3个回答
0
投票

并非所有对象都支持所有属性和方法。此错误有以下原因及解决方法:

  • 您指定的方法或属性对此自动化对象不存在。有关对象的更多信息,请参阅对象的文档并检查属性和方法的拼写。

要找出导致问题的属性或方法,我建议删除

On Error GoTo OError
行。因此,您将能够运行代码并查看到底是哪一行导致了问题。


0
投票

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

0
投票

我重写了它并且有效:

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
© www.soinside.com 2019 - 2024. All rights reserved.