如何使用公式生成的文件名将saveSave和作为电子邮件附件上传工作簿

时间:2019-05-01 23:07:26

标签: excel vba

我正在研究VBA代码,以根据Y1工作表中单元格Schedule_Maker中的公式使用特定名称保存excel工作簿。保存文件后,我想通过附件将工作簿作为附件通过Outlook发送到特定的电子邮件地址(“ specifname@company.com”)

问题: -我在根据saveAs中的公式将代码写入.xlsmY1时遇到一些麻烦 -由于每个用户都有不同的计算机,因此我需要代码来找到他们的“我的文档”文件夹并将工作簿保存到该文件路径。 -我不确定将保存的工作簿附加到电子邮件的最佳编码方式。

到目前为止,这是我的代码:我们将不胜感激。谢谢。

 Private Sub SaveandEmail()
    Dim strFolder As String
    Dim i As Long

    'Find the position of the period in the file name
    i = InStr(ActiveWorkbook.Name, ".")

    'Create a default file name by concatenating the file name without the extention _

    Filename = ThisWorkbook.Sheets("Schedule_Maker").Range("Y1").Value & ".xlsm"

    'Open Save As dialog to a default folder with default file name
    With Application.FileDialog(msoFileDialogSaveAs)
        .AllowMultiSelect = False
        . Filename
        .InitialView = msoFileDialogViewDetails

        If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub

        'get selected folder path from FileDialog, but remove filename from FileDialog
        folderPath = call GetFolderPath

        'Save this workbook in chosen file path & appropriate filename
        'File format .xlsm
        ThisWorkbook.SaveAs Filename:=folderPath & Filename, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End With 
End Sub

Sub GetFolderPath()
    Dim objSFolders As Object
    Dim Folderpath As String
    Set objSFolders = CreateObject("WScript.Shell").SpecialFolders

    objSFolders ("mydocuments")

    End With
End Sub

0 个答案:

没有答案