Vba Outlook使用主题行保存多个附件,并递增该名称
我花了几个星期的时间玩VBA,我绝对不是这方面的专家 我要找的是对这段代码的修改Vba Outlook使用主题行保存多个附件,并递增该名称,vba,excel,email,outlook,Vba,Excel,Email,Outlook,我花了几个星期的时间玩VBA,我绝对不是这方面的专家 我要找的是对这段代码的修改 Public Sub SaveAttachments() Dim objOL As Outlook.Application Dim objMsg As Outlook.MailItem 'Object Dim objAttachments As Outlook.Attachments Dim objSelection As Outlook.Selection Dim i As Integer Dim lngCoun
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Integer
Dim lngCount As Integer
Dim strFile As String
Dim strFolderpath As String
Dim strFileName As String
Dim objSubject As String
Dim strDeletedFiles As String
' Get the path to your My Documents folder
'strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
On Error Resume Next
' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection
' The attachment folder needs to exist
' You can change this to another folder name of your choice
' Set the Attachment folder.
strFolderpath = "C:\Users\demkep\Documents\"
' Check each selected item for attachments.
For Each objMsg In objSelection
'Set FileName to Subject
objSubject = objMsg.Subject
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
' Use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
For i = lngCount To 1 Step -1
' Get the file name.
strFileName = objSubject & ".pdf"
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFileName
Debug.Print strFile
' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
Next i
End If
Next
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
这是最接近我想要完成的
但是,当我收到一封带有多个附件的电子邮件时,它只会覆盖最后一个文件。如果可能的话。我想将它保存为(有时最多30个.pdf文件)emailsubject、emailsubject(1)、emailsubject(2)、emailsubject(3)等
任何帮助都将不胜感激。您不会在循环中更改文件名。差不多
strFileName = objSubject & "(" & i & ").pdf"
我应该处理好这件事
如果您只需要数字,如果有多个附件,您可以在设置名称之前检查lngCount或使用IIf
If lngCount > 1 Then
strFileName = objSubject & "(" & i & ").pdf"
Else
strFileName = objSubject & ".pdf"
End If
或
您不应该在整个子循环中使用“错误恢复下一步”。您没有更改循环中的文件名。差不多
strFileName = objSubject & "(" & i & ").pdf"
我应该处理好这件事
如果您只需要数字,如果有多个附件,您可以在设置名称之前检查lngCount或使用IIf
If lngCount > 1 Then
strFileName = objSubject & "(" & i & ").pdf"
Else
strFileName = objSubject & ".pdf"
End If
或
你不应该在你的整个sub-btw上使用下一步错误恢复时的
。这里的函数将完全满足你的需要
Function UniqueName(FilePath As String) As String
Dim FSO As Object
Set FSO = CreateObject("Scripting.FilesystemObject")
Dim FileName As String
FileName = FilePath
Dim Ext As String
Ext = Chr(46) & FSO.GetExtensionName(FilePath)
Dim i As Long
i = 1
Do While FSO.FileExists(FileName)
FileName = Left(FilePath, Len(FilePath) - Len(Ext)) & " (" & i & ")" & Ext
i = i + 1
Loop
UniqueName = FileName
End Function
并将此strFile=strFolderpath&strFileName
更改为strFile=UniqueName(strFolderpath&strFileName)
这是一个可以完全满足您需要的函数
Function UniqueName(FilePath As String) As String
Dim FSO As Object
Set FSO = CreateObject("Scripting.FilesystemObject")
Dim FileName As String
FileName = FilePath
Dim Ext As String
Ext = Chr(46) & FSO.GetExtensionName(FilePath)
Dim i As Long
i = 1
Do While FSO.FileExists(FileName)
FileName = Left(FilePath, Len(FilePath) - Len(Ext)) & " (" & i & ")" & Ext
i = i + 1
Loop
UniqueName = FileName
End Function
并将此strFile=strFolderpath&strFileName
更改为strFile=UniqueName(strFolderpath&strFileName)
谢谢,这样可以节省我每天大约一小时的繁忙工作。工作很有魅力。谢谢你,这让我每天节省了一个小时的繁忙工作。工作得很有魅力。