从历史上看,我使用Excel和Lotus笔记来实现这一目标,公司正在通过Outlook 2016过渡,因为它是标准的电子邮件客户端 .
我们从多个分支机构的冰箱单位获取每日报告到邮箱 . 每个分支都是单独的电子邮件,但某些附件的名称相同 .
我使用了一个从LN复制附件的脚本,它有一个私有函数,在处理复制附件时,如果它们具有相同的名称,它将重命名它们 .
我在堆栈溢出处找到了一个脚本,我修改了该脚本以将Outlook中的附件保存到Network文件夹中 . 这很好 .
这是脚本
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 Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
' Get the path to your My Documents folder
'strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
strFolderpath = "J:\Clayton\Logistics\Plantwatch\REPORTS\ZDumpSites\"
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
' Set the Attachment folder.
strFolderpath = strFolderpath '& "\Attachments\"
' Check each selected item for attachments. If attachments exist,
' save them to the strFolderPath folder and strip them from the item.
For Each objMsg In objSelection
' This code only strips attachments from mail items.
' If objMsg.class=olMail Then
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
strDeletedFiles = ""
If lngCount > 0 Then
' We need to 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
' Save attachment before deleting from item.
' Get the file name.
strFile = objAttachments.Item(i).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile
' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
' Delete the attachment.
'objAttachments.Item(i).Delete
'write the save as path to a string to add to the message
'check for html and use html tags in link
If objMsg.BodyFormat <> olFormatHTML Then
strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
Else
strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
strFile & "'>" & strFile & "</a>"
End If
'Use the MsgBox command to troubleshoot. Remove it from the final code.
'MsgBox strDeletedFiles
Next i
' Adds the filename string to the message body and save it
' Check for HTML body
If objMsg.BodyFormat <> olFormatHTML Then
objMsg.Body = vbCrLf & "The file(s) were saved to " & strDeletedFiles & vbCrLf & objMsg.Body
Else
objMsg.HTMLBody = "<p>" & "The file(s) were saved to " & strDeletedFiles & "</p>" & objMsg.HTMLBody
End If
objMsg.Save
End If
Next
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
我试图将此函数添加到此脚本:
Private Function UniqueFileName(ByVal Fn As String) As String ' Rename same Name files.
Dim Fun As String ' Function return value
Dim Sp() As String ' Split file name
Dim Ext As Long ' file extension character count
Dim i As Integer ' file name index
Sp = Split(Fn, ".")
If UBound(Sp) Then Ext = Len(Sp(UBound(Sp))) + 1
Fun = stPath & Fn
Do While Len(Dir(Fun))
i = i + 1
Fun = stPath & Left(Fn, Len(Fn) - Ext) & _
"(" & CStr(i) & ")" & Right(Fn, Ext)
If i > 100 Then Exit Do
Loop
UniqueFileName = Fun
End Function
但我可以搜索到无法找到适合或添加到脚本的位置 .
如何将此功能添加到上面的优秀脚本中以重命名相同的命名附件?
我怀疑我错过了一些简单的东西!
2 回答
更改:
至:
功能:
试试吧
将以下内容添加到变量中
然后调用函数
这是你有两个功能
祝好运 - :-)