gpt4 book ai didi

vba - 用一个宏运行多个宏,遇到编译错误

转载 作者:行者123 更新时间:2023-12-02 20:00:21 25 4
gpt4 key购买 nike

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

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

然后我单击“常规”和“全部运行”并遇到编译错误:参数不可选。

任何意见都将不胜感激!

最佳答案

您需要做的是将 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 Occurs when one or more Items are added to the specified collection. This event does not run when a large number of items are added to the folder at once.

<小时/>

关于vba - 用一个宏运行多个宏,遇到编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43195807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com