+ Reply to Thread
Results 1 to 10 of 10

Call a beforesave macro in an email macro

Hybrid View

  1. #1
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Call a beforesave macro in an email macro

    Hi there,

    I have a beforesave macro that checks for any missing data that is required. It has message boxes that pop up and such to let users know they are missing data. I also have a mail macro where the user can press a submit button and it prompts the workbook to mail out. Obviously this mail macro does not have any sort of validation to check whether the required values are actually present. Is there any way i can call the beforesave macro into the mail macro?

    Code for Beforesave (In Workbook module)
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim myArr() As Variant
    Dim i As Integer, x As Integer
    Dim icell As Range, xcell As Range, rng1 As Range, rng2 As Range
    Dim iMsg
    
    myArr = Array("WO1", "WO2", "WO3", "WO4", "WO5")
    
    
    On Error Resume Next
    For i = 0 To 4
        Set rng1 = Union(Sheets(myArr(i)).Range("C26:C33"), Sheets(myArr(i)).Range("I26:I33"), Sheets(myArr(i)).Range("O26:O33"))
        For Each icell In rng1
            Select Case icell.Column
                Case Is = 3
                    If Not IsEmpty(icell) Then
                        If IsEmpty(icell.Offset(0, 1)) Then
                            iMsg = MsgBox("Error.  Please enter Yes/No for 'Customer Spare Part' in cell '" & icell.Offset(0, 1).Address & "' in section A of " & Sheets(myArr(i)).Name & _
                            vbNewLine & "                              Would you like to see the part number?", vbYesNo Or vbExclamation, "Missing Information!")
                            Cancel = True
                        End If
                        If iMsg = vbYes Then
                            MsgBox (icell.Value)
                        End If
                    End If
                Case Is = 9
                    If Not IsEmpty(icell) Then
                        If IsEmpty(icell.Offset(0, 1)) Then
                            iMsg = MsgBox("Error.  Please enter Yes/No for 'Customer Spare Part' in cell '" & icell.Offset(0, 1).Address & "' in section B of " & Sheets(myArr(i)).Name & _
                            vbNewLine & "                              Would you like to see the part number?", vbYesNo Or vbExclamation, "Missing Information!")
                            Cancel = True
                        End If
                        If iMsg = vbYes Then
                            MsgBox (icell.Value)
                        End If
                    End If
                Case Is = 15
                    If Not IsEmpty(icell) Then
                        If IsEmpty(icell.Offset(0, 1)) Then
                            iMsg = MsgBox("Error.  Please enter Yes/No for 'Customer Spare Part' in cell '" & icell.Offset(0, 1).Address & "' in section C of " & Sheets(myArr(i)).Name & _
                            vbNewLine & "                              Would you like to see the part number?", vbYesNo Or vbExclamation, "Missing Information!")
                            Cancel = True
                        End If
                        If iMsg = vbYes Then
                            MsgBox (icell.Value)
                        End If
                    End If
            End Select
        Next icell
    Next i
    
    For x = 0 To 4
    Set rng2 = Union(Sheets(myArr(x)).Range("E17"), Sheets(myArr(x)).Range("K17"), Sheets(myArr(x)).Range("R17"), Sheets(myArr(x)).Range("V17"))
        For Each xcell In rng2
            Select Case xcell.Column
                Case Is = 5
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C38")) Then
                            MsgBox "Please Enter a comment for Section A of " & Sheets(myArr(x)).Name
                            Cancel = True
                        End If
                    End If
                Case Is = 11
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C42")) Then
                            MsgBox "Please Enter a comment for Section B of " & Sheets(myArr(x)).Name
                            Cancel = True
                        End If
                    End If
                Case Is = 18
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C46")) Then
                            MsgBox "Please Enter a comment for Section C of " & Sheets(myArr(x)).Name
                            Cancel = True
                        End If
                    End If
                Case Is = 22
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C50")) Then
                            MsgBox "Please enter a comment for section D of " & Sheets(myArr(x)).Name
                            Cancel = True
                        End If
                    End If
            End Select
        Next xcell
    Next x
                    
    End Sub
    Mail Macro (In Module 1)
    Sub Mail_WO()
    'Working in 2000-2010
        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
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        Set Sourcewb = ActiveWorkbook
        'Call CheckData
    
        'Determine the Excel version and file extension/format
        With Sourcewb
            If Val(Application.Version) < 12 Then
                'You use Excel 2000-2003
                FileExtStr = ".xls": FileFormatNum = -4143
            Else
                'You use Excel 2007-2010, we exit the sub when your answer is
                'NO in the security dialog that you only see  when you copy
                'an 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
    
    
        'Save the new workbook/Mail it/Delete it
        TempFilePath = Environ$("temp") & "\"
        TempFileName = Sheets("WO1").Range("E10").Value & " - " & Sheets("WO1").Range("W5").Text & " " & "Work Order - " & Sheets("WO1").Range("Site").Value
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        With Sourcewb
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OutMail
                .To = ""
                .CC = Sheets("WO1").Range("E54").Value
                .BCC = ""
                .Subject = Sheets("WO1").Range("W5").Value & " Work Order" & " - " & Sheets("WO1").Range("Site").Value
                .Body = "Please find attached work order for  " & Sheets("WO1").Range("W5").Value
                .Attachments.Add Sourcewb.FullName
                .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

    Any help is appreciated.

    Thanks!

  2. #2
    Forum Contributor ragavan.sridar1's Avatar
    Join Date
    11-19-2012
    Location
    India
    MS-Off Ver
    Excel 2010, Excel 2003
    Posts
    208

    Re: Call a beforesave macro in an email macro

    May be you can try like

    Sub Mail_WO()
    activeworkbook.save
    ''' your macro code here..
    end sub
    My guess is that as you already have an before save macro it will go and check it and then proceed with your email macro..

    again this is just a guess...
    Thanks!
    Raga.

    Please,mark your thread [SOLVED] if you received your answer.

    Click the little star * below, to give some Rep if you think an answer deserves it.

    I learnt so many things from these links.

  3. #3
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Re: Call a beforesave macro in an email macro

    Quote Originally Posted by ragavan.sridar1 View Post
    May be you can try like

    Sub Mail_WO()
    activeworkbook.save
    ''' your macro code here..
    end sub
    My guess is that as you already have an before save macro it will go and check it and then proceed with your email macro..

    again this is just a guess...
    Thanks for the quick reply man.

    I have this code already in my mail macro

    With Sourcewb
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OutMail
                .To = ""
                .CC = Sheets("WO1").Range("E54").Value
                .BCC = ""
                .Subject = Sheets("WO1").Range("W5").Value & " Work Order" & " - " & Sheets("WO1").Range("Site").Value
                .Body = "Please find attached work order for  " & Sheets("WO1").Range("W5").Value
                .Attachments.Add Sourcewb.FullName
                .Display   'or use .Send
            End With
            On Error GoTo 0
            .Close SaveChanges:=False
        End With
    It already has a saveas feature in it because it saves it to a temporary new file before emailing out then deleted the file after.

    I've tried using Option Private Module at the top then calling the beforesave macro using the following code:
    Application.Run "ThisWorkbook.Workbook_BeforeSave", Variable1, Variable2
    I tried putting the Activeworkbook.save at the beginning and it starts to seem like it's working. But once it hits the above .saveas code for the mail portion, it seems to ignore the .save and proceeds with the email. Any ideas?

  4. #4
    Forum Contributor ragavan.sridar1's Avatar
    Join Date
    11-19-2012
    Location
    India
    MS-Off Ver
    Excel 2010, Excel 2003
    Posts
    208

    Re: Call a beforesave macro in an email macro

    did you try to add the before save macro as a module code and call it in the macro?

    like
    sub something()
    Dim myArr() As Variant
    Dim i As Integer, x As Integer
    Dim icell As Range, xcell As Range, rng1 As Range, rng2 As Range
    Dim iMsg
    
    myArr = Array("WO1", "WO2", "WO3", "WO4", "WO5")
    
    
    On Error Resume Next
    For i = 0 To 4
        Set rng1 = Union(Sheets(myArr(i)).Range("C26:C33"), Sheets(myArr(i)).Range("I26:I33"), Sheets(myArr(i)).Range("O26:O33"))
        For Each icell In rng1
            Select Case icell.Column
                Case Is = 3
                    If Not IsEmpty(icell) Then
                        If IsEmpty(icell.Offset(0, 1)) Then
                            iMsg = MsgBox("Error.  Please enter Yes/No for 'Customer Spare Part' in cell '" & icell.Offset(0, 1).Address & "' in section A of " & Sheets(myArr(i)).Name & _
                            vbNewLine & "                              Would you like to see the part number?", vbYesNo Or vbExclamation, "Missing Information!")
                            Cancel = True
                        End If
                        If iMsg = vbYes Then
                            MsgBox (icell.Value)
                        End If
                    End If
                Case Is = 9
                    If Not IsEmpty(icell) Then
                        If IsEmpty(icell.Offset(0, 1)) Then
                            iMsg = MsgBox("Error.  Please enter Yes/No for 'Customer Spare Part' in cell '" & icell.Offset(0, 1).Address & "' in section B of " & Sheets(myArr(i)).Name & _
                            vbNewLine & "                              Would you like to see the part number?", vbYesNo Or vbExclamation, "Missing Information!")
                            Cancel = True
                        End If
                        If iMsg = vbYes Then
                            MsgBox (icell.Value)
                        End If
                    End If
                Case Is = 15
                    If Not IsEmpty(icell) Then
                        If IsEmpty(icell.Offset(0, 1)) Then
                            iMsg = MsgBox("Error.  Please enter Yes/No for 'Customer Spare Part' in cell '" & icell.Offset(0, 1).Address & "' in section C of " & Sheets(myArr(i)).Name & _
                            vbNewLine & "                              Would you like to see the part number?", vbYesNo Or vbExclamation, "Missing Information!")
                            Cancel = True
                        End If
                        If iMsg = vbYes Then
                            MsgBox (icell.Value)
                        End If
                    End If
            End Select
        Next icell
    Next i
    
    For x = 0 To 4
    Set rng2 = Union(Sheets(myArr(x)).Range("E17"), Sheets(myArr(x)).Range("K17"), Sheets(myArr(x)).Range("R17"), Sheets(myArr(x)).Range("V17"))
        For Each xcell In rng2
            Select Case xcell.Column
                Case Is = 5
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C38")) Then
                            MsgBox "Please Enter a comment for Section A of " & Sheets(myArr(x)).Name
                            Cancel = True
                        End If
                    End If
                Case Is = 11
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C42")) Then
                            MsgBox "Please Enter a comment for Section B of " & Sheets(myArr(x)).Name
                            Cancel = True
                        End If
                    End If
                Case Is = 18
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C46")) Then
                            MsgBox "Please Enter a comment for Section C of " & Sheets(myArr(x)).Name
                            Cancel = True
                        End If
                    End If
                Case Is = 22
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C50")) Then
                            MsgBox "Please enter a comment for section D of " & Sheets(myArr(x)).Name
                            Cancel = True
                        End If
                    End If
            End Select
        Next xcell
    Next x
                    
    End Sub
    and call this in your mail macro??

  5. #5
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Re: Call a beforesave macro in an email macro

    Tried it and it still didn't work

    Also tried this:
    Sub Mail_WO()
    Activeworkbook.save
    On Error GoTo Mail_WO_err
    
    
    .....mail code here.....
    
    Mail_WO_err:
      MsgBox "Please fill out all required data before Printing.", vbExclamation, "Error!"
     Exit Sub
    This still didn't work. It seems to run through the beforesave code and displays the proper message boxes for fields missing but then once it gets to this point:
     With Sourcewb
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OutMail
                .To = ""
                .CC = Sheets("WO1").Range("E54").Value
                .BCC = ""
                .Subject = Sheets("WO1").Range("W5").Value & " Work Order" & " - " & Sheets("WO1").Range("Site").Value
                .Body = "Please find attached work order for  " & Sheets("WO1").Range("W5").Value
                .Attachments.Add Sourcewb.FullName
                .Display   'or use .Send
            End With
            On Error GoTo 0
            .Close SaveChanges:=False
        End With
    It's like this portion of the code ignores anything else in the macro. I need to add something right before that .saveas i think. Any ideas?

  6. #6
    Forum Contributor ragavan.sridar1's Avatar
    Join Date
    11-19-2012
    Location
    India
    MS-Off Ver
    Excel 2010, Excel 2003
    Posts
    208

    Re: Call a beforesave macro in an email macro

    do you want the users to enter all the required/correct values and then proceed to email?

  7. #7
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Re: Call a beforesave macro in an email macro

    Quote Originally Posted by ragavan.sridar1 View Post
    do you want the users to enter all the required/correct values and then proceed to email?
    Yes that is the intent. Just like how right now if they are missing data when they go to Save the workbook, It will not save until all required data is entered. I want that to be the same with emailing. I don't know if it's giving me trouble because i'm saving to to a temporary file path & name - would that prevent the code from running properly? I have no idea. Stumped on this.

    Edit:

    Okay, i figured it out but now i have another issue which may be easier to address.
    Here is my code:
    Sub Mail_WO()
    'Working in 2000-2010
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim Sourcewb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim OutApp As Object
        Dim OutMail As Object
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        Set Sourcewb = ActiveWorkbook
    
        'Determine the Excel version and file extension/format
        With Sourcewb
            If Val(Application.Version) < 12 Then
                'You use Excel 2000-2003
                FileExtStr = ".xls": FileFormatNum = -4143
            Else
                'You use Excel 2007-2010, we exit the sub when your answer is
                'NO in the security dialog that you only see  when you copy
                'an 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
    
    
        'Save the new workbook/Mail it/Delete it
        TempFilePath = Environ$("temp") & "\"
        TempFileName = Sheets("WO1").Range("E10").Value & " - " & Sheets("WO1").Range("W5").Text & " " & "Work Order - " & Sheets("WO1").Range("Site").Value
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
        With Sourcewb
        .Save
        On Error GoTo Mail_WO_err
        
    Mail_WO_err:
        MsgBox "Please fill out all required data before Printing.", vbExclamation, "Error!"
    Exit Sub
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            With OutMail
                .To = ""
                .CC = Sheets("WO1").Range("E54").Value
                .BCC = ""
                .Subject = Sheets("WO1").Range("W5").Value & " Work Order" & " - " & Sheets("WO1").Range("Site").Value
                .Body = "Please find attached work order for  " & Sheets("WO1").Range("W5").Value
                .Attachments.Add Sourcewb.FullName
                .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
    The bold is what i added to make this work. However, if the user forgets to put a value and hits the Mail button, It won't mail it and will give the error message but then it seems like all my macros and code just die and stop working. So the next time the user hits the Mail button (even if they are missing required data) it will send. The only way to fix this is to physically close excel (not just that workbook). If i have other spreadsheets going at the same time, i have to close those as well. Any way around this? Am i missing something like a Goto or Resume somewhere?

    Edit: HAHA. Fixed and solved. Ended up moving the bold portion right below the sub Mail_WO() before i defined my variables. It attempts to save it and if there is anything missing it exits the sub right there.

    Thanks for the help ragavan.
    Last edited by Spritz; 05-03-2013 at 01:04 PM. Reason: Solved

  8. #8
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Re: Call a beforesave macro in an email macro

    Alright after playing around with this more i found out that it prevents mailing even if all required values are entered. I have it checking for an Error and if there is one it calls that exit sub routine. The only thing is, a message box isn't an error (correct me if i'm wrong) I'm not really sure how it is finding an error? but it does display the message box i have in the exit sub routine in bold.


  9. #9
    Forum Contributor ragavan.sridar1's Avatar
    Join Date
    11-19-2012
    Location
    India
    MS-Off Ver
    Excel 2010, Excel 2003
    Posts
    208

    Re: Call a beforesave macro in an email macro

    i've added both the codes together, when the details are not entered properly then it will ask the user to prompt whether they have entered the value incorrectly, if yes, it will end the macro else proceed to mailing.

    Sub Mail_WO()
    'Working in 2000-2010
    
    
    Dim myArr() As Variant
    Dim i As Integer, x As Integer
    Dim icell As Range, xcell As Range, rng1 As Range, rng2 As Range
    Dim iMsg
    
    myArr = Array("WO1", "WO2", "WO5")
    
    
    On Error Resume Next
    For i = 0 To 4
        Set rng1 = Union(Sheets(myArr(i)).Range("C26:C33"), Sheets(myArr(i)).Range("I26:I33"), Sheets(myArr(i)).Range("O26:O33"))
        For Each icell In rng1
            Select Case icell.Column
                Case Is = 3
                    If Not IsEmpty(icell) Then
                        If IsEmpty(icell.Offset(0, 1)) Then
                            iMsg = MsgBox("Error.  Please enter Yes/No for 'Customer Spare Part' in cell '" & icell.Offset(0, 1).Address & "' in section A of " & Sheets(myArr(i)).Name & _
                            vbNewLine & "                              Would you like to see the part number?", vbYesNo Or vbExclamation, "Missing Information!")
                            Cancel = True
                        End If
                        If iMsg = vbYes Then
                            MsgBox (icell.Value)
                        End If
                    End If
                Case Is = 9
                    If Not IsEmpty(icell) Then
                        If IsEmpty(icell.Offset(0, 1)) Then
                            iMsg = MsgBox("Error.  Please enter Yes/No for 'Customer Spare Part' in cell '" & icell.Offset(0, 1).Address & "' in section B of " & Sheets(myArr(i)).Name & _
                            vbNewLine & "                              Would you like to see the part number?", vbYesNo Or vbExclamation, "Missing Information!")
                            Cancel = True
                        End If
                        If iMsg = vbYes Then
                            MsgBox (icell.Value)
                        End If
                    End If
                Case Is = 15
                    If Not IsEmpty(icell) Then
                        If IsEmpty(icell.Offset(0, 1)) Then
                            iMsg = MsgBox("Error.  Please enter Yes/No for 'Customer Spare Part' in cell '" & icell.Offset(0, 1).Address & "' in section C of " & Sheets(myArr(i)).Name & _
                            vbNewLine & "                              Would you like to see the part number?", vbYesNo Or vbExclamation, "Missing Information!")
                            Cancel = True
                        End If
                        If iMsg = vbYes Then
                            MsgBox (icell.Value)
                        End If
                    End If
            End Select
        Next icell
    Next i
    
    For x = 0 To 4
    Set rng2 = Union(Sheets(myArr(x)).Range("E17"), Sheets(myArr(x)).Range("K17"), Sheets(myArr(x)).Range("R17"), Sheets(myArr(x)).Range("V17"))
        For Each xcell In rng2
            Select Case xcell.Column
                Case Is = 5
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C38")) Then
                            MsgBox "Please Enter a comment for Section A of " & Sheets(myArr(x)).Name
                            Cancel = True
                        End If
                    End If
                Case Is = 11
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C42")) Then
                            MsgBox "Please Enter a comment for Section B of " & Sheets(myArr(x)).Name
                            Cancel = True
                        End If
                    End If
                Case Is = 18
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C46")) Then
                            MsgBox "Please Enter a comment for Section C of " & Sheets(myArr(x)).Name
                            Cancel = True
                        End If
                    End If
                Case Is = 22
                    If Not IsEmpty(xcell) Then
                        If IsEmpty(Sheets(myArr(x)).Range("C50")) Then
                            MsgBox "Please enter a comment for section D of " & Sheets(myArr(x)).Name
                            Cancel = True
                            Exit Sub
                        End If
                    End If
            End Select
        Next xcell
    Next x
    
    If MsgBox("have to entered any data incorrectly?", vbYesNo) = vbYes Then
    Exit Sub
    Else
    End If
    
    
    
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim Sourcewb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim OutApp As Object
        Dim OutMail As Object
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        Set Sourcewb = ActiveWorkbook
    
        'Determine the Excel version and file extension/format
        With Sourcewb
            If Val(Application.Version) < 12 Then
                'You use Excel 2000-2003
                FileExtStr = ".xls": FileFormatNum = -4143
            Else
                'You use Excel 2007-2010, we exit the sub when your answer is
                'NO in the security dialog that you only see  when you copy
                'an 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
    
    
        'Save the new workbook/Mail it/Delete it
        TempFilePath = Environ$("temp") & "\"
        TempFileName = Sheets("WO1").Range("E10").Value & " - " & Sheets("WO1").Range("W5").Text & " " & "Work Order - " ' & Sheets("WO1").Range("Site").Value
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
        With Sourcewb
        .Save
       ' On Error GoTo Mail_WO_err
        
    'Mail_WO_err:
     '   MsgBox "Please fill out all required data before Printing.", vbExclamation, "Error!"
    'Exit Sub
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            With OutMail
                .To = ""
                .CC = Sheets("WO1").Range("E54").Value
                .BCC = ""
                .Subject = Sheets("WO1").Range("W5").Value & " Work Order" & " - " '& Sheets("WO1").Range("Site").Value
                .Body = "Please find attached work order for  " & Sheets("WO1").Range("W5").Value
                .Attachments.Add Sourcewb.FullName
                .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

  10. #10
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Re: Call a beforesave macro in an email macro

    Thanks Ragavan.

    That is exactly what i did. I just copied the code from each instance i needed it for (pdf, email, print) and it seems to be working perfectly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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