Find text within email template (1 Viewer)

Isaac

Lifelong Learner
Local time
Today, 12:51
Joined
Mar 14, 2017
Messages
8,777
It just seemed a simple thing to try to inspect an email to find if a particular text exists
I think if you utilize If Instr(1,myMail.Body,"Text to Find")>0 , you'll have your test. And this is JUST me probably, but I think when you are newer to code, With blocks make things seem harder, not easier. Although they look neat and tidy, they make the hierarchy of objects vs. members a little bit harder to grasp (unless everything is just on one simple level), and make it a bit confusing when you need to "do" other things within the With block.

You can also write that area of code as something like:
Code:
'get new info to populate email template
strFind="company"
If Instr(1,myMail.body,strFind)=0 then
    strMsg = "Enter caller's company."
    strInput = InputBox(Prompt:=strMsg, Title:="Leave blank if not known/relevant")
    sbjtCoy = strInput
    strInput = "from " & strInput
End If
myMail.HTMLBody = Replace(MyMail.HTMLBody, strFind, strInput)
The only line of code I would still take a second look at is the last line. If "company" doesn't exist, then Replace() is not going to find anything to replace! Something like this would "add" it:
Code:
strFind="company"

If Instr(1,myMail.body,strFind)=0 then
    strMsg = "Enter caller's company."
    strInput = InputBox(Prompt:=strMsg, Title:="Leave blank if not known/relevant")
    sbjtCoy = strInput
    strInput = "from " & strInput
    myMail.HTMLBody = myMail.HTMLBody & vbnewline & strInput
Else
    myMail.HTMLBody = myMail.HTMLBody
    'unnecessary, but I added for clarification
End If
 

Micron

AWF VIP
Local time
Today, 15:51
Joined
Oct 20, 2018
Messages
3,478
Maybe it's easier to follow after some experience, but I don't have a problem following With blocks. I'm not a fan of repeating the object reference over and over on subsequent lines. However, I don't nest them as a rule.
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:51
Joined
Sep 21, 2011
Messages
14,260
Don't forget that I mentioned that this might work if you have the required references and are not using late binding.
@Micron, I tried the code in Outlook and it balked at that .Content.Find, so I gave up after that.
 

Isaac

Lifelong Learner
Local time
Today, 12:51
Joined
Mar 14, 2017
Messages
8,777
Maybe it's easier to follow after some experience, but I don't have a problem following With blocks. I'm not a fan of repeating the object reference over and over on subsequent lines. However, I don't nest them as a rule.
Maybe I am just lazy in my thinking, but yes also probably gets easier from experience. For some reason to this day I rarely use them, perhaps without good reason.
I think it also depends on how many variables one has declared & set.
For example, I wouldn't repeat over and over:
Code:
ThisWorkbook.Worksheets(Sheet1").Range("A1").Value =

Some people solve that lengthy reference by using a With block
Code:
With ThisWorkbook.Worksheets(Sheet1").Range("A1")
...
End With

I tend to solve it by
Code:
dim rng as Range
set rng = With ThisWorkbook.Worksheets(Sheet1").Range("A1")
rng..
rng..
rng..
I always tend to declare and set variables down to the most granular level I'll need, which ends up achieving roughly the same level of shortcut thereafter. But you're right ... there is just a bit more repeating my way. Something about following the visual flow seems to suit me. Just an odd preference of mine I guess.
 

Micron

AWF VIP
Local time
Today, 15:51
Joined
Oct 20, 2018
Messages
3,478
it balked at that .Content.Find
Don't forget that I said earlier that .Find wasn't a method of the object (in so many words). It belongs to the Items object and early binding with the correct reference would likely reveal that. It seems that .Content s not a member of MailItem either, so the problem is somewhere up the reference ladder.
 

Micron

AWF VIP
Local time
Today, 15:51
Joined
Oct 20, 2018
Messages
3,478
Something about following the visual flow seems to suit me
I agree that sometimes it may cause some confusion even in your own code, especially when reviewing much later. That's where notes help I guess. I find that once I establish in my mind the level to which the object variable goes, it's not much of a problem as long as I can keep it straight for that block. Once I'm out of the block, it likely can be forgotten.
 

Users who are viewing this thread

Top Bottom