-1

Currently, I'm trying to create a sub that will read the selected email's "from" field, and set only the sender's domain as a variable:

So if I receive an email from someone@example.com, I can select the email, run the macro, and it will set example.com (not the entire email) as a variable I can use later.

I appreciate any help!

pnuts
  • 58,317
  • 11
  • 87
  • 139
zasx150
  • 25
  • 5

1 Answers1

1

Welcome to the board. Not sure why they downvoted you. But here is how to do what you asked.

Option Explicit

Public savedDomain As String

Public Sub Example()
    Dim mi As Outlook.MailItem
    Dim emailAddress As String
    If Not TypeName(Outlook.Application.ActiveWindow) = "Inspector" Then
        Exit Sub
    End If
    Set mi = Application.ActiveWindow.CurrentItem
    emailAddress = mi.SenderEmailAddress
    'Save it a variable like you asked.
    savedDomain = Mid(emailAddress, InStrRev(emailAddress, "@") + 1)
    'But... State loss can do weird things, so I'd save it to registry
    VBA.SaveSetting "MyExampleProgram", "SomeSectionName", "SavedDomain", savedDomain
    'You get it back like so:
    MsgBox VBA.GetSetting("MyExampleProgram", "SomeSectionName", "SavedDomain", vbNullString)
End Sub
Pillgram
  • 804
  • 6
  • 11
  • The downvoter may not consider it good use of time to comment on one-day user posts, or at all on downvotes. Some receivers of downvotes do not see a comment as a learning opportunity so the downvoters choose to remain anonymous. As to the reason for downvoting see these for possible reasons. http://meta.stackoverflow.com/questions/260828/do-we-need-a-close-reason-for-zero-effort-questions and http://meta.stackoverflow.com/questions/270903/how-to-reduce-and-quickly-close-questions-that-are-merely-requirements. – niton Feb 15 '15 at 12:24
  • Yes well, I admit I get annoyed with people who don't seem to put forth effort, but VBA is often someone's first language or first attempt to code. You get a lot of power users who are trying to spread their wings. So I usually cut people some slack within that tag. I also am not a big fan of crushing brand new users. Just my opinion. There are others:) – Pillgram Feb 15 '15 at 20:41