【问题标题】:Run multiple macros with a single macro, run into Compile Error使用单个宏运行多个宏,遇到编译错误
【发布时间】: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 只打印任何带有附件的传入电子邮件的第一页。

标签: vba outlook


【解决方案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 在将一个或多个项目添加到指定集合时发生。一次将大量项目添加到文件夹时不会运行此事件。


【讨论】:

  • 0m3r 感谢您的指导和解释。我会试一试,明天早上恢复。
【解决方案2】:

参考此post,我会将您的订阅添加到此代码中(它位于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)。

【讨论】:

  • 我尝试用您在下面提供的代码替换 Sub RunAll 部分并得到另一个编译错误:子或函数中的无效属性
  • @MingLian 它应该转到ThisOutlookSession 模块并且不应该运行。将所有内容粘贴到该模块并重新启动 Outlook 后,向该地址发送一封测试电子邮件,看看它是否有效(检查打印的文件是否在目录中)。另外,请参考我提供的帖子尝试一些调试,以防万一。
  • 我明天早上试试。感谢您的跟进。
猜你喜欢
  • 1970-01-01
  • 2016-02-16
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
相关资源
最近更新 更多