2017-10-13 79 views
1

我有一個vba代碼,當到期日期距離當前日期至少7天7天時,自動發送電子郵件。問題是他們在沒有我的Outlook簽名的情況下發送電子郵件。我使用Outlook 2016. 如果你能幫助我,這對我來說將是一個很大的幫助。VBA,在vba代碼中插入outlook簽名

的代碼是:

Sub email() 
Dim lRow As Integer 
Dim i As Integer 
Dim toDate As Date 
Dim toList As String 
Dim eSubject As String 
Dim eBody As String 

With Application 
    .ScreenUpdating = False 
    .EnableEvents = False 
    .DisplayAlerts = False 
End With 

Sheets(1).Select 
lRow = Cells(Rows.Count, 4).End(xlUp).Row 

For i = 2 To lRow 
toDate = Cells(i, 3) 
If toDate - Date <= 7 Then 
    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 

     toList = Cells(i, 4) 'gets the recipient from col D 
     eSubject = "Doukementacion per " & Cells(i, 2) & " Targa " & Cells(i, 5) 
     eBody = "Pershendetje Adjona" & vbCrLf & vbCrLf & "Perfundo dokumentacionin e nevojshem per " & Cells(i, 2) & " me targa " & Cells(i, 5) 

     On Error Resume Next 
     With OutMail 
     .To = toList 
     .CC = "" 
     .BCC = "" 
     .Subject = eSubject 
     .Body = eBody 
     .bodyformat = 1 
     '.Display ' ********* Creates draft emails. Comment this out when you are ready 
     .Send  '********** UN-comment this when you are ready to go live 
     End With 
    On Error GoTo 0 
    Set OutMail = Nothing 
    Set OutApp = Nothing 
Cells(i, 11) = "Mail Sent " & Date + Time 'Marks the row as "email sent in Column A" 
End If 
Next i 

ActiveWorkbook.Save 

With Application 
    .ScreenUpdating = True 
    .EnableEvents = True 
    .DisplayAlerts = True 
End With 
End Sub 

謝謝!

+1

可能的重複事件檢測[如何添加默認在Outlook中籤名](https://stackoverflow.com/questions/8994116/how-to-add-default-signature-in-outlook) – niton

回答

1

我發現有用的是使它成爲HTMLBody。所以這部分:

With OutMail 
    .To = toList 
    .CC = "" 
    .BCC = "" 
    .Subject = eSubject 
    .Body = eBody 
    .bodyformat = 1 
    '.Display ' ********* Creates draft emails. Comment this out when you are ready 
    .Send  '********** UN-comment this when you are ready to go live 
End With 

會是什麼樣子

With OutMail 
    .Display 'ads the signature 
    .To = toList 
    .Subject = eSubject 
    .HTMLBody = eBody & .HTMLBody 
    '.Display ' ********* Creates draft emails. Comment this out when you are ready 
    .Send  '********** UN-comment this when you are ready to go live 
    End With 

您可能需要切換事件,不知道,因爲我沒有禁用

+0

這是錯誤的 - 你不能連接2個HTML文件併產生一個有效的HTML文件 - 你的數據需要在適當的地方插入到HTML主體中。 –

+0

它的工作原理,但可能是我用這個一年左右的運氣,或者我錯過了'身體'的東西。從https://www.rondebruin.nl/win/s1/outlook/signature.htm找到靈感來源:http://www.rondebruin.nl/win/s1/outlook/signature.htm – krib

+0

它在那個鏈接上工作,因爲被附加的數據不是一個帶有html和head標籤的完整的HTML文檔。在你的情況下,你是在有效的HTML文檔開始之前追加數據。如果Outlook可以解析出來,你很幸運。通常情況並非如此。 –