+ Reply to Thread
Results 1 to 3 of 3

File name based upon the cell value

  1. #1
    Registered User
    Join Date
    10-11-2019
    Location
    Nottingham
    MS-Off Ver
    MS Office
    Posts
    70

    File name based upon the cell value

    Hello Guys,

    I am facing a bit of an issue i hope some one will be able to put e in the correct direction.

    I am trying to use the below code for attaching an active work sheet to outlook and would like to name the file name as per the cell value D9 when i type the text in the cell value the code works fine but when the cell value has a formula like =E8&"/"&E6 the code stops working.

    I have posted similar question last year this wasnt problem then not sure what wrong with the below code.

    Thank you so much for your help.

    Sub Mail_workbook_Outlook_2()
    'Working in Excel 2000-2016
    'Mail a copy of the ActiveWorkbook with another file name
    Dim wb1 As Workbook
    Dim xSht As Worksheet
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim OutApp As Object
    Dim OutMail As Object
    Dim xUsedRng As Range
    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    End With

    Set wb1 = ActiveWorkbook
    Set xSht = ActiveSheet

    'Make a copy of the file/Open it/Mail it/Delete it
    'If you want to change the file name then change only TempFileName
    TempFilePath = Environ$("temp") & "\"
    TempFileName = xSht.Range("D9").Value & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))

    wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
    .to = ""
    .CC = ""
    .BCC = ""
    .Subject = "New Asset Form " + xSht.Range("M16").Value
    .Body = ""
    .Attachments.Add TempFilePath & TempFileName & FileExtStr
    'You can add other files also like this
    '.Attachments.Add ("C:\test.txt")
    .Display
    End With
    On Error GoTo 0

    'Delete the file
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    End Sub

  2. #2
    Registered User
    Join Date
    05-12-2021
    Location
    Europe
    MS-Off Ver
    365
    Posts
    45

    Re: File name based upon the cell value

    Use .text instead of .value

    Please Login or Register  to view this content.
    "Good Morning, Good Evening and Good Night"

  3. #3
    Registered User
    Join Date
    10-11-2019
    Location
    Nottingham
    MS-Off Ver
    MS Office
    Posts
    70

    Re: File name based upon the cell value

    thank you iustin , i am getting a debugg error wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr

    the formula works when the value is typed but will not work when the cell has = some thing

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Import a CSV file based on the value in a cell as the file name
    By chrismeeky in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-04-2018, 01:53 AM
  2. [SOLVED] Save Each Worksheet as a PDF File and Name each File Based on a Cell Value in each Sheet
    By hamidxa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-16-2016, 09:36 AM
  3. Replies: 0
    Last Post: 09-26-2012, 01:15 PM
  4. Replies: 1
    Last Post: 09-12-2012, 11:29 AM
  5. [SOLVED] VBA code to diplay a file path in a cell based on another cell value
    By zhb12810 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-03-2012, 03:24 PM
  6. External File(s) referenced and file links change based on row cell.
    By Jimmydageek in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-23-2009, 02:59 PM
  7. Replies: 1
    Last Post: 01-26-2007, 02:25 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1