AW: wer lesen kann...
28.05.2009 20:51:55
Josef
Hallo Volker,
hier der Code mit ein paar deutschen Kommentaren.
Achte besinders auf die Zeile ####Hier####.
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************
Option Explicit
'Anpassen!
Private Const cstrRecipient As String = "a.b@c.com" 'Empfängeradresse
Private Const cstrSenderName As String = "Dein Name" 'Absendername
Private Const cstrSheetSend As String = "Tabelle1" 'Tabelle die Verschickt wird
Public Const cstrSheetWatch As String = "Tabelle1" 'Tabelle die Überwacht wird
Public Const cstrCellWatch As String = "G25" 'Überwachte Zelle
'###
Public varOldValue As Variant
''Read this!!!
''
''This code will not work in Win 98 and ME.
''You must be connected to the internet when you run a example.
''
''It is possible that you get a Send error when you use one of the examples.
''AFAIK : This will happen if you haven't setup an account in Outlook Express or Windows Mail.
''In that case the system doesn't know the name of your SMTP server.
''If this happens you can use the commented green lines in each example.
''don 't forget to fill in the SMTP server name in each code sample where
''it says "Fill in your SMTP server here"
''
''When you also get the Authentication Required Error you can add this three lines.
''.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
''.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
''.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
''
''don 't remove the TextBody line in the code. If you do you can't open the attachment (bug in CDO).
''If you don't want to have text in the body use this then .TextBody = ""
Sub sendSheet()
'original by http://www.rondebruin.nl/
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 iMsg As Object
Dim iConf As Object
Dim Flds As Variant
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ThisWorkbook
'Copy the ActiveSheet to a new workbook
Sourcewb.Sheets(cstrSheetSend).Copy
'Or if you want to copy more then one sheet use:
'Sourcewb.Sheets(Array("Sheet1", "Sheet3")).Copy
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007
'We exit the sub when your answer is NO in the security dialog that you only
'see when you copy a 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 Destwb to values if you want
' For Each sh In Destwb.Worksheets
' sh.Select
' With sh.UsedRange
' .Cells.Copy
' .Cells.PasteSpecial xlPasteValues
' .Cells(1).Select
' End With
' Application.CutCopyMode = False
' Next sh
' Destwb.Worksheets(1).Select
'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
.Close savechanges:=False
End With
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
'####Hier####
iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Deinen SMTP server hier eintragen" 'Findest du in den Kontoeinstellungen deines mailprogrammes!
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
' 'Bei Authentifizierungsfehler sind auch folgende Zeilen notwendig!
' 'Username und Passwort deines E-Mail Accounts
' .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
' .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username" 'Dein Username
' .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password" 'Dein Passwort
.Update
End With
'############
With iMsg
Set .Configuration = iConf
.To = cstrRecipient
.CC = ""
.BCC = ""
.From = cstrSenderName
.Subject = "This is a test" 'Betreff
.TextBody = "Hi there" 'Text - Achtung! nicht entfernen! "" eingeben wenn kein Text!
.AddAttachment TempFilePath & TempFileName & FileExtStr
.Send
End With
'Delete the file you have send
Kill TempFilePath & TempFileName & FileExtStr
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Gruß Sepp