
Originally Posted by
xladept
Hi flunzy,
Is the destination workbook macro-enabled??
Hi XL adept,
The destination workbook is macro-enabled. I've made a workaround that partly does the trick. I've saved the module on a fixed location and added a macro that imports the module. This succeeds but when the module is imported and linked to the button, it states that the macro is only available in original sheet : ( Below you find the codes
The send option:
Sub Mail_Range()
Dim Source As Range
Dim Dest As Workbook
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim OutApp As Object
Dim OutMail As Object
Set Source = Nothing
On Error Resume Next
Set Source = Range("A:O").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Source Is Nothing Then
MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Dest = Workbooks.Add(xlWBATWorksheet)
Set wb = ActiveWorkbook
Source.Copy
Range("Q8").Select
With Dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial Paste:=xlPasteValues
.Cells(1).PasteSpecial Paste:=xlPasteFormats
.Cells(1).PasteSpecial Paste:=xlPasteValidation
.Cells(1).Select
Application.CutCopyMode = False
Rows("1:1").Select
Selection.RowHeight = 15
Columns("M:M").Select
Selection.ColumnWidth = 27
Range("M3").Select
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
End With
ActiveSheet.Buttons.Add(1371, 29.25, 191.25, 45.75).Select
Selection.Characters.Text = "Knop 4"
With Selection.Characters(Start:=1, Length:=6).Font
.Name = "Calibri"
.FontStyle = "Standaard"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
ActiveSheet.Shapes.SelectAll
Selection.Characters.Text = "test"
With Selection.Characters(Start:=1, Length:=4).Font
.Name = "Calibri"
.FontStyle = "Standaard"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
Range("R9").Select
With Dest.Sheets(1)
ActiveWorkbook.VBProject.VBComponents.Import ("H:\My documents\Send_whole_workbook.bas")
ActiveWorkbook.ActiveSheet.Buttons.Add(1371, 30, 192.75, 45).Select
End With
ActiveSheet.Shapes.Range(Array("Button 3")).Select
Selection.OnAction = "Mail_workbook_Outlook_3"
TempFilePath = Environ$("temp") & "\"
TempFileName = "File" & " "
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2013
FileExtStr = ".xlsm": FileFormatNum = 52
End If
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Dest
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = ThisWorkbook.Sheets("Main").Range("P8").Value
.SentOnBehalfOfName = ""
.CC = ""
.BCC = ""
.Subject = "File"
.HTMLBody = "<H3><B>File</B></H3>" &
.Attachments.Add Dest.FullName
.Display
End With
On Error GoTo 0
.Close savechanges:=False
End With
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
and this is the module that needs to be imported:
Sub Mail_workbook_Outlook_1()
'Working in Excel 2000-2013
'This example send the last saved version of the Activeworkbook
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "info@me.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Attachments.Add ActiveWorkbook.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Bookmarks