Consulting

Results 1 to 3 of 3

Thread: custom 'Filename' function

  1. #1

    Question custom 'Filename' function

    Hi,

    I have the following code below which when executed, picks up certain data from the worksheet and auto-populates this into the filename of the 'Save As' dialogue box for the user.

    [vba]Sub SaveToFile()

    Dim FileName As Variant
    Dim sSheetName As String

    On Error Resume Next
    sSheetName = Application.VLookup(Range("K12").Value, Range("F119:G127"), 2, False)
    On Error GoTo 0

    With ActiveSheet

    If .Range("E6").Value = "" Or .Range("K8").Value = "" Then

    MsgBox "Both Name and Effective Date must be entered in order to Save", vbOKOnly + vbExclamation, "Save File"

    Else

    Range("P37").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    Range("R37").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    FileName = Range("E6").Value
    FileName = Mid(FileName, InStr(FileName, " ") + 1) & " " & _
    Left(FileName, InStr(FileName, " ") - 1) & _
    ", CONTRACT, " & Format(Range("K8").Value, "dd.mm.yy")
    FileName = Application.GetSaveAsFilename(FileName, "Microsoft Excel Files (*.xls), *.xls")
    If FileName <> False Then

    ActiveWorkbook.SaveAs FileName
    Worksheets(sSheetName).Activate
    End If
    End If
    End With
    End Sub[/vba]

    I would like to use this code to name my file (which gets attatched to an email automatically) in the following code. However, I am not sure where and how to put this in..... I think its this bit of the code

    [VBA] TempFilePath = Environ$("temp") & "\"
    TempFileName = "Your New Employee " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy")[/VBA]

    Could someone please help. Many Thanks!! Full code below;

    [vba]Sub ProduceEmail()
    'Working in 2000-2007
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object

    Dim sSheetName As String

    On Error Resume Next
    sSheetName = Application.VLookup(Range("K12").Value, Range("F119:G127"), 2, False)
    On Error GoTo 0
    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    End With
    Set Sourcewb = ActiveWorkbook
    'Copy the sheet to a new workbook
    Worksheets(sSheetName).Copy
    Set Destwb = ActiveWorkbook
    'Determine the Excel version and file extension/format
    With Destwb
    If Val(Application.Version) < 12 Then
    'You use Excel 97-2003
    FileExtStr = ".xls": FileFormatNum = -4143
    Else
    'You use Excel 2007
    'We exit the sub when your answer is NO in the security dialog that you only
    'see when you copy a sheet from a xlsm file with macro's disabled.
    If Sourcewb.Name = .Name Then
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    MsgBox "Your answer is NO in the security dialog"
    Exit Sub
    Else
    Select Case Sourcewb.FileFormat
    Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
    Case 52:
    If .HasVBProject Then
    FileExtStr = ".xlsm": FileFormatNum = 52
    Else
    FileExtStr = ".xlsx": FileFormatNum = 51
    End If
    Case 56: FileExtStr = ".xls": FileFormatNum = 56
    Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
    End Select
    End If
    End If
    End With
    ' 'Change all cells in the worksheet to values if you want
    With Destwb.Sheets(1).UsedRange
    .Cells.Copy
    .Cells.PasteSpecial xlPasteValues
    .Cells(1).Select
    End With
    Application.CutCopyMode = False
    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Your New Employee " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy")
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    With Destwb
    .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
    On Error Resume Next
    With OutMail
    .to = ""
    .CC = ""
    .BCC = ""
    .Subject = "This is the Subject line"
    .Body = "Hi there"
    .Attachments.Add Destwb.FullName
    'You can add other files also like this
    '.Attachments.Add ("C:\test.txt")
    .Display 'or use .Send
    End With
    On Error GoTo 0
    .Close savechanges:=False
    End With
    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr
    Set OutMail = Nothing
    Set OutApp = Nothing
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    End Sub[/vba]

  2. #2
    Can anyone help with this?

  3. #3
    Anyone! pls!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •