|
-
Feb 11th, 2012, 06:55 PM
#1
Thread Starter
New Member
[RESOLVED] [Excel] Help with saving and emailing worksheet
Hello All,
Thank you for taking time to help me out. I am new to VBA (hint: my username) and am constantly learning new code and their functions. I am to the point where I can take some already written code, fumble through it, and manipulate it to function as I need it. The situation that I'm in now, I have not been able to find exactly what I need.
As I've seen posted in a lot of areas, you can easily take an active worksheet, temporarily save it, open an Outlook message, attach the file, and then send it. After all is done, the code deletes the temp file.
I have a workbook with about 70 worksheets. I want to put one button on each worksheet that will:
- Extract that single active worksheet to a new workbook.
- Save the new workbook to a specified network drive using the sheet name + today's date as the file name. Example: Access Control 02/11/2012.xlsx.
- Open a new Outlook message.
- Populate the To:, Subject:, and Body with my own addresses and text.
- Attach the recently saved workbook.
- And then finally display the message and not send it. I believe this will use .display instead of .send in the code.
- Somewhere in here too, when done attaching the file, close the new workbook.
The code listed below partially works for me, but I'm sure there are things in there that I don't need, and probably a few extra things that I need.
Code:
Sub Mail_ActiveSheet()
'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
'Copy the sheet 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 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
' 'Change all cells in the worksheet to values if you want
' With Destwb.Sheets(1).UsedRange
' .Cells.Copy
' .Cells.PasteSpecial xlPasteValues
' .Cells(1).Select
' End With
' Application.CutCopyMode = False
'Save the new workbook/Mail it/Delete it
TempFilePath = "Z:\ABC\ABC Terminal\ASOC\ABC Matrix Reports"
TempFileName = ActiveSheet.Name & " " & Format(Date, "mm-dd-yyyy")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFileName & FileExtStr, _
FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = "[email protected]"
.CC = ""
.BCC = ""
.Subject = "ABC Matrix Report " & Format(Date, "mm/dd/yyyy")
.body = ""
.Attachments.Add Destwb.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.display 'or use .Display
End With
On Error Resume Next
.Close SaveChanges:=True
End With
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Also, when I am writing text in the body of the email, how would I go about making a particular section of text red?
Code:
.body = "How would I make this string text red?"
Thanks again for any help provided. I continue to research and learn more as I go along and hope for this to be a great learning experience.
-Adam
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|