PDA

View Full Version : Reply to Email and Loop a Number For Each Reply?



VBE313
12-23-2019, 06:40 AM
I have the following code in Outlook. Is there a way to set testNum to whatever testNum was in the last email + 1? For example. If someone sent me an email and I replied, it should say "Please refer to this RFQ as Quote #1" for this person. If i receive another email, I want the email to say "Please refer to this RFQ as Quote #2".

Is there a way to globally set testNum and have it changed after every time i send a email?


Sub ReplyMSG()
Dim testNum As Double
Dim olItem As Outlook.MailItem
Dim olReply As MailItem ' Reply
testNum = 0
For Each olItem In Application.ActiveExplorer.Selection
Set olReply = olItem.ReplyAll
With olReply
.Body = "Please refer to this RFQ as Quote #" & testNum + 1
End With
olReply.Display
Next olItem
End Sub

gmayor
12-23-2019, 09:47 PM
Maybe as follows


Sub ReplyMSG()

Dim testNum As Double
Dim olItem As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim olReply As MailItem ' Reply
testNum = InputBox("Enter start number", "Quote Number", 1)
For Each olItem In Application.ActiveExplorer.Selection
Set olReply = olItem.ReplyAll
With olReply
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
oRng.Text = "Please refer to this RFQ as Quote #" & testNum
End With
olReply.Display
'olReply.Send
testNum = testNum + 1
DoEvents
Next olItem
Set olReply = Nothing
Set olItem = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
End Sub

or if it was only for your use, you could store the number in the registry and lose the prompt e.g. as follows
The registry entry is at HKEY_CURRENT_USER\Software\VB and VBA Program Settings\ReplyMsg\


Sub ReplyMSG()

Dim testNum As Double
Dim olItem As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim olReply As MailItem ' Reply
testNum = GetSetting("ReplyMsg", "Settings", "Quote Number", 1) '1 is the start number
For Each olItem In Application.ActiveExplorer.Selection
Set olReply = olItem.ReplyAll
With olReply
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
oRng.Text = "Please refer to this RFQ as Quote #" & testNum
End With
olReply.Display
'olReply.Send
testNum = testNum + 1
SaveSetting "ReplyMsg", "Settings", "Quote Number", testNum
DoEvents
Next olItem
Set olReply = Nothing
Set olItem = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
End Sub

Sub ResetStartNum()
SaveSetting "ReplyMsg", "Settings", "Quote Number", 1 '1 is the start number
End Sub

VBE313
12-24-2019, 08:48 AM
gmayor Thank you very much! This works perfect!