0
votes

I currently have a draft in my Outlook draft folder with the subject title "Test Subject". The draft also has a body and a recipient. I want to make a VBScript file that would find that draft, add some new text to its body, and then send it to the recipient.

I found this page and was using it as sort of a template http://www.techrepublic.com/forums/discussions/send-all-mails-from-my-drafts-folder-at-one-go-in-outlook-2003/

This is what I've got so far

Public Sub SendDrafts()
Dim lDraftItem
Dim myOutlook
Dim myNameSpace
Dim myFolders
Dim myDraftsFolder


'Setup Outlook

Set myOutlook = CreateObject("Outlook.Application")
Set myNameSpace = myOutlook.GetNamespace("MAPI")
Set myFolders = myNameSpace.Folders

'Set up Drafts folder
Set myDraftsFolder = myFolders("Personal Folders").Folders("Drafts")

'iterate through drafts
For lDraftItem = myDraftsFolder.Items.Count To 1 Step -1

     'find draft that matches designated subject, in this case 'Test Subject'
      If StrComp(myDraftsFolder.Items.Item(lDraftItem).Subject, "Test Subject") Then

          'Add new text to draft
           myDraftsFolder.Items.Item(lDraftItem).htmlBody = myDraftsFolder.Items.Item(lDraftItem).htmlBody & "</b><br><br><br> New Text here."

          'Send Item
          myDraftsFolder.Items.Item(lDraftItem).Send

      End If

Next

End sub

For whatever reason, when I run the script I get nothing. The draft remains in my draft folder but I also get no error message. I'm fairly new to VBScript, so any help would be appreciated. Thanks.

1
Well, did the solution provided by me worked? - Gurmanjot Singh

1 Answers

2
votes

When the match is found, strcomp will return 0 which means false and hence it is not going inside the If condition as false is returned. You need to modify your If condition as below.

If StrComp(myDraftsFolder.Items.Item(lDraftItem).Subject, "Test Subject")=0 Then
    myDraftsFolder.Items.Item(lDraftItem).htmlBody = myDraftsFolder.Items.Item(lDraftItem).htmlBody & "</b><br><br><br> New Text here."
    myDraftsFolder.Items.Item(lDraftItem).Send
    Exit For
End If