excel 如何保存文件名中包含今天日期的工作表,然后将该工作表作为附件通过电子邮件发送?[已关闭]

t40tm48m  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(89)

已关闭。此问题需要超过focused。当前不接受答案。
**想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

昨天关门了。
Improve this question
我需要生成一个周报并发送它。我已经设法编译代码;
1.将文件保存为“工作表名称”+当天日期。
1.在outlook中创建一封电子邮件,包含收件人、主题和消息,并添加附件。
1.清除所选单元格的内容。
我遇到的问题是2代码的最后一步,在那里我需要附加一个特定的文档。相反,我想添加在步骤1中创建的文件,但不确定如何将代码指向那个方向。
作为参考,我的代码如下:

`'Specify Email Items and Add Attachment
With EmailItem
.To = "blank@outlook.com.au"
.Subject = "Reports - Orders and Sales"
.Body = "Hello 'Blank'," & vbNewLine & _
"I'm sharing the Foot Traffic Report. Please find the PDF attachment." & _
vbNewLine & "Regards," & vbNewLine & "Blank"
.Attachments.Add "C:\Users\FilePath\Documents\Foot Traffic Report\Reports - Orders and Sales.pdf"
'.send
.Display
End With
Set EmailItem = Nothing
Set EmailApp = Nothing
`

我有一个现有的代码,保存文件与今天的日期,如步骤一所述:

`Sub Save_PDF_Current_Folder()

Dim xName As String

xName = ThisWorkbook.Path & "\" & ActiveSheet.Name & " " & _
`Format(Now(), "dd.mm.yy") & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub

`

我无法对附件进行将来校样,因此它将使用新日期附加文档。
粗略的搜索告诉我如何做步骤1或步骤2,但不是在一个步骤中同时做这两个。

tjjdgumg

tjjdgumg1#

要保存为包含今天日期的字符串,请考虑以下情况。

Sub SaveToday()

ActiveWorkbook.SaveAS ("C:\My Documents\Daily Sales Report" _ 
                              & Format(Now(), "DD-MMM-YYYY") & ".xlsx"

End Sub

要添加包含收件人、主题和消息的电子邮件,请考虑以下事项。

Make a list in Sheets("Sheet1") with :

In column A : Names of the people
In column B : E-mail addresses
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)

宏将遍历“Sheet1”中的每一行,如果B列中有电子邮件地址,C:Z列中有文件名,则宏将使用此信息创建邮件并发送。

Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .to = cell.Value
                .Subject = "Testfile"
                .Body = "Hi " & cell.Offset(0, -1).Value

                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                    If Trim(FileCell) <> "" Then
                        If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
                        End If
                    End If
                Next FileCell

                .Send  'Or use .Display
            End With

            Set OutMail = Nothing
        End If
    Next cell

    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

相关问题