使用单个宏运行多个宏,运行到编译错误

时间:2017-04-03 23:13:42

标签: vba outlook outlook-vba

在收到新电子邮件时尝试编写两个宏来自动打印附件,并且只打印电子邮件的第一页。代码如下所示:

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不是可选的。

非常感谢任何输入!

2 个答案:

答案 0 :(得分:1)

您需要做的是将PrintOnePage更改为

Public Sub PrintOnePage(ByVal Item As Object)
    SendKeys "%FPR"
    SendKeys "%S"
    SendKeys "1"
    SendKeys "{ENTER}"
End Sub

然后在 ItemAdd Events 上添加

Private Sub Items_ItemAdd(ByVal Item As Object)
  If TypeOf Item Is Outlook.MailItem Then
    Printattachments Item
    PrintOnePage Item '<-------- add
  End If
End Sub

请记住,只要您收到电子邮件,它就会打印一个电子邮件正文页面。

仅打印带附件的项目正文,然后将PrintOnePage Item移至

实施例

Private Sub Printattachments(ByVal Item As Outlook.MailItem)
    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 = Item.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

    PrintOnePage Item '<-------- add

End Sub
  

Items.ItemAdd Event在将一个或多个项目添加到指定集合时发生。当大量项目一次添加到文件夹时,此事件不会运行。

答案 1 :(得分:0)

参考此post我会将您的Subs添加到此代码(它转到Sub RunAll的位置):

Private WithEvents Items As Outlook.Items 
Private Sub Application_Startup() 
  Dim olApp As Outlook.Application 
  Dim objNS As Outlook.NameSpace 
  Set olApp = Outlook.Application 
  Set objNS = olApp.GetNamespace("MAPI") 
  ' default local Inbox
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items 
End Sub
Private Sub Items_ItemAdd(ByVal item As Object) 

  On Error Goto ErrorHandler 
  Dim Msg As Outlook.MailItem 
  If TypeName(item) = "MailItem" Then
    Set Msg = item 
    ' ******************
    Call Printattachments(Msg)
    Call PrintOnePage
    ' ******************
  End If
ProgramExit: 
  Exit Sub
ErrorHandler: 
  MsgBox Err.Number & " - " & Err.Description 
  Resume ProgramExit 
End Sub

重要

将所有代码粘贴到ThisOutlookSession模块中。

这将在收到任何电子邮件(需要重新启动Outlook)后运行宏。