通过收件人循环Outlook VBA中的错误

时间:2018-09-11 08:21:57

标签: excel vba excel-vba outlook outlook-vba

我有一个Excel表格,范围A1:B7中具有以下值

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<p> Try inserting Year Ended = 2018, Name = Test, DOP = 1st April, 2010, OC = 1250000, wdv = 686931, add = null(no need to enter), eul = 15 then it will give the output bda=2919, day used = 365, rate of dep = 25.8986, dep amt = 177906, net block = 509025..</p>

<p>Ignore the console error</p>

<div class="form-group">
  <label for="yearEnd">Year Ended in</label>
  <input type="number" min="0" name="yearEnded" id="yearEnd" class="form-control" placeholder="Eg:- 2001" required>
</div>

<div style="border-style: dotted; border-radius: 10px; border-width: thin;">
  <div class="col-md-12">
    <div class="row">
      <div class="col-md-2">
        <label for="assetName">Name</label><br>
        <input type="text" id="assetName" class="form-control">
      </div>
      <div class="col-md-2">
        <label for="assetDOP">DOP</label><br>
        <input type="date" id="assetDOP" class="form-control date_of_purchase">
      </div>
      <div class="col-md-2">
        <label for="assetOC">OC</label><br>
        <input type="number" min="0" id="assetOC" class="form-control originalCost">
      </div>
      <div class="col-md-2">
        <label for="assetWDV">WDV</label><br>
        <input type="number" min="0" id="assetWDV" class="form-control assWDV">
      </div>
      <div class="col-md-2">
        <label for="assetADD">ADD</label><br>
        <input type="number" min="0" id="assetADD" class="form-control addition">
      </div>
      <div class="col-md-2">
        <label for="assetEUL">EUL</label><br>
        <input type="number" min="0" id="assetEUL" class="form-control no_of_year">
      </div>
    </div>
  </div>

  <div class="col-md-12" style="padding-bottom: 10px;">
    <div class="row">
      <div class="col-md-2">
        <label for="assetBDA">BDA</label><br>
        <input type="number" readonly min="0" id="assetBDA" class="form-control bdaValue">
      </div>
      <div class="col-md-2">
        <label for="assetDays">Day Used</label><br>
        <input type="number" readonly min="0" id="assetDays" class="form-control dayUsed">
      </div>
      <div class="col-md-2">
        <label for="assetRateofDep">Rate of Dep.</label><br>
        <input type="number" readonly min="0" id="assetRateofDep" class="form-control rateOfDep">
      </div>
      <div class="col-md-2">
        <label for="assetDepAmount">Dep. Amount</label><br>
        <input type="number" readonly min="0" id="assetDepAmount" class="form-control depAmount">
      </div>
      <div class="col-md-2">
        <label for="assetNetBlock">Net Block</label><br>
        <input type="number" readonly min="0" id="assetNetBlock" class="form-control netBlock">
      </div>
    </div>
  </div>
</div>

<div id="sch8_wdv"></div>
<a href="javascript:void(0);" style=" margin-top: 10px;margin-bottom: 10px!important;" onclick="addMoreSchedule8WDV()" class="btn btn-primary btn-sm">Add More</a>

然后我制作了以下VBA宏,将其添加为Outlook中的电子邮件收件人

+----------------+--------------------+
| Recipient Type | Recipient Addresss |
+----------------+--------------------+
| To             | a@xyz.com          |
| To             | b@xyz.com          |
| CC             | c@xyz.com          |
| CC             | d@xyz.com          |
| BCC            | e@xyz.com          |
| BCC            | f@xyz.com          |
+----------------+--------------------+

问题是.. 最后一个收件人,即f@xyz.com总是添加到“收件人”字段而不是“密件抄送”字段中。 但是,如果我在表中将最后一个空白行设为空白,如下所示 收件人类型=密件抄送和收件人地址=“”(一个空白),然后代码开始工作,并按预期的方式在收件人,抄送和密件抄送字段中分别添加了两个收件人

可能是什么原因?

3 个答案:

答案 0 :(得分:0)

尝试像这样调试:

For Each cl In rn
    Debug.Print cl.Address; cl.Parent.Name
    Select Case cl.Value
        Case "To"
            Debug.Print "adding "; cl.Offset(, 1); "TO"
            olMail.Recipients.Add(cl.Offset(0, 1).Value).Type = olTo
        Case "CC"
            Debug.Print "adding "; cl.Offset(, 1); "CC"
            olMail.Recipients.Add(cl.Offset(0, 1).Value).Type = olCC
        Case "BCC"
            Debug.Print "adding "; cl.Offset(, 1); "BCC"
            olMail.Recipients.Add(cl.Offset(0, 1).Value).Type = olBCC
    End Select
Next

并查看您是否在即时窗口 Ctrl + G 中没有发现任何有趣的内容。

答案 1 :(得分:0)

Select Case似乎有效,但是Recipients.Add方法存在一些问题。

但是,如果您愿意接受其他解决方案,则可以尝试以下代码:

Option Explicit

Sub Add_Recipients_Data_and_Type()
Dim olApp As Outlook.Application

Set olApp = GetObject(, "Outlook.Application")
Dim olMail As Outlook.MailItem

Set olMail = olApp.CreateItem(olMailItem)
olMail.Display
Dim rn      As Range
Dim cl      As Range
Dim mailTo  As String
Dim mailCC  As String
Dim mailBCC As String
Dim i       As Long

i = 1
Set rn = Range("A1").CurrentRegion.Columns(1).Range(Cells(1, 1), Cells(Range("A1").CurrentRegion.Rows.Count, 1))
For Each cl In rn
    Select Case cl.Value
        Case "To"
            mailTo = mailTo & cl.Offset(0, 1).Value & ";"
        Case "CC"
            mailCC = mailCC & cl.Offset(0, 1).Value & ";"
        Case "BCC"
            mailBCC = mailBCC & cl.Offset(0, 1).Value & ";"
    End Select
    i = i + 1
Next cl

olMail.To = mailTo
olMail.CC = mailCC
olMail.BCC = mailBCC
End Sub

答案 2 :(得分:0)

似乎是一个错误。当我单击“检查名称”时,在密件抄送中添加了一个重复的f@xyz.com。

我在代码中尝试了ResolveAll,而f@xyz.com位于“密件抄送”而不是“收件人”中。

Option Explicit

Sub Add_Recipients_Data_and_Type()

Dim olApp As Outlook.Application
Set olApp = GetObject(, "Outlook.Application")
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
olMail.Display
Dim rn As Range
Dim cl As Range
Dim i As Long
i = 1
Set rn = Range("A1").CurrentRegion.Columns(1).Range(Cells(1, 1), Cells(Range("A1").CurrentRegion.Rows.Count, 1))
For Each cl In rn
    Select Case cl.Value
        Case "To"
            olMail.Recipients.Add(cl.Offset(0, 1).Value).Type = olTo
        Case "CC"
            olMail.Recipients.Add(cl.Offset(0, 1).Value).Type = olCC
        Case "BCC"
            olMail.Recipients.Add(cl.Offset(0, 1).Value).Type = olBCC
    End Select
    i = i + 1
Next cl

olMail.Recipients.ResolveAll

End Sub