Solved

Email current Active Sheet from Excel 2010

Posted on 2015-02-19
8
72 Views
Last Modified: 2016-02-12
Hi Experts,

I would like a script/marco I can use to email the current active worksheet from excel 2010 via Outlook 2010.

I have the attached script that I used regulally for many years but I dont think it is compatible with 2010.  The attachment should be the name of the worksheet.

can you help bring my script into the future?

Thank you
Sub Mail_ActiveSheet()
' Works in Excel 97 through Excel 2007.
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
 
With Application
  .ScreenUpdating = False
  .EnableEvents = False
End With
 
Set Sourcewb = ActiveWorkbook
' Using ActiveSheet.Copy creates a new workbook with
' the sheet and the file format is the same as the
' original workbook.
' Copy the worksheet to a new workbook.
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
 
' Determine the Excel version and file extension/format.
With Destwb
  If Val(Application.Version) < 12 Then
     ' You are using Excel 97-2003.
     FileExtStr = ".xls": FileFormatNum = -4143
  Else
     ' You are using Excel 2007.
     ' When you use ActiveSheet.Copy to create a workbook,
     ' you are prompted with a security dialog. If you click No
     ' in the dialog, then the name of Sourcewb is the same
     ' as Destwb and you exit the subroutine. You only see this
     ' dialog when you attempt to copy a worksheet from an .xlsm file with macros 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
           ' Code 51 represents the enumeration for a macro-free
           ' Excel 2007 Workbook (.xlsx).
           Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
           ' Code 52 represents the enumeration for a
           ' macro-enabled Excel 2007 Workbook (.xlsm).
           Case 52:
              If .HasVBProject Then
                 FileExtStr = ".xlsm": FileFormatNum = 52
              Else
                 FileExtStr = ".xlsx": FileFormatNum = 51
              End If
           ' Code 56 represents the enumeration for a
           ' a legacy Excel 97-2003 Workbook (.xls).
           Case 56: FileExtStr = ".xls": FileFormatNum = 56
           ' Code 50 represents the enumeration for a
           ' binary Excel 2007 Workbook (.xlsb).
            Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
        End Select
     End If
  End If
End With
 
' Change all cells in the worksheet to values, if desired.
''   With Destwb.Sheets(1).UsedRange
''      .Cells.Copy
''      .Cells.PasteSpecial xlPasteValues
''      .Cells(1).Select
''   End With
''Application.CutCopyMode = False
 
'Save the new workbook and then mail it.
  TempFilePath = Environ$("temp") & "\"
  TempFileName = ActiveSheet.Name
 
With Destwb
  .SaveAs TempFilePath & TempFileName, FileFormat:=FileFormatNum
     On Error Resume Next
  .SendMail "", _
     "Subject of email goes here"
     On Error GoTo 0
  .Close SaveChanges:=False
End With
 
' Delete the file you just sent.
Kill TempFilePath & TempFileName & FileExtStr
 
With Application
  .ScreenUpdating = True
  .EnableEvents = True
End With
End Sub

Open in new window

0
Comment
Question by:wisemat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40619166
HI,

works fine for me

in Xl2010 with OL 010

Regards
0
 

Author Comment

by:wisemat
ID: 40619183
Oh, it always fails for me at this line:

 TempFilePath = Environ$("temp") & "\"
0
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40619210
maybe
 TempFilePath = Environ("temp") & "\"
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:wisemat
ID: 40619228
No change

Compile error:
Can't find project or library
0
 
LVL 16

Accepted Solution

by:
Jerry Paladino earned 500 total points
ID: 40620862
You are most likely missing a VBA Reference Library on your installation of Excel.  See this link on Chip Pearson site for an explanation:  Missing References and also a similar discussion at this link: Compile/Reference Errors
0
 

Author Comment

by:wisemat
ID: 40620865
Thanks for the pointer, a lot of words to read there, I'll get back to you as soon as I can.
0
 
LVL 16

Expert Comment

by:Jerry Paladino
ID: 40622902
Wisemat  -  Here is some additional reading where I had a similar problem with VBA not properly compiling the code and the suggestion in this EE Question solved it.  Compile Time Error on Common VBA Function.   Hopefully one of two suggestions will help solve your existing issue.
0
 

Author Closing Comment

by:wisemat
ID: 40655855
Thanks for the library info. This was the source of the problem and not the script. It just took a while for me to work it out.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
3,000 rows of comma separated values 14 31
Pull first name out of cell 2 28
Excel Conditional formatting - icon sets 5 15
VBA Lookup Problem 2 15
Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question