Reputation: 1
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
Upvotes: 0
Views: 325
Reputation: 1
I rewrote it and it works:
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
Upvotes: 0
Reputation: 9199
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
Upvotes: 0
Reputation: 49455
Not all objects support all properties and methods. This error has the following cause and solution:
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.
Upvotes: 0