簡體   English   中英

Excel VBA-將單元格的內容添加到文件名的第一個空格中

[英]Excel VBA - Add contents of cell into first space of file name

在Excel電子表格上,我有A3中文件夾的路徑,A6中列出的A3文件夾中的文件名。 當我在J列中單擊時,現有宏會從活動行中生成帶有附件的電子郵件。 我需要在此宏中添加一部分代碼,該代碼還將通過將K1的內容添加到文件名的第一個空格中來重命名該文件,即將“ First Second.pdf”重命名為“ K1 Second.pdf的第一個內容”或“ First Second Third.pdf”到“ K1 Second Third.pdf的第一內容”

Sub Email_with_attachment()


Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItem)

    '------------------------
        Signature = Environ("appdata") & "\Microsoft\Signatures\"
    If Dir(Signature, vbDirectory) <> vbNullString Then
        Signature = Signature & Dir$(Signature & "*.htm")
    Else:
        Signature = ""
    End If
    Signature = CreateObject("Scripting.FileSystemObject").GetFile(Signature).OpenAsTextStream(1, -2).ReadAll

    '------------------------
    olMail.To = ""
    olMail.CC = ""
    olMail.VotingOptions = "Buyer resolving with Supplier;Now received/Corrected"
    olMail.Importance = olImportanceHigh
    '
    olMail.FlagRequest = "Reply"
    olMail.FlagDueBy = Range("H1").Value
'    olMail.OriginatorDeliveryReportRequested = True
'    olMail.ReadReceiptRequested = True
    '
    olMail.Subject = "Invoice issue: " & Range("A" & (ActiveCell.Row)).Value
    olMail.BodyFormat = olFormatHTML

    olMail.HTMLBody = "<HTML><BODY>Hello, <br /><br />Should this have been received by now?<br /><br /> Use Voting buttons above to reply, for convenience. </BODY></HTML>" & Signature


    olMail.Attachments.Add Range("A3") & Range("A" & (ActiveCell.Row)).Value
    olMail.Display

End Sub

Attachments.Add行之前,您需要重命名文件。 因此,您可以執行以下操作:

OldFilename = Range("A3") & Range("A" & (ActiveCell.Row))
NewFilename = Range("A3") & Mid(Range("A" & (ActiveCell.Row)), 1, 6) & Range("K1") & " " & Mid(Range("A" & (ActiveCell.Row)), 7)

Name OldFilename As NewFilename
olMail.Attachments.Add NewFilename

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM