VBA - Command Button - Save file and reset information

Nadine1988

Board Regular
Joined
Jun 12, 2023
Messages
62
Office Version
  1. 365
Platform
  1. Windows
Hello all,

so I do have a command button in my excel which first checks a few fields (they're not supposed to be empty and if there are an automated message box is created) and secondly sends the file attached to an email

I now would like to extend the function of this message box - so after the fields are checked BUT BEFORE it get's sent per email I would like the file to be saved automatically with a new file name.

Below you can see the current code. Any ideas?

VBA Code:
Private Sub CommandButton1_Click()
Dim xOutlookObj As Object
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    On Error Resume Next
   
    If IsEmpty(Range("A7")) Then
    MsgBox "Enter date"
    GoTo ends
   
    Else
    If IsEmpty(Range("D7")) Then
    MsgBox "Enter Vizrt sales peson"
    GoTo ends
       
    Else
    If IsEmpty(Range("C9")) Then
    MsgBox "Enter the start date of your rental"
    GoTo ends
 
    Else
    If IsEmpty(Range("C11")) Then
    MsgBox "Enter the end date of your rental"
    GoTo ends
   

End If
End If
End If
End If

   
   
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Please add any special requirements here. For FOC rentals - please attach Vanessa's approval to your email." & vbNewLine & vbNewLine & _
              "" & vbNewLine & _
              ""
                  On Error Resume Next
    With xOutMail
        .To = "logisticaustria@vizrt.com"
        .CC = ""
        .BCC = ""
        .Subject = "Demopool Request_"
        .Body = xMailBody
        .Attachments.Add ActiveWorkbook.FullName
        .Display   'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
   
ends:
     
     
End Sub

thanks for your help!
best
nadine
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try something like the followiing:
VBA Code:
Private Sub CommandButton1_Click()
Dim xOutlookObj As Object
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    Dim rng As Range
    Dim strmsg As String
    Dim arrymsg()
    Dim i As Long
    On Error Resume Next
   
    Set rng = Range("A7,D7,C9,C11")
    strmsg = ""
    arrymsg = Array("Enter date", "Enter Vizrt sales person", "Enter the start date of your rental", "Enter the end date of your rental")
    For i = 1 To 4
      If IsEmpty(rng(1, i)) Then strmsg = strmsg & vbLf & arrymsg(i - 1)
    Next i
    If Len(strmsg) > 1 Then
      strmsg = Mid(strmsg, 2, Len(strmsg))
      MsgBox strmsg
      GoTo ends
    End If

    'change the path and filename below.
    ActiveWorkbook.SaveAs Filename:= _
        "C:\path\......\filename.xlsm", FileFormat:= _
        xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
        
    'code to send emails goes here
    '   ...
    
ends:
End Sub
 
Upvote 0
Guten Morgen,

das funktioniert leider nicht. Wenn in den Code kopiere bekomme ich in den Felden A7, D7,... eine Fehlermeldung obwohl ich etwas eingetrage habe. Ich weiß sowieso nicht, warum der Code oben geändert werden muss, dieser funktioniert ja. Es geht lediglich um das zusätzliche Speichern.

Danke
Lg
Nadine
 
Upvote 0
Oh i'm so sorry - totally forgot about writing in English.
Soo the solution suggested is not working. I receive an error message saying the the fields are empty when I copy the code into my file for the fields A7, D7, C9, and C11.
I actually don't know why the previous code was changed that much as it actually did work - I only need an additional code for saving the file.

Thank you for your help - really appreciated!
@Nadine1988 please post to this forum in English only. If you wish to post in your own language then you need to post in the Questions in Other Languages section.
Thanks

VBA Code:
Private Sub CommandButton1_Click()
Dim xOutlookObj As Object
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    Dim rng As Range
    Dim strmsg As String
    Dim arrymsg()
    Dim i As Long
    On Error Resume Next
  
    Set rng = Range("A7,D7,C9,C11")
    strmsg = ""
    arrymsg = Array("Enter date", "Enter Vizrt sales person", "Enter the start date of your rental", "Enter the end date of your rental")
    For i = 1 To 4
      If IsEmpty(rng(1, i)) Then strmsg = strmsg & vbLf & arrymsg(i - 1)
    Next i
    If Len(strmsg) > 1 Then
      strmsg = Mid(strmsg, 2, Len(strmsg))
      MsgBox strmsg
      GoTo ends
    End If

    'change the path and filename below.
    ActiveWorkbook.SaveAs Filename:= _
        "C:\path\......\filename.xlsm", FileFormat:= _
        xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
       
    'code to send emails goes here
    '   ...
   
ends:
End Sub
 
Upvote 0
Hello everyone - any other suggestions on how to fix this?
Thanks
Nadine
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,988
Members
449,093
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top