Results 1 to 1 of 1

Merging 2 VBAs together, Email & Save

Threaded View

  1. #1
    Registered User
    Join Date
    04-17-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    36

    Merging 2 VBAs together, Email & Save

    Hi All,

    I have 2 VBAs which I need to work off of the same CommandButton1, is there someone out there who can help :-D

    Private Sub CommandButton1_Click()
        
        Set OutApp = CreateObject("Outlook.Application")
        OutApp.Session.Logon
        Set OutMail = OutApp.CreateItem(0)
     
        On Error Resume Next
        With OutMail
            .To = "email@email.com"
            .Subject = "Customer enquiry"
            .Body = "Customer enquiry!"
            .Attachments.Add ActiveWorkbook.FullName
            .Importance = 2
            .Send
        End With
        On Error GoTo 0
     
        Set OutMail = Nothing
        Set OutApp = Nothing
        
    MsgBox "Thank you."
        
    End Sub
    
    
    Sub CreateNewFileName()
         '--------------------------------------------------------------------------------
         'Produces an incremental FileName (if name is 'Data' it creates Data-1.xls)
         'Builds a suffix always one greater than the max suffix of any other potentially
         'existing files that have the same 'root' name, e.g. if 'Data.xls' and 'Data-2.xls'
         'exist, it creates Data-3.xls
         'Helps to avoid overwrite old files (among other uses)
         '--------------------------------------------------------------------------------
        Dim newFileName As String, strPath As String
        Dim strFileName As String, strExt As String
        strPath = "W:\CS 12\Enquiry Forms\" 'Change to suit
        strFileName = "Customer Enquiry Form" 'Change to suit
        strExt = ".xls" 'Change to suit
        newFileName = strFileName & "-" & GetNewSuffix(strPath, strFileName, strExt) & strExt
        MsgBox "The new file name is: " & newFileName
         'Save copy
        ActiveWorkbook.SaveCopyAs strPath & newFileName
    End Sub
     
    Function GetNewSuffix(ByVal strPath As String, ByVal strName As String, ByVal strExt As String) As Integer
        Dim strFile As String, strSuffix As String, intMax As Integer
        On Error GoTo ErrorHandler
         'File's name
        strFile = Dir(strPath & "\" & strName & "*")
        Do While strFile <> ""
             'File's suffix starts 2 chars after 'root' name (right after the "-")
            strSuffix = Mid(strFile, Len(strName) + 2, Len(strFile) - Len(strName) - Len(strExt) - 1)
             'FileName is valid if 1st char after name is "-" and suffix is numeric with no dec point
             'Skip file if "." or "," exists in suffix
            If Mid(strFile, Len(strName) + 1, 1) = "-" And CSng(strSuffix) >= 0 And _
            InStr(1, strSuffix, ",") = 0 And InStr(1, strSuffix, ".") = 0 Then
                 'Store the max suffix
                If CInt(strSuffix) >= intMax Then intMax = CInt(strSuffix)
            End If
    NextFile:
            strFile = Dir
        Loop
        GetNewSuffix = intMax + 1
        Exit Function
         
    ErrorHandler:
        If Err Then
            Err.Clear
            Resume NextFile
        End If
    End Function
    For the observant ones, yes, it's for a customer enquiry form, and it'll have commandbuttons to the other staff so the end user fills it out, clicks their name and it sends then saves it in the specified folder with the new sequential number as a .xls

    i hope that makes sense, they both work separately but i cant get them to work together.

    thanks!
    Last edited by adamj1910; 10-10-2012 at 04:33 AM. Reason: wrong code

Thread Information

Users Browsing this Thread

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

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