Results 1 to 4 of 4

New to VBA - VBA Before Save & Before Email

Threaded View

giggles2005 New to VBA - VBA Before Save... 05-10-2020, 06:49 AM
LJMetzger Re: New to VBA - VBA Before... 07-02-2020, 02:49 PM
giggles2005 Re: New to VBA - VBA Before... 07-28-2020, 05:52 AM
LJMetzger Re: New to VBA - VBA Before... 07-28-2020, 09:00 AM
  1. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: New to VBA - VBA Before Save & Before Email

    Hi,

    The workbook was blank because the file was never saved. I thought you were going to do that manually. You had this code in the file (which I did not touch) that prevented the file from being saved unless VBA was disabled (Application.EnableEvents = False).
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    'If the six specified cells do not contain data, then display a message box with 'and cancel the attempt to save.
    '"If worksheetfunction.CountA(Worksheets("Sheet1").Range(C40,D40,E40,F40,G40,H40") ) < 6 Then MsbBox "Worrkbook will not be saved unless" & vbCrlf & _"
    '"All required fields have been filled in"
    Cancel = True
    
    End Sub
    'Exit If
    I updated the file, to change the code so the file can be saved when all the fields are filled in. Changes in red:
    In the ThisWorkbook Code Module:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      
      'Verify that required Inputs are NOT BLANK
      'Display an Error Message and Exit if they are BLANK
      sErrorMessage = VerifyInputs()
      If Len(sErrorMessage) > 0 Then
        Cancel = True
        MsgBox sErrorMessage
        Exit Sub
      End If
      
    End Sub

    In the Sheet PO Request Form Code Module:
    Option Explicit
    
    Private Sub CommandButton1_Click()
    
      Dim OutlookApp As Object
      Dim OutlookMail As Object
      
      Dim sErrorMessage As String
      
      'Verify that required Inputs are NOT BLANK
      'Display an Error Message and Exit if they are BLANK
      sErrorMessage = VerifyInputs()
      If Len(sErrorMessage) > 0 Then
        MsgBox sErrorMessage
        Exit Sub
      End If
      
      'Save this file
      ActiveWorkbook.Save
      
      Set OutlookApp = CreateObject("Outlook.Application")
      Set OutlookMail = OutlookApp.CreateItem(0)
      
      With OutlookMail
        .To = "giggles2005-2006@hotmail.co.uk"
        .Subject = "PO Request"
        .Body = " Hi Team, please can you raise the attached?"
        .attachments.Add ActiveWorkbook.FullName
        .Display
    End With
    
      Set OutlookMail = Nothing
      Set OutlookApp = Nothing
    
    End Sub
    The Ordinary Code module that verified inputs had no change.

    Lewis

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 5
    Last Post: 04-29-2020, 11:12 AM
  2. [SOLVED] Rule to file email in email folder, save the attachment to desk top and update spreadsheet
    By JET2011 in forum Outlook Formatting & Functions
    Replies: 49
    Last Post: 08-29-2018, 12:49 PM
  3. [VBA] Save Range as picture, save to file, attach to email
    By Armitage2k in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-23-2018, 07:34 AM
  4. Save Email into specific folder with file name from body of email
    By hudson andrew in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-21-2016, 02:27 PM
  5. Macro - Save Word as PDF with Unique Name and Email PDF to specified email address.
    By newbie1234 in forum Word Programming / VBA / Macros
    Replies: 6
    Last Post: 07-08-2014, 11:54 PM
  6. Replies: 0
    Last Post: 11-22-2012, 08:42 AM
  7. edit, save as new and email to multiple email addresses
    By murphyx232 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-20-2007, 02:37 PM

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