2017-12-12 10 views
3

안녕하세요 저는 Ron De Bruin의 환상적인 웹 사이트를 사용하여 Excel 파일에서 특정 사용자에게 전자 메일을 보내는 VBA 코드를 작성했습니다.excel/vba에서 Outlook으로 전자 메일을 생성 할 때 전자 메일 서명이 나타나지 않습니까?

유일한 점은 각 이메일에 내 서명이 표시되지 않고 코드 내에 추가하는 방법을 찾을 수 없다는 것입니다.

아무에게도 조언을 줄 수 있습니까?

나는 완전한 초보자라고 말할 수 있습니다!

모듈 1

Option Explicit Sub Send_Row_Or_Rows_2() 

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 strbody As String 

On Error GoTo cleanup 
Set OutApp = CreateObject("Outlook.Application") 

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

'Set filter sheet, you can also use Sheets("MySheet") 
Set Ash = ActiveSheet 

strbody = "<BODY style=font-size:11pt;font-family:Calibri>Hi;<p>Please see below details of outstanding files. We will require these by 25th December 2017. Please feel free to respond with any questions.<p>Thank you.</BODY>" 

'Set filter range and filter column (column with e-mail addresses) 
Set FilterRange = Ash.Range("A1:L" & Ash.Rows.Count) 
FieldNum = 2 'Filter column = B because the filter range start in column 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 

     'If the unique value is a mail addres create a mail 
     If Cws.Cells(Rnum, 1).Value Like "?*@?*.?*" 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 = Cws.Cells(Rnum, 1).Value 
       .Subject = "Test mail" 
       .HTMLBody = strbody & 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 

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

모듈 2 :

Option Explicit 

Function RangetoHTML(rng As Range) 

Dim fso As Object 
Dim ts As Object 
Dim TempFile As String 
Dim TempWB As Workbook 
Dim strbody As String 

TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm" 

'Copy the range and create a new workbook to past the data in 
rng.Copy 
Set TempWB = Workbooks.Add(1) 
With TempWB.Sheets(1) 
    .Cells(1).PasteSpecial Paste:=8 
    .Cells(1).PasteSpecial xlPasteValues, , False, False 
    .Cells(1).PasteSpecial xlPasteFormats, , False, False 
    .Cells(1).Select 
    Application.CutCopyMode = False 
    On Error Resume Next 
    .DrawingObjects.Visible = True 
    .DrawingObjects.Delete 
    On Error GoTo 0 
End With 

'Publish the sheet to a htm file 
With TempWB.PublishObjects.Add(_ 
    SourceType:=xlSourceRange, _ 
    Filename:=TempFile, _ 
    Sheet:=TempWB.Sheets(1).Name, _ 
    Source:=TempWB.Sheets(1).UsedRange.Address, _ 
    HtmlType:=xlHtmlStatic) 
    .Publish (True) 
End With 

'Read all data from the htm file into RangetoHTML 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2) 
RangetoHTML = ts.ReadAll 
ts.Close 
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _ 
         "align=left x:publishsource=") 

'Close TempWB 
TempWB.Close savechanges:=False 

'Delete the htm file we used in this function 
Kill TempFile 
Set ts = Nothing 
Set fso = Nothing 
Set TempWB = Nothing 
End Function 
+0

코드를 사용하여 자동 이메일을 만들 때 서명을 표시하는 방법은 없습니다. – braX

+0

동적으로 서명을 만드시겠습니까? "TempFile = Environ $ ("temp ") &"/ "& Format (지금은"dd-mm-yy h-mm-ss ") &".htm ""으로 무엇을하려합니까? – Xabier

+0

@braX 서명이 .htm으로 저장되고 컴퓨터의 어딘가에 저장되어있는 경우 (이미지의 src가 절대적 일 때) 나도 그렇게 할 수 있습니다. – Xabier

답변

1

을 html로 문자열에 서명을 번역하고 이메일에 추가합니다. 이처럼 : 당신이 다음에 모듈 하나에 모든 것을 바꿀 것 인 경우에

Dim mySignature As String 
mySignature = "<p>Best Regards,<p>Your name and company<p>" 

With OutMail 
    .to = Cws.Cells(Rnum, 1).Value 
    .Subject = "Test mail" 
    .HTMLBody = strbody & RangetoHTML(Rng) & mySignature 
    .Display 'Or use Send 
End With 
+0

고맙습니다. 모듈 1 또는 2에이 코드를 추가하겠습니다. 미안 한 초보자! – withnoice

+0

'.HTMLBody'가있는 모듈 1, @withnoice가 있다고 생각합니다. – Vityata

0

이 ...

With OutMail 
    .BodyFormat = 2 
    .Display = True 
    .To = Cws.Cells(Rnum, 1).Value 
    .Subject = "Test mail" 
    .HTMLBody = strbody & RangetoHTML(rng) & "<br>" & .HTMLBody 
    '.Send 'To send 
End With 
+0

모듈 1 또는 2에이 코드를 추가해 주시겠습니까? 미안 한 초보자! – withnoice

+0

Module1에 비슷한 코드가 있지만 제안 된 코드로 대체하십시오. – sktneer

0

문제가 해결됩니다 있는지 확인이 시도를주십시오, 나는 그것을 확신 .htm 서명 파일의 이름을 바꾸고 해당 이미지를 절대적으로 포함하도록 htm을 편집하십시오.

Option Explicit 

Sub Send_Row_Or_Rows_2() 
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 strbody As String 
Dim SigString As String 
Dim Signature As Variant 

On Error GoTo cleanup 
Set OutApp = CreateObject("Outlook.Application") 
Set OutMail = OutApp.CreateItem(0) 

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

'Set filter sheet, you can also use Sheets("MySheet") 
Set Ash = ActiveSheet 

strbody = "<BODY style=font-size:11pt;font-family:Calibri>Hi;<p>Please see below details of outstanding files. We will require these by 25th December 2017. Please feel free to respond with any questions.</p>Thank you.</BODY>" 

SigString = Environ("appdata") & _ 
    "\Microsoft\Signatures\YourSignature.htm" 

'CHANGE ABOVE TO YOUR SIGNATURE NAME .htm 
'Make sure that the Htm file has all sources defined with absolute references 
'so if an image's src=\img\signature.jpg, then you should change \img\signature to something like: 
'C:\Users\Me\AppData\Roaming\Microsoft\Signatures\ 

If Dir(SigString) <> "" Then 
    Signature = GetBoiler(SigString) 
Else 
    Signature = "" 
End If 

'Set filter range and filter column (column with e-mail addresses) 
Set FilterRange = Ash.Range("A1:L" & Ash.Rows.Count) 
FieldNum = 2 'Filter column = B because the filter range start in column 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 

     'If the unique value is a mail addres create a mail 
     If Cws.Cells(Rnum, 1).Value Like "?*@?*.?*" 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 = Cws.Cells(Rnum, 1).Value 
       .Subject = "Test mail" 
       .HTMLBody = strbody & RangetoHTML(rng) & "<br>" & Signature 
       .Display 'Or use Send 
      End With 
      On Error GoTo 0 

      Set OutMail = Nothing 
     End If 

     'Close AutoFilter 
     Ash.AutoFilterMode = False 

    Next Rnum 
End If 

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

Function GetBoiler(ByVal sFile As String) As String 
'https://www.rondebruin.nl/win/s1/outlook/signature.htm 
    Dim fso As Object 
    Dim ts As Object 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2) 
    GetBoiler = ts.readall 
    ts.Close 
End Function