我一直在论坛上拖网,拼凑了各种代码。此代码的想法是基于固定标准创建电子邮件。此固定标准的多个唯一标识符将列在电子邮件中(在正文/主题标题中),并且单词文档将附加到电子邮件中。
我遇到了列出的唯一标识符并附加了word文档的问题。这是我到目前为止拼凑的代码。
Sub Email_Outlook()
Dim olApp As Outlook.Application
Dim MailDoc As Outlook.MailItem
Set olApp = CreateObject("Outlook.Application")
Set MailDoc = olApp.CreateItem(olMailItem)
Dim Mail_worksheet As Worksheet
Dim Mail_worksheet1 As Worksheet
Set Mail_worksheet = ThisWorkbook.Sheets("Email")
Set Mail_worksheet1 = ThisWorkbook.Sheets("Send")
Dim a As Integer
Dim b As String
Dim c As Integer
Dim d As String
b = ThisWorkbook.Worksheets("Sheet1").Cells(1, 4)
d = ThisWorkbook.Worksheets("Sheet1").Cells(1, 4)
For a = 0 To b - 1
'Check Production Selection
If Mail_worksheet.Cells(17, 2 + a) = "ProductA" And Mail_worksheet.Cells(15, 2 + a) = Mail_worksheet1.Cells(7, 6) Then
MailDoc.Subject = "Word document for ProductA - " & Format(Date, "DD MMM YYYY")
For c = 0 To d - 1
If Mail_worksheet.Cells(17, 2 + a) = "ProductA" And Mail_worksheet.Cells(15, 2 + a) = Mail_worksheet1.Cells(7, 6) Then
MailDoc.Body = "Hi," & vbCrLf & vbCrLf & _
"Word documents attached for:" & vbCrLf & _
Mail_worksheet.Cells(4, 2 + c)
End If
Next c
End If
Next a
MailDoc.Attachments.Add ("file:///c:\users\word doc\") & Mail_worksheet.Cells(4, 2 + c) & Like "*.docx"
MailDoc.Display
End Sub
答案 0 :(得分:0)
我认为你需要这样的东西
我无法弄清楚为什么你有两个循环和两个If
语句,所以我把它们拿出来
另外,使用看起来像系统对象的变量名称是个坏主意。 email_Worksheet
乍一看似Worksheet
Sub Email_Outlook()
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim mailDoc As Outlook.MailItem
Set mailDoc = olApp.CreateItem(olMailItem)
Dim emailWs As Worksheet
Set emailWs = ThisWorkbook.Sheets("Email")
Dim sendWs As Worksheet
Set sendWs = ThisWorkbook.Sheets("Send")
Dim b As Integer
b = ThisWorkbook.Worksheets("Sheet1").Range("D1") ' Cells(1, 4)
Dim a As Integer
For a = 0 To b - 1
'Check Production Selection
If emailWs.Cells(17, 2 + a) = "ProductA" And emailWs.Cells(15, 2 + a) = sendWs.Cells(7, 6) Then
mailDoc.Subject = "Word document for ProductA - " & Format(Date, "DD MMM YYYY")
mailDoc.Body = "Hi," & vbCrLf & vbCrLf & _
"Word documents attached for:" & vbCrLf & _
emailWs.Cells(4, 2 + a)
End If
Next a
mailDoc.Attachments.Add "c:\users\word doc\" & emailWs.Cells(4, 2 + c) & ".docx"
mailDoc.Display
End Sub