0

I am writing a VBScript to extract CC from the email. When I extract the cc, instead of an email address, it shows the email name of the person. I had same issue while I was extracting the "from" address. I checked whether the email address type of the from the person (.SenderEmailType) is SMTP or EX and was able to fetch the email address instead of email name. I don't know how to do the same for CC. I have checked online, it is written to loop through "Mailitems.Recipent". I am new to vbscript to I am not sure how to do this. Currently I am using .CC object to get the cc detail.

Set Arg = WScript.Arguments
dim item1
dim objsubject 
dim intcount
Dim i 
dim savename 
dim vTextFile
dim filename 
dim extension
Dim t
Dim Itimestamp 
dim savefolder 
Dim vSenderEmailAddress
Dim vCcEmailAddress
Dim vFlagTextFileCreate


vFlagTextFileCreate = True
savefolder = "C:\Users\tgssupport\Documents\Automation Anywhere Files\Automation Anywhere\My Scripts\Retro Pricing\junk"
vTextFile = savefolder & "\File Report.txt"
vFlagExcelAttachmentFound = False
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then    'Could not get instance of Outlook, so create a new one
   Err.Clear
   Set olApp = CreateObject("Outlook.Application")
End If
on error goto 0
Set olns = olApp.GetNameSpace("MAPI")
olns.logon "Outlook",,False,True
'6 is for Inbox
Set objFolder = olns.GetDefaultFolder(6)
For each item1 in objFolder.Items

  if item1.Unread=true then
    objsubject = item1.subject
      vCcEmailAddress = item1.CC
      If item1.SenderEmailType = "SMTP" Then
               vSenderEmailAddress = item1.SenderEmailAddress
      ElseIf item1.SenderEmailType = "EX" Then
               vSenderEmailAddress = item1.Sender.GetExchangeUser.PrimarySmtpAddress
      End If 'If item1.SenderEmailType
      msgbox vCcEmailAddress.
      msgbox vSenderEmailAddress.
  end if 'if item1.Unread=true
Next
olns.logoff
Set olns  = Nothing
Set olApp = Nothing
WScript.Quit
  • Hi, would be helpful if you could share the relevant part of the script. – arnonuem Jul 03 '19 at 08:53
  • Hello @arnonuem, I have added the code. You can see that the variable vCcEmailAddress is my variable to fetch the cc. Let me know if some more detail is required. Thank you for your response. – Vikit Shetty Jul 03 '19 at 09:01
  • I am not an expert on this but i just found this solution: See the Recipients collection, and check the type property of each recipient object. Please try if this helps. I found that here: https://forums.slipstick.com/threads/29621-extracting-cc-email-address-using-vba/ – arnonuem Jul 03 '19 at 09:09
  • Thanks @arnonuem. I already saw that like I mentioned earlier. I am not able to understand how to use that. – Vikit Shetty Jul 03 '19 at 09:12
  • 1
    Please reduce that wall of code to a [mcve]. – Ansgar Wiechers Jul 03 '19 at 09:34
  • Hello @AnsgarWiechers. I have reduced the code. Now you will see that I am trying to get the cc email address(variable vCcEmailAddress) and sender email in message box (variable vSenderEmailAddress ). Thanks for the feedback. Let me know if anymore details is required. – Vikit Shetty Jul 03 '19 at 09:46
  • @AAA Currently I have coded it just for one cc – Vikit Shetty Jul 03 '19 at 10:01
  • I assume you have no problem getting SenderEmailAddress. The answer below helps you get CC email address – AAA Jul 03 '19 at 10:16
  • @AAA i did something similar to your answer and was able to solve it. Thanks a lot for your help buddy. Attached is my answer – Vikit Shetty Jul 03 '19 at 10:45
  • Possible duplicate of [Get item.recipient with Outlook VBA](https://stackoverflow.com/questions/28986306/get-item-recipient-with-outlook-vba) – user692942 Jul 04 '19 at 07:05

2 Answers2

1
With item1.Recipients
    For i = 1 To .Count
        If .Item(i).Type = OlMailRecipientType.olCC Then
            vCcEmailAddress = .Item(i).Address
            Exit For
        End If
    Next i
End With
AAA
  • 3,520
  • 1
  • 15
  • 31
-1
Set objFolder = olns.GetDefaultFolder(6)
For each item1 in objFolder.Items      
   For Each RecipientObject In item1.Recipients
      If RecipientObject.Type = 2 Then
      msgbox RecipientObject.Address
      End if
   Next
Next