【发布时间】:2020-06-24 05:08:18
【问题描述】:
正在尝试编写两个宏来在收到新电子邮件时自动打印附件,并且只打印电子邮件的第一页。代码如下:
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Dim Folder As Outlook.MAPIFolder
Set Ns = Application.GetNamespace("MAPI")
Set Folder = Ns.GetDefaultFolder(olFolderInbox)
Set Items = Folder.Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
Printattachments Item
End If
End Sub
Private Sub Printattachments(oMail As Outlook.MailItem)
On Error Resume Next
Dim colAtts As Outlook.Attachments
Dim oAtt As Outlook.Attachment
Dim sFile As String
Dim sDirectory As String
Dim sFileType As String
sDirectory = "D:\Attachments\"
Set colAtts = oMail.Attachments
If colAtts.Count Then
For Each oAtt In colAtts
' This code looks at the last 4 characters in a filename
sFileType = LCase$(Right$(oAtt.FileName, 4))
Select Case sFileType
' Add additional file types below
Case "xlsx", "docx", ".pdf", ".doc", ".xls"
sFile = sDirectory & oAtt.FileName
oAtt.SaveAsFile sFile
ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
End Select
Next
End If
End Sub
Sub PrintOnePage()
SendKeys "%F", False
SendKeys "P"
SendKeys "{TAB 2}", True
SendKeys "{DOWN}", True
SendKeys "1"
SendKeys "{ENTER}"
End Sub
Sub RunAll()
Call Printattachments
Call PrintOnePage
End Sub
然后我点击了 General 和 Run-all 并遇到了 Compile Error: Argument not optional。
任何意见将不胜感激!
【问题讨论】:
-
Printattachments获取oMail作为参数。你应该在调用它时将它传递给 Sub。如果您想在收到的电子邮件上运行它,则需要对其进行修改。 -
你想做什么?运行它而不是自动运行?
-
让我知道这是否适合您。提出问题后进行跟进很重要,让每个人都可以使用已提出问题的答案。
-
您好,Masoud 感谢您的及时回复!我对代码很陌生。我尝试用您在下面提供的代码替换 Sub RunAll 部分并得到另一个编译错误:子或函数中的无效属性
-
我创建了规则来打印任何带有附件的传入电子邮件。 printattachment 代码本身可以正常工作,但同时我希望 Outlook 只打印任何带有附件的传入电子邮件的第一页。