Hello,
i am creating a VBA macro which is called by an Outlook rule.
I am trying to insert 3 random quotes in an Outlook model where %Ligne1% get replaced
by a random quote picked in "c:\outlook\ligne1.txt" and so on.
I got the macro working,
but sometimes, instead of getting a quote replaced, i got a blank line.
Sometimes line1, sometimes line2.... sometimes line3, sometimes all lines working...
sometimes 2 are blanks => random problem.. for my random quote problem (randomception?)
Here is the full code:
What i already done :
- Checked if my txt file had blank line. No blank lines.
- Placed some msgbox to see where can be the problem :
randLine are ok, numLines are ok.
- lines(randLine) seems to show blank line when it happens,
so the problem is more likely to be on the "lines" string.
I really don't understand what's going on
Thanks by advance for help
Tobby
i am creating a VBA macro which is called by an Outlook rule.
I am trying to insert 3 random quotes in an Outlook model where %Ligne1% get replaced
by a random quote picked in "c:\outlook\ligne1.txt" and so on.
I got the macro working,
but sometimes, instead of getting a quote replaced, i got a blank line.
Sometimes line1, sometimes line2.... sometimes line3, sometimes all lines working...
sometimes 2 are blanks => random problem.. for my random quote problem (randomception?)
Here is the full code:
Code:
Sub SendNew(Item As Outlook.MailItem)
Dim objMsg As MailItem
Set objMsg = Application.CreateItemFromTemplate("C:\outlook\modele.oft")
' Copy the original message subject
objMsg.Subject = "Re: " & Item.Subject
' Ligne 1
Dim lines() As String
Dim numLines As Integer
numLines = 0
' Open the file for reading
Open "c:\outlook\ligne1.txt" For Input As #1
' Go over each line in the file and save it in the array + count it
Do Until EOF(1)
ReDim Preserve lines(numLines + 1)
Line Input #1, lines(numLines)
numLines = numLines + 1
Loop
Close #1
' Get the random line number
Dim randLine As Integer
randLine = Int(numLines * Rnd()) + 1
' Insert the random quote
objMsg.HTMLBody = Replace(objMsg.HTMLBody, "%Ligne1%", lines(randLine))
' Ligne 2
Dim lines2() As String
Dim numLines2 As Integer
numLines2 = 0
' Open the file for reading
Open "c:\outlook\ligne2.txt" For Input As #2
' Go over each line in the file and save it in the array + count it
Do Until EOF(2)
ReDim Preserve lines2(numLines2 + 1)
Line Input #2, lines2(numLines2)
numLines2 = numLines2 + 1
Loop
Close #2
' Get the random line number
Dim randLine2 As Integer
randLine2 = Int(numLines2 * Rnd()) + 1
' Insert the random quote
objMsg.HTMLBody = Replace(objMsg.HTMLBody, "%Ligne2%", lines2(randLine2))
' Ligne 3
Dim lines3() As String
Dim numLines3 As Integer
numLines3 = 0
' Open the file for reading
Open "c:\outlook\ligne3.txt" For Input As #3
' Go over each line in the file and save it in the array + count it
Do Until EOF(3)
ReDim Preserve lines3(numLines3 + 1)
Line Input #3, lines3(numLines3)
numLines3 = numLines3 + 1
Loop
Close #3
' Get the random line number
Dim randLine3 As Integer
randLine3 = Int(numLines3 * Rnd()) + 1
' Insert the random quote
objMsg.HTMLBody = Replace(objMsg.HTMLBody, "%Ligne3%", lines3(randLine3))
' use for testing
' objMsg.Send
objMsg.Display
End Sub
What i already done :
- Checked if my txt file had blank line. No blank lines.
- Placed some msgbox to see where can be the problem :
randLine are ok, numLines are ok.
- lines(randLine) seems to show blank line when it happens,
so the problem is more likely to be on the "lines" string.
I really don't understand what's going on
Thanks by advance for help
Tobby