Hej Hej,Send mail (även postat i access)
Använder scriptet nedan för att plocka ut poster ur ett excel ark och skicka dom till en exklusiv mottagare, vilket funkar bra. Dock skulle jag vilja ha ett annat utformat (excel) på utskicket än det som man är hänvisade till i skriptet ( .HTMLBody = RangetoHTML(rng) ).
Hur löser man det ?
Sub Send_Row_Or_Rows_2()
' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim mailAddress As String
On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
'Set filter sheet, you can also use Sheets("MySheet")
Set Ash = ActiveSheet
'Set filter range and filter column (Column with E-mail addresses)
Set FilterRange = Ash.Range("A1:G" & Ash.Rows.Count)
FieldNum = 1 'Filter column = A because the filter range start in A
'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Cws.Range("A1"), _
CriteriaRange:="", Unique:=True
'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))
'If there are unique values start the loop
If Rcount >= 2 Then
For Rnum = 2 To Rcount
'Filter the FilterRange on the FieldNum column
FilterRange.AutoFilter Field:=FieldNum, _
Criteria1:=Cws.Cells(Rnum, 1).Value
'Look for the mail address in the MailInfo worksheet
mailAddress = ""
On Error Resume Next
mailAddress = Application.WorksheetFunction. _
VLookup(Cws.Cells(Rnum, 1).Value, _
Worksheets("Mailinfo").Range("A1:B" & _
Worksheets("Mailinfo").Rows.Count), 2, False)
On Error GoTo 0
If mailAddress <> "" Then
With Ash.AutoFilter.Range
On Error Resume Next
Set rng = .SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = mailAddress
.Subject = "Test mail"
.HTMLBody = RangetoHTML(rng)
.Display 'Or use Send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
'Close AutoFilter
Ash.AutoFilterMode = False
Next Rnum
End If
cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End SubSv: Send mail (även postat i access)
Det verkar som om du använder koden från http://www.rondebruin.nl/mail/folder3/mail4.htm(?)
Kolla gärna på svaret från Karl E Peterson här: http://vb.mvps.org/articles/ap200106.asp. Där visar han hur du kan få ut data från Excel utan formatering, eller också hur du själv lägger till formatering. Hans kod ser då ut så här:
VBA Strip the HTML to Its Basics
Private Const OpenTable As String = "<table>"
Private Const CloseTable As String = "</table>"
Private Const OpenRow As String = "<tr>"
Private Const CloseRow As String = "</tr>"
Private Const OpenCellHead As String = "<th>"
Private Const CloseCellHead As String = "</th>"
Private Const OpenCell As String = "<td>"
Private Const CloseCell As String = "</td>"
Public Enum CopyWhatOpts
copyText = 1
copyValue = 2
copyFormula = 3
End Enum
Private Function RangeToHtml(ByVal Source As _
Excel.Range, Optional Indent As Long = 2, _
Optional TabBump As Long = 2, Optional _
PadEmpty As Boolean = True) As String
' This version of this routine uses the
' CStringBuilder class to concatenate.
Dim r As Long, c As Long
Dim Padding As String
Dim sRet As CStringBuilder
Dim Cell As String
Dim Tab1 As String
Dim Tab2 As String
' Pad based on passed indent value
Padding = Space$(Indent)
Tab1 = Space$(TabBump)
Tab2 = Space$(TabBump * 2)
Set sRet = New CStringBuilder
With Source
' Loop through range
sRet.Append Padding & OpenTable & vbCrLf
For r = 1 To .Rows.Count
sRet.Append Padding & Tab1 & OpenRow & vbCrLf
For c = 1 To .Columns.Count
sRet.Append Padding & Tab2 & OpenCell
If IsEmpty(.Cells(r, c).Value) Then
If PadEmpty Then
sRet.Append " "
End If
Else
Cell = Trim$(.Cells(r, c).Text)
If Len(Cell) Then
sRet.Append Cell
Else
If PadEmpty Then
sRet.Append " "
End If
End If
End If
sRet.Append CloseCell & vbCrLf
Next c
sRet.Append Padding & Tab1 & _
CloseRow & vbCrLf
Next r
sRet.Append Padding & CloseTable & vbCrLf
End With
' Return results
RangeToHtml = sRet.ToString
End Function
Eftersom funktionen har samma namn som den från rondebruin.nl, så borde det vara en smal sak att ersätta denna.
// Torgrim