+ Reply to Thread
Results 1 to 18 of 18

why is the file name duplicating in subject field?

Hybrid View

  1. #1
    Registered User
    Join Date
    04-01-2021
    Location
    United Kingdom
    MS-Off Ver
    2018
    Posts
    17

    why is the file name duplicating in subject field?

    Hi there,

    I need some help with a VBA code and was wondering if anyone could help.

    I needed a VBA code to email a single excel sheet from a workbook and found the code below. The problem is that when the file gets emailed to the recipient, the file name says 'TestFileScottNew.xlsm.xlsm'. In other words, the 'xlsm' part gets duplicated in the attachment and I want to prevent this from happening. I was wondering if anyone could help and let me know how to remove one of the xlsm's in the file name? (if you have a better code to help me achieve my aim then feel free to suggest it!)

    Here is the code:

    Sub SendWorkSheet()
    'Update 20131209
    Dim xFile As String
    Dim xFormat As Long
    Dim Wb As Workbook
    Dim Wb2 As Workbook
    Dim FilePath As String
    Dim FileName As String
    Dim OutlookApp As Object
    Dim OutlookMail As Object
    On Error Resume Next
    Application.ScreenUpdating = False
    Set Wb = Application.ActiveWorkbook
    ActiveSheet.Copy
    Set Wb2 = Application.ActiveWorkbook
    Select Case Wb.FileFormat
    Case xlOpenXMLWorkbook:
    xFile = ".xlsx"
    xFormat = xlOpenXMLWorkbook
    Case xlOpenXMLWorkbookMacroEnabled:
    If Wb2.HasVBProject Then
    xFile = ".xlsm"
    xFormat = xlOpenXMLWorkbookMacroEnabled
    Else
    xFile = ".xlsx"
    xFormat = xlOpenXMLWorkbook
    End If
    Case Excel8:
    xFile = ".xls"
    xFormat = Excel8
    Case xlExcel12:
    xFile = ".xlsb"
    xFormat = xlExcel12
    End Select
    FilePath = Environ$("temp") & ""
    FileName = Wb.Name
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)
    Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
    With OutlookMail
    .To = "xxxxx"
    .CC = ""
    .BCC = ""
    .Subject = "Excel sheet test"
    .Body = "Hello, please see file attached. Regards"
    .Attachments.Add Wb2.FullName
    .Send
    End With
    Wb2.Close
    Kill FilePath & FileName & xFile
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
    Application.ScreenUpdating = True
    End Sub
    Any help appreciated!
    Last edited by ray707; 04-01-2021 at 05:03 PM.

  2. #2
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: why is the file name duplicating in subject field?

    Try...
    Wb2.SaveAs FilePath & FileName, FileFormat:=xFormat
    ... and add code tags to your post.

  3. #3
    Registered User
    Join Date
    09-01-2018
    Location
    USA
    MS-Off Ver
    365
    Posts
    96

    Re: why is the file name duplicating in subject field?

    change this line
    Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
    to this
    Wb2.SaveAs FilePath & FileName , FileFormat:=xFormat

  4. #4
    Registered User
    Join Date
    04-01-2021
    Location
    United Kingdom
    MS-Off Ver
    2018
    Posts
    17

    Re: why is the file name duplicating in subject field?

    Quote Originally Posted by dangelor View Post
    Try...
    Wb2.SaveAs FilePath & FileName, FileFormat:=xFormat
    ... and add code tags to your post.
    Quote Originally Posted by AC PORTA VIA View Post
    change this line
    Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
    to this
    Wb2.SaveAs FilePath & FileName , FileFormat:=xFormat
    That worked! Thank you!

    The only thing is the file name now has 'temp' at the beginning, so it says 'TempTestFileScottNew.xlsm'- this is less of a problem but any ideas why and how to fix it?!

    also how can I put this into a command button so that the user can press a button and it pings out the email? I tried putting the above into the VBA for a command button but it didn't work:

    Private Sub CommandButton1_Click()
    Sub SendWorkSheet()
    'Update 20131209
    Dim xFile As String
    Dim xFormat As Long
    Dim Wb As Workbook
    Dim Wb2 As Workbook
    Dim FilePath As String
    Dim FileName As String
    Dim OutlookApp As Object
    Dim OutlookMail As Object
    On Error Resume Next
    Application.ScreenUpdating = False
    Set Wb = Application.ActiveWorkbook
    ActiveSheet.Copy
    Set Wb2 = Application.ActiveWorkbook
    Select Case Wb.FileFormat
    Case xlOpenXMLWorkbook:
    xFile = ".xlsx"
    xFormat = xlOpenXMLWorkbook
    Case xlOpenXMLWorkbookMacroEnabled:
    If Wb2.HasVBProject Then
    xFile = ".xlsm"
    xFormat = xlOpenXMLWorkbookMacroEnabled
    Else
    xFile = ".xlsx"
    xFormat = xlOpenXMLWorkbook
    End If
    Case Excel8:
    xFile = ".xls"
    xFormat = Excel8
    Case xlExcel12:
    xFile = ".xlsb"
    xFormat = xlExcel12
    End Select
    FilePath = Environ$("temp") & ""
    FileName = Wb.Name
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)
    Wb2.SaveAs FilePath & FileName, FileFormat:=xFormat
    With OutlookMail
    .To = "xxxxx"
    .CC = ""
    .BCC = ""
    .Subject = "Excel sheet test"
    .Body = "Hello, please see file attached. Regards"
    .Attachments.Add Wb2.FullName
    .Send
    End With
    Wb2.Close
    Kill FilePath & FileName & xFile
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
    Application.ScreenUpdating = True
    
    End Sub
    The error says:

    Compile error: expected end sub
    Last edited by ray707; 04-01-2021 at 05:32 PM.

  5. #5
    Registered User
    Join Date
    09-01-2018
    Location
    USA
    MS-Off Ver
    365
    Posts
    96

    Re: why is the file name duplicating in subject field?

    remove below line
    'Sub SendWorkSheet()

  6. #6
    Registered User
    Join Date
    04-01-2021
    Location
    United Kingdom
    MS-Off Ver
    2018
    Posts
    17

    Re: why is the file name duplicating in subject field?

    Quote Originally Posted by AC PORTA VIA View Post
    remove below line
    'Sub SendWorkSheet()
    This worked for the command button, thank you. It does generate a pop up notification upon activation though which says 'A file named C:\Users\xxxx\AppData\Local\TempBook1.xlsm already exists in this location. Do you want to replace it?' but I think that is okay, right? My understanding is that the way these codes work is that they create a temp file in a location and then deletes it, so this is simply doing that..? Therefore I don't think we can avoid this can we..?
    Last edited by ray707; 04-02-2021 at 04:05 AM.

  7. #7
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: why is the file name duplicating in subject field?

    The only thing is the file name now has 'temp' at the beginning, so it says 'TempTestFileScottNew.xlsm'- this is less of a problem but any ideas why and how to fix it?!
    Try this...
        FilePath = Environ$("temp") & "\"

  8. #8
    Registered User
    Join Date
    04-01-2021
    Location
    United Kingdom
    MS-Off Ver
    2018
    Posts
    17

    Re: why is the file name duplicating in subject field?

    Quote Originally Posted by dangelor View Post
    Try this...
        FilePath = Environ$("temp") & "\"
    This didn't work for removing 'temp'; it would send an email without an attachment and before that it would ask me to save the file which I want to avoid...

  9. #9
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: why is the file name duplicating in subject field?

    Try this version...
    Private Sub CommandButton1_Click()
        'Update 20131209
        
        Dim wb1 As Workbook, wb2 As Workbook
        Dim sFilePath As String, sFileName As String
        Dim iFormat As Integer
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
        End With
        
        Set wb1 = Application.ActiveWorkbook
        ActiveSheet.Copy
        Set wb2 = Application.ActiveWorkbook
        
        sFilePath = Environ$("temp")
        sFileName = sFilePath & "\ " & wb1.Name
        iFormat = wb1.FileFormat
        wb2.SaveAs sFileName, iFormat
        wb2.Close
        
        With CreateObject("Outlook.Application").createItem(0)
            .To = "xxxxx"
            .CC = ""
            .BCC = ""
            .Subject = "Excel sheet test"
            .Body = "Hello, please see file attached. Regards"
            .Attachments.Add sFileName
            .Send
        End With
        
        Kill sFileName
        
        With Application
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
        
    End Sub
    Last edited by dangelor; 04-02-2021 at 08:12 AM.

  10. #10
    Registered User
    Join Date
    04-01-2021
    Location
    United Kingdom
    MS-Off Ver
    2018
    Posts
    17

    Re: why is the file name duplicating in subject field?

    Quote Originally Posted by dangelor View Post
    try this version...
    private sub commandbutton1_click()
        'update 20131209
        
        dim wb1 as workbook, wb2 as workbook
        dim sfilepath as string, sfilename as string
        dim iformat as integer
    
        with application
            .screenupdating = false
            .displayalerts = false
        end with
        
        set wb1 = application.activeworkbook
        activesheet.copy
        set wb2 = application.activeworkbook
        
        sfilepath = environ$("temp")
        sfilename = sfilepath & "\ " & wb1.name
        iformat = wb1.fileformat
        wb2.saveas sfilename, iformat
        wb2.close
        
        with createobject("outlook.application").createitem(0)
            .to = "xxxxx"
            .cc = ""
            .bcc = ""
            .subject = "excel sheet test"
            .body = "hello, please see file attached. Regards"
            .attachments.add sfilename
            .send
        end with
        
        kill sfilename
        
        with application
            .displayalerts = true
            .screenupdating = true
        end with
        
    end sub
    thank you!

  11. #11
    Registered User
    Join Date
    04-01-2021
    Location
    United Kingdom
    MS-Off Ver
    2018
    Posts
    17

    Re: why is the file name duplicating in subject field?

    Hello, I have a quick question about the code posted by dangelor 2 posts above. I want to amend this code to send out the email to an address in a specific cell in that worksheet; so for example, if I write an email address in cell D2 of the worksheet, I want the code to send the sheet to that email address. I thought I could do this by simple amending the .To part of the code:


    With CreateObject("Outlook.Application").createItem(0)
            .To = Sheet1.Range("D2")
            .CC = ""
            .BCC = ""
            .Subject = "Excel sheet"
            .Body = "Hello" & vbLf & vbLf & "Please find spreadsheet attached." & vbLf & vbLf & "Regards" & vbLf & "Ray"
            .Attachments.Add sFileName
            .Send
        End With
    However this did not work and no email was sent out. Please can someone advise?
    Last edited by ray707; 04-14-2021 at 05:48 AM.

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: why is the file name duplicating in subject field?

    Are you sure the codename of your sheet is Sheet1?
    Everyone who confuses correlation and causation ends up dead.

  13. #13
    Registered User
    Join Date
    04-01-2021
    Location
    United Kingdom
    MS-Off Ver
    2018
    Posts
    17

    Re: why is the file name duplicating in subject field?

    Quote Originally Posted by rorya View Post
    Are you sure the codename of your sheet is Sheet1?
    In the actual excel file the sheet is called 'Craft summary' and in the visual basic under Microsoft Excel Objects it says 'Sheet1 (Craft summary)'. Am I inputting the name incorrectly for the sheet?

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: why is the file name duplicating in subject field?

    And that is the workbook with the code in it?

  15. #15
    Registered User
    Join Date
    04-01-2021
    Location
    United Kingdom
    MS-Off Ver
    2018
    Posts
    17

    Re: why is the file name duplicating in subject field?

    I got it working! I transferred the code from module to worksheet and that worked. Perhaps I need to improve my understanding of module vs worksheet as I thought it was always best to put the code in the module

    Thanks!
    Last edited by ray707; 04-14-2021 at 08:10 AM.

  16. #16
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    91,007

    Re: why is the file name duplicating in subject field?

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  17. #17
    Registered User
    Join Date
    04-01-2021
    Location
    United Kingdom
    MS-Off Ver
    2018
    Posts
    17

    Re: why is the file name duplicating in subject field?

    Hi guys, quick question.

    If I want the text in the body of the email to reference a cell in the excel sheet, how would I do that?

    So for example, if the job number is in cell A9, and I want the text in the email body to say 'please find spreadsheet attached for A9', how would I amend the code?

            .Body = "Hello" & vbLf & vbLf & "Please find spreadsheet attached." & vbLf & vbLf & "Regards" & vbLf & "Ray"
    Any help would be appreciated

  18. #18
    Registered User
    Join Date
    04-01-2021
    Location
    United Kingdom
    MS-Off Ver
    2018
    Posts
    17

    Re: why is the file name duplicating in subject field?

    hmmm the code to send out an email is not working now, I am not sure why. Does anyone have any idea why? When I click on the command button to send the email it creates a new file with that worksheet in it and nothing gets emailed. This didn't happen usually, it would just send that worksheet out automatically (no new file was created or anything, it would get emailed straight out to the recipient)

    Private Sub CommandButton1_Click()
        'Update 20131209
        
        Dim wb1 As Workbook, wb2 As Workbook
        Dim sFilePath As String, sFileName As String
        Dim iFormat As Integer
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
        End With
        
        Set wb1 = Application.ActiveWorkbook
        ActiveSheet.Copy
        Set wb2 = Application.ActiveWorkbook
        
        sFilePath = Environ$("temp")
        sFileName = sFilePath & "\ " & wb1.Name
        iFormat = wb1.FileFormat
        wb2.SaveAs sFileName, iFormat
        wb2.Close
        
        With CreateObject("Outlook.Application").createItem(0)
            .To = "xxxxxxx"
            .CC = ""
            .BCC = ""
            .Subject = "Excel sheet test"
            .Body = "Hello, please see file attached. Regards"
            .Attachments.Add sFileName
            .Send
        End With
        
        Kill sFileName
        
        With Application
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
        
    End Sub


    EDIT: The code works now so this post is redundant. If anyone can help with the post above that would be great
    Last edited by ray707; 04-29-2021 at 10:24 AM.

+ 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. Populate Subject Line if the "To" Field belongs to a specific Recipient
    By RXcel in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 10-06-2019, 03:04 AM
  2. Replies: 17
    Last Post: 01-12-2018, 12:57 PM
  3. [SOLVED] Help comparing two files by subject+Date, subject+DatePlus1, Subject+DateMinus1
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 07-11-2017, 07:37 AM
  4. [SOLVED] Todays date in subject field of mailto hyperlink formula
    By ayz in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-29-2016, 08:30 AM
  5. Save outlook attachment as file using email subject as file name w/o invalid characters
    By kristinlarmer in forum Outlook Formatting & Functions
    Replies: 1
    Last Post: 10-16-2015, 05:07 PM
  6. [SOLVED] Pivot tables duplicating one field
    By ctone51 in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 03-23-2014, 02:02 AM
  7. Duplicating rows and doing calculations based on a field
    By Iceotron in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-17-2012, 02:48 AM

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