SOLVED

VBA - Sending Active Sheet by email

%3CLINGO-SUB%20id%3D%22lingo-sub-2996814%22%20slang%3D%22fr-FR%22%3EVBA%20-%20Sending%20Active%20Sheet%20by%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2996814%22%20slang%3D%22fr-FR%22%3E%3CP%3EHi%20everyone!%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20struggling%20with%20my%20VBA%20code%2C%20I%20dont%20understand%20why%20it%20doesn't%20work.%20You%20have%20to%20know%20that%20I%20am%20not%20very%20good%20with%20VBA%20coding%2C%20just%20trying%20to%20implement%20small%20things%20for%20my%20company.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20goal%20is%20to%20send%20an%20active%20sheet%20by%20email%20to%20our%20concerned%20customer%20by%20clicking%20on%20a%20button.%20On%20the%20sheet%20you%20can%20find%20the%20customer%20email%20address%20in%20D5%2C%20the%20object%20in%20A2%2C%20the%20filename%20in%20G7%20and%20cc%20in%20H1.%26nbsp%3B%3CBR%20%2F%3EOnce%20the%20button%20is%20triggered%2C%20a%20pop%20up%20ask%20if%20I%20want%20to%20send%20the%20sheet%20or%20all%20the%20workbook.%20When%20I%20select%20%22workbook%22%20the%20code%20works%20fine.%20The%20problem%20is%20when%20I%20select%20the%20active%20sheet%2C%20it%20creates%20a%20new%20workbook%20with%20only%20the%20active%20sheet%20(which%20is%20what%20I%20want)%2C%20but%20struggle%20to%20save%20it%20and%20give%20it%20the%20write%20filename%20(which%20should%20be%20G7).%20Actually%2C%20excel%20creates%20a%20new%20workbook%20called%20%22book%22%20and%20a%20number%20instead%20of%20the%20filename%20I%20would%20want%2C%20and%20doesn't%20save%20it.%20Excel%20Debug%20mode%20says%20the%20problem%20come%20from%20this%20line%20%3A%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23FF0000%22%3EWB.SaveAs%20FileName%3A%3D%22e%3A%5C%22%20%26amp%3B%20FileName%20%3CFONT%20color%3D%22%23000000%22%3E%20which%20I%20highlighted%20in%20red%20in%20the%20code%20bellow.%26nbsp%3B%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20the%20code%20(I%20haven't%20made%20it%20myself%2C%20found%20it%20from%20the%20internet%2C%20but%20doesn't%20work%20for%20me.%20I%20tried%20quite%20a%20few%20things%20but%20I%20really%20dont%20understand%20what's%20not%20going%20on)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20someone%20please%20help%20me%20%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EThanks%20a%20lot%2C%20whish%20you%20a%20nice%20day!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20Button2()%20%3CBR%20%2F%3E%20'Variable%20declaration%20%3CBR%20%2F%3E%20'Outlook%20application%20%3CBR%20%2F%3E%20Dim%20oApp%20As%20Object%20%3CBR%20%2F%3E%20'Outlook%20MailItem%20%3CBR%20%2F%3E%20Dim%20oMail%20As%20Object%20%3CBR%20%2F%3E%20Dim%20WB%20As%20Workbook%20%3CBR%20%2F%3E%20Dim%20WS%20As%20Worksheet%20%3CBR%20%2F%3E%20Dim%20FileName%20As%20String%20%3CBR%20%2F%3E%20Dim%20SendSheetOnly%20As%20Boolean%20%3CBR%20%2F%3E%20'Turn%20off%20screen%20updating%20%3CBR%20%2F%3E%20Application.ScreenUpdating%20%3D%20False%20%3CBR%20%2F%3E%20'Ask%20the%20user%20what%20to%20send%20%3CBR%20%2F%3E%20Select%20Case%20MsgBox(%20_%20%3CBR%20%2F%3E%20Prompt%3A%3D%22Do%20you%20want%20to%20send%20the%20active%20sheet%20only%3F%22%2C%20_%20%3CBR%20%2F%3E%20Buttons%3A%3DvbYesNoCancel)%20%3CBR%20%2F%3E%20Case%20vbYes%20%3CBR%20%2F%3E%20'Yes%2C%20make%20a%20copy%20of%20the%20active%20sheet.%3CBR%20%2F%3ESendSheetOnly%20%3D%20True%20%3CBR%20%2F%3E%20ActiveSheet.Copy%20%3CBR%20%2F%3E%20Set%20WB%20%3D%20ActiveWorkbook%20%3CBR%20%2F%3E%20'Save%20without%20formulas%3F%3CBR%20%2F%3ECells.Copy%20%3CBR%20%2F%3E%20Cells.PasteSpecial%20Paste%3A%3DxlPasteValues%20%3CBR%20%2F%3E%20'Clear%20the%20clipboard%20%3CBR%20%2F%3E%20Application.CutCopyMode%20%3D%20False%20%3CBR%20%2F%3E%20FileName%20%3D%20Cells(7%2C%20%22G%22).Value%20%26amp%3B%20%22.xls%22%3C%2FP%3E%3CP%3E'Continue%20when%20error%20occurs%20%3CBR%20%2F%3E%20On%20Error%20Resume%20Next%20Kill%20%3CBR%20%2F%3E%20%22e%3A%5C%22%20%26amp%3B%20FileName%20%3CBR%20%2F%3E%20On%20Error%20GoTo%200%20%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3E%20WB.SaveAs%20FileName%3A%3D%22e%3A%5C%22%20%26amp%3B%20FileName%20%3C%2FFONT%3E%3CBR%20%2F%3E%20Case%20vbNo%20%3CBR%20%2F%3E%20If%20Len(ActiveWorkbook.Path)%20%3D%200%20Then%20%3CBR%20%2F%3E%20'File%20hasn't%20been%20saved%20before%20%3CBR%20%2F%3E%20MsgBox%20%22Can't%20send%20an%20unsaved%20workbook%22%2C%20vbCritical%20%3CBR%20%2F%3E%20GoTo%20exiting%20%3CBR%20%2F%3E%20End%20If%20%3CBR%20%2F%3E%20SendSheetOnly%20%3D%20False%20%3CBR%20%2F%3E%20Set%20WB%20%3D%20ActiveWorkbook%20%3CBR%20%2F%3E%20Case%20vbCancel%20%3CBR%20%2F%3E%20'olDiscard%20%3D%201%20%3CBR%20%2F%3E%20oMail.Close%20SaveMode%3A%3D1%20%3CBR%20%2F%3E%20GoTo%20exiting%20%3CBR%20%2F%3E%20End%20Select%3C%2FP%3E%3CP%3E%3CBR%20%2F%3ESet%20oApp%20%3D%20CreateObject(%22Outlook.Application%22)%20%3CBR%20%2F%3E%20Set%20oMail%20%3D%20oApp.CreateItem(0)%20'olMailItem%20%3D%200%20%3CBR%20%2F%3E%20With%20oMail%20%3CBR%20%2F%3E%20'User%20input%20To%20property%20%3CBR%20%2F%3E%20.To%20%3D%20Range(%22D5%22).Value%20%3CBR%20%2F%3E%20'User%20input%20CC%20property%20%3CBR%20%2F%3E%20.%20CC%20%3D%20Range(%22H1%22).Value%20%3CBR%20%2F%3E%20'Hard%20code%20Read%20Receipt%20Requested%20property%20%3CBR%20%2F%3E%20'.ReadReceiptRequested%20%3D%20True%20%3CBR%20%2F%3E%20'User%20input%20Read%20Receipt%20Requested%20property%20%3CBR%20%2F%3E%20'.ReadReceiptRequested%20%3D%20Range(%22B3%22).Value%20%3CBR%20%2F%3E%20'User%20input%20Subject%20property%20%3CBR%20%2F%3E%20.Subject%20%3D%20Range(%22A2%22).Value%20%3CBR%20%2F%3E%20'User%20input%20Body%20property%20%3CBR%20%2F%3E%20.Body%20%3D%20Range(%22e5%22).Value%20%3CBR%20%2F%3E%20'Hard%20code%20Sensitivity%20property%20%3CBR%20%2F%3E%20.Sensitivity%20%3D%203%20'olConfidential%20%3D%203%20%3CBR%20%2F%3E%20'Set%20attachment%20%3CBR%20%2F%3E%20.Attachments.Add%20WB.FullName%20%3CBR%20%2F%3E%20'Hard%20code%20importance%20%3CBR%20%2F%3E%20''olImportanceHigh%20%3D2%20%3CBR%20%2F%3E%20.Importance%20%3D%202%20%3CBR%20%2F%3E%20'Send%20directly%20(remove%20apostrophe%20on%20line%20below%20to%20activate)%20%3CBR%20%2F%3E%20'.Send%20%3CBR%20%2F%3E%20'Display%20it%20%3CBR%20%2F%3E%20.Display%20%3CBR%20%2F%3E%20End%20With%20%3CBR%20%2F%3E%20If%20SendSheetOnly%20Then%20%3CBR%20%2F%3E%20WB.ChangeFileAccess%20Mode%3A%3DxlReadOnly%20%3CBR%20%2F%3E%20Kill%20WB.FullName%20%3CBR%20%2F%3E%20WB.Close%20SaveChanges%3A%3DFalse%20%3CBR%20%2F%3E%20End%20If%20%3CBR%20%2F%3E%20exiting%3A%20%3CBR%20%2F%3E%20'Restore%20screen%20updating%20%3CBR%20%2F%3E%20Application.ScreenUpdating%20%3D%20True%20%3CBR%20%2F%3E%20'destroy%20variables%20and%20restore%20memory%20%3CBR%20%2F%3E%20Set%20oMail%20%3D%20Nothing%20%3CBR%20%2F%3E%20Set%20oApp%20%3D%20Nothing%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2996814%22%20slang%3D%22fr-FR%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2997174%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20Sending%20Active%20Sheet%20by%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2997174%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1227203%22%20target%3D%22_blank%22%3E%40YannDV%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethis%20might%20work%20for%20you%3A%20try%20moving%20this%20line%3A%26nbsp%3B%3CSPAN%3ESet%20WB%20%3D%20ActiveWorkbook%20before%20your%20error%20line%20when%20debugging.%20perhaps%20to%20the%20beginning%26nbsp%3Bof%20the%20code.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2997242%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20Sending%20Active%20Sheet%20by%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2997242%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1227203%22%20target%3D%22_blank%22%3E%40YannDV%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDo%20you%20really%20want%20to%20save%20the%20workbook%20as%20an%20Excel%2097-2003%20workbook%3F%20If%20so%2C%20change%20the%20offending%20line%20to%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWB.SaveAs%20FileName%3A%3D%22e%3A%5C%22%20%26amp%3B%20FileName%2C%20FileFormat%3A%3DxlExcel8%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2997247%22%20slang%3D%22fr-FR%22%3ERe%3A%20VBA%20-%20Sending%20Active%20Sheet%20by%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2997247%22%20slang%3D%22fr-FR%22%3EHi%20Josh%2C%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20Thanks%20for%20your%20help.%20Unfortunately%2C%20it%20did%20not%20change%20anything%2C%20still%20get%20an%20error%20at%20the%20same%20line...%20%3CLI-EMOJI%20id%3D%22lia_confused-face%22%20title%3D%22%3Aconfused_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2997299%22%20slang%3D%22fr-FR%22%3ERe%3A%20VBA%20-%20Sending%20Active%20Sheet%20by%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2997299%22%20slang%3D%22fr-FR%22%3EThanks%20I%20changed%20it%20%3CLI-EMOJI%20id%3D%22lia_slightly-smiling-face%22%20title%3D%22%3Aslightly_smiling_face%3A%22%3E%3C%2FLI-EMOJI%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20Unfortunately%2C%20same%20problem%20still%20occurs%20%3CLI-EMOJI%20id%3D%22lia_disappointed-face%22%20title%3D%22%3Adisappointed_face%3A%22%3E%3C%2FLI-EMOJI%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20I%20m%20going%20to%20add%20the%20error%20message%20in%20my%20main%20text%2C%20maybe%20the%20problem%20can%20be%20point%20out%20more%20easily%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi everyone! 

 

I am struggling with my VBA code, I dont understand why it doesn't work. You have to know that I am not very good with VBA coding, just trying to implement small things for my company. 

 

My goal is to send an active sheet by email to our concerned customer by clicking on a button. On the sheet you can find the customer email address in D5, the object in A2, the filename in G7 and cc in H1. 
Once the button is triggered, a pop up ask if I want to send the sheet or all the workbook. When I select "workbook" the code works fine. The problem is when I select the active sheet, it creates a new workbook with only the active sheet (which is what I want), but struggle to save it and give it the write filename (which should be G7). Actually, excel creates a new workbook called "book" and a number instead of the filename I would want, and doesn't save it. Excel Debug mode says the problem come from this line : 

WB.SaveAs FileName:="e:\" & FileName which I highlighted in red in the code bellow. 

 

 

This is the code (I haven't made it myself, found it from the internet, but doesn't work for me. I tried quite a few things but I really dont understand what's not going on) 

 

This is my error message :

helpvba.PNG

When I launch the macro, as it create and open the new workbook with the active sheet, it is open (but with the name book 1)

 

Can someone please help me ? 


Thanks a lot, whish you a nice day!

 

Sub Button2()
'Variable declaration
'Outlook application
Dim oApp As Object
'Outlook MailItem
Dim oMail As Object
Dim WB As Workbook
Dim WS As Worksheet
Dim FileName As String
Dim SendSheetOnly As Boolean
'Turn off screen updating
Application.ScreenUpdating = False
'Ask the user what to send
Select Case MsgBox( _
Prompt:="Do you want to send the active sheet only ?", _
Buttons:=vbYesNoCancel)
Case vbYes
'Yes, make a copy of the active sheet.
SendSheetOnly = True
ActiveSheet.Copy
Set WB = ActiveWorkbook
'Save without formulas ?
Cells.Copy
Cells.PasteSpecial Paste:=xlPasteValues
'Clear the clipboard
Application.CutCopyMode = False
FileName = Cells(7, "G").Value & ".xls"

'Continue when error occurs
On Error Resume Next
Kill "e:\" & FileName
On Error GoTo 0
WB.SaveAs FileName:="e:\" & FileName
Case vbNo
If Len(ActiveWorkbook.Path) = 0 Then
'File hasn't been saved before
MsgBox "Can't send an unsaved workbook", vbCritical
GoTo exiting
End If
SendSheetOnly = False
Set WB = ActiveWorkbook
Case vbCancel
'olDiscard = 1
oMail.Close SaveMode:=1
GoTo exiting
End Select


Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0) 'olMailItem = 0
With oMail
'User input To property
.To = Range("D5").Value
'User input CC property
.CC = Range("H1").Value
'Hard code Read Receipt Requested property
'.ReadReceiptRequested = True
'User input Read Receipt Requested property
'.ReadReceiptRequested = Range("B3").Value
'User input Subject property
.Subject = Range("A2").Value
'User input Body property
.Body = Range("e5").Value
'Hard code Sensitivity property
.Sensitivity = 3 'olConfidential = 3
'Set attachment
.Attachments.Add WB.FullName
'Hard code importance
''olImportanceHigh = 2
.Importance = 2
'Send directly (remove apostrophe on line below to activate)
'.Send
'Display it
.Display
End With
If SendSheetOnly Then
WB.ChangeFileAccess Mode:=xlReadOnly
Kill WB.FullName
WB.Close SaveChanges:=False
End If
exiting:
'Restore screen updating
Application.ScreenUpdating = True
'destroy variables and restore memory
Set oMail = Nothing
Set oApp = Nothing

End Sub

8 Replies

@YannDV 

this might work for you: try moving this line: Set WB = ActiveWorkbook before your error line when debugging. perhaps to the beginning of the code.

@YannDV 

Do you really want to save the workbook as an Excel 97-2003 workbook? If so, change the offending line to

 

WB.SaveAs FileName:="e:\" & FileName, FileFormat:=xlExcel8

Hi Josh,

Thanks for your help. Unfortunately, it did not change anything, still get an error at the same line...
Thanks I changed it

Unfortunately, same problem still occurs

I m going to add the error message in my main text, maybe the problem can be point out more easily

@YannDV 

What is the error message?

I added it in the main question with a screen shot
best response confirmed by YannDV (Occasional Contributor)
Solution

@YannDV 

try replacing your whole code with this

 

Sub Button2()
'Variable declaration
'Outlook application
Dim oApp As Object
'Outlook MailItem
Dim oMail As Object
Dim WB As Workbook
Dim WS As Worksheet
Dim FileName As String
Dim SendSheetOnly As Boolean
'Turn off screen updating
Application.ScreenUpdating = False
'Ask the user what to send
Select Case MsgBox( _
Prompt:="Do you want to send the active sheet only ?", _
Buttons:=vbYesNoCancel)
Case vbYes
'Yes, make a copy of the active sheet.
SendSheetOnly = True
ActiveSheet.Copy
Set WB = ActiveWorkbook
'Save without formulas ?
Cells.Copy
Cells.PasteSpecial Paste:=xlPasteValues
'Clear the clipboard
Application.CutCopyMode = False
FileName = ".xls"

'Continue when error occurs
On Error Resume Next
Kill "e:\" & FileName
On Error GoTo 0
WB.SaveAs FileName:=Range("G7").value & FileName
Case vbNo
If Len(ActiveWorkbook.Path) = 0 Then
'File hasn't been saved before
MsgBox "Can't send an unsaved workbook", vbCritical
GoTo exiting
End If
SendSheetOnly = False
Set WB = ActiveWorkbook
Case vbCancel
'olDiscard = 1
oMail.Close SaveMode:=1
GoTo exiting
End Select


Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0) 'olMailItem = 0
With oMail
'User input To property
.To = Range("D5").value
'User input CC property
.CC = Range("H1").value
'Hard code Read Receipt Requested property
'.ReadReceiptRequested = True
'User input Read Receipt Requested property
'.ReadReceiptRequested = Range("B3").Value
'User input Subject property
.Subject = Range("A2").value
'User input Body property
.Body = Range("e5").value
'Hard code Sensitivity property
.Sensitivity = 3 'olConfidential = 3
'Set attachment
.Attachments.Add WB.FullName
'Hard code importance
''olImportanceHigh = 2
.Importance = 2
'Send directly (remove apostrophe on line below to activate)
'.Send
'Display it
.Display
End With
If SendSheetOnly Then
WB.ChangeFileAccess Mode:=xlReadOnly
Kill WB.FullName
WB.Close SaveChanges:=False
End If
exiting:
'Restore screen updating
Application.ScreenUpdating = True
'destroy variables and restore memory
Set oMail = Nothing
Set oApp = Nothing

End Sub

 

Thank you sooooo much, it works! Made my day!
Have a nice one too!