简体   繁体   中英

Outlook creating a new email with lookup to a csv file using vba

I have an incoming email. When it arrives I need a rule to create a new email (I already can do this successfully in vba). I want to use a value in the body of the email to lookup in a csv file to see if the incoming email should generate an outgoing email or not and also pick up a full name from the csv file. I was thinking about loading the csv file into an array in vba?? eg File

Name             Full Name
CEEABS           Absolute Scent
CEEJKL           Jelly and Kohl
ANDFED           Overall Federation

Body of the incoming email will have something like

Description:  This is a communication from the Estates
LoadName:     CEEJKL
ProviderName: The Nice to have company
ID:           45672-ddc4-4leo-ba6d-faci1b631
TinyID:       6919

Thanks - Chrissy

I understand how to run rules with scripting - I have loads already done including creating new emails and output files. But I have never looked up an external file from within a script running from Outlook. That is what I need help with.

First you need to know what your email looks like to a VBA macro. Try the macro in https://stackoverflow.com/a/58000707/973283 . This is a diagnostic macro that allows you to see properties of an email. In particular, it will output the entire Text and Html bodies to a file in a human-readable format. The macro needs references to "Microsoft Scripting Runtime" and "Microsoft ActiveX Data Objects nn Library". Ask if you do not know what that means.

Near the top of the macro, you will find #Const Selected = True . Replace that with #Const Selected = False . Select one of these emails and then run macro InvestigateEmails() . Study at file “InvestigateEmails.txt” which will be on your desktop. My guess is the Text body will be something like:

Description:‹t›This is a communication from the Estates‹c›‹l›
LoadName:‹t›CEEJKL‹c›‹l›
ProviderName:‹t›The Nice to have company‹c›‹l›
ID:‹t›45672-ddc4-4leo-ba6d-faci1b631‹c›‹l›
TinyID:‹t›6919‹c›‹l›

In the above guess: ‹t› represents tab , ‹c› represents carriage return and ‹l› represents linefeed . “If LoadName:” is followed by spaces, you will see something like ‹s 5› which represents 5 spaces.

There may also be an Html body. It may be easier to extract the LoadName from the Hmtl body if it is something simple like:

<table>
  <tr><td>Description:</td><td>This is a communication from the Estates</td></tr>
  <tr><td>LoadName:</td><td>CEEJKL</td></tr>
  <tr><td>ProviderName:</td><td>The Nice to have company</td></tr>
  <tr><td>ID:</td><td>45672-ddc4-4leo-ba6d-faci1b631</td></tr>
  <tr><td>TinyID:</td><td>6919</td></tr>
</table>

Please report back what you see in file “InvestigateEmails.txt”.

I created an email to myself containing a table containing the data from your email. I used my diagnostic macro to create “InvestigateEmails.txt”. The important section of that text file are:

Text: |Description:‹2 crlf›|
      |This is a communication from the Estates‹2 crlf›|
      |LoadName:‹2 crlf›|
      |CEEJKL‹2 crlf›|
      |ProviderName:‹2 crlf›|
      |The Nice to have company‹2 crlf›|
      |ID:‹2 crlf›|
      |45672-ddc4-4leo-ba6d-faci1b631‹2 crlf›|
      |TinyID:‹2 crlf›|
      |6919‹2 crlf›|
      | ‹crlf›|
Html: |<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmln|
 : : : : : :
      |WordSection1><table class=MsoTableGrid border=1 cellspacing=0 cellpadding=0 style='border-collapse:c|
      |ollapse;border:none'><tr><td width=312 valign=top style='width:233.75pt;border:solid windowtext 1.0p|
      |t;padding:0cm 5.4pt 0cm 5.4pt'><p class=MsoNormal><span style='font-size:10.0pt;font-family:"inherit|
      |",serif;color:#242729;border:none windowtext 1.0pt;padding:0cm;mso-fareast-language:EN-GB'>Descripti|
      |on:</span><o:p></o:p></p></td><td width=312 valign=top style='width:233.75pt;border:solid windowtext|
      | 1.0pt;border-left:none;padding:0cm 5.4pt 0cm 5.4pt'><p class=MsoNormal><span style='font-size:10.0p|
      |t;font-family:"inherit",serif;color:#242729;border:none windowtext 1.0pt;padding:0cm;mso-fareast-lan|
      |guage:EN-GB'>This is a communication from the Estates</span><o:p></o:p></p></td></tr><tr><td width=3|
      |12 valign=top style='width:233.75pt;border:solid windowtext 1.0pt;border-top:none;padding:0cm 5.4pt |
      |0cm 5.4pt'><p class=MsoNormal><span style='font-size:10.0pt;font-family:"inherit",serif;color:#24272|
      |9;border:none windowtext 1.0pt;padding:0cm;mso-fareast-language:EN-GB'>LoadName:</span><o:p></o:p></|
      |p></td><td width=312 valign=top style='width:233.75pt;border-top:none;border-left:none;border-bottom|
      |:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt'><p class=Ms|
      |oNormal><span style='font-size:10.0pt;font-family:"inherit",serif;color:#242729;border:none windowte|
      |xt 1.0pt;padding:0cm;mso-fareast-language:EN-GB'>CEEJKL</span><o:p></o:p></p></td></tr><tr><td width|
      |=312 valign=top style='width:233.75pt;border:solid windowtext 1.0pt;border-top:none;padding:0cm 5.4p|
      |t 0cm 5.4pt'><p class=MsoNormal><span style='font-size:10.0pt;font-family:"inherit",serif;color:#242|
      |729;border:none windowtext 1.0pt;padding:0cm;mso-fareast-language:EN-GB'>ProviderName:</span><o:p></|
      |o:p></p></td><td width=312 valign=top style='width:233.75pt;border-top:none;border-left:none;border-|
      |bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt'><p cl|
      |ass=MsoNormal><span style='font-size:10.0pt;font-family:"inherit",serif;color:#242729;border:none wi|
      |ndowtext 1.0pt;padding:0cm;mso-fareast-language:EN-GB'>The Nice to have company</span><o:p></o:p></p|
      |></td></tr><tr><td width=312 valign=top style='width:233.75pt;border:solid windowtext 1.0pt;border-t|
      |op:none;padding:0cm 5.4pt 0cm 5.4pt'><p class=MsoNormal><span style='font-size:10.0pt;font-family:"i|
      |nherit",serif;color:#242729;border:none windowtext 1.0pt;padding:0cm;mso-fareast-language:EN-GB'>ID:|
      |</span><o:p></o:p></p></td><td width=312 valign=top style='width:233.75pt;border-top:none;border-lef|
      |t:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0c|
      |m 5.4pt'><p class=MsoNormal><span style='font-size:10.0pt;font-family:"inherit",serif;color:#242729;|
      |border:none windowtext 1.0pt;padding:0cm;mso-fareast-language:EN-GB'>45672-ddc4-4leo-ba6d-faci1b631<|
      |/span><o:p></o:p></p></td></tr><tr><td width=312 valign=top style='width:233.75pt;border:solid windo|
      |wtext 1.0pt;border-top:none;padding:0cm 5.4pt 0cm 5.4pt'><p class=MsoNormal><span style='font-size:1|
      |0.0pt;font-family:"inherit",serif;color:#242729;border:none windowtext 1.0pt;padding:0cm;mso-fareast|
      |-language:EN-GB'>TinyID:</span><o:p></o:p></p></td><td width=312 valign=top style='width:233.75pt;bo|
      |rder-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.|
      |0pt;padding:0cm 5.4pt 0cm 5.4pt'><p class=MsoNormal><span style='font-size:10.0pt;font-family:"inher|
      |it",serif;color:#242729;border:none windowtext 1.0pt;padding:0cm;mso-fareast-language:EN-GB'>6919</s|
      |pan><o:p></o:p></p></td></tr></table><p class=MsoNormal><o:p>&nbsp;</o:p></p></div></body></html>|

The Html body created by Outlook is full of formatting information. It is possible to extract data from this Html, but it would be easier to extract from the Text body. Almost every email I receive with an Html body has a Text body in this format. I am confident that when Outlook receives an email without a Text body, it creates one by taking the Html body, discarding all the tags and inserting two vbCrLfs after every discarded block tag. The resultant Text body is usually easy to process.

Ignoring the problem of extracting the company code from the email until the email's format is known, this Outlook macro demonstrates how to open a workbook and extract a cell value.

Option Explicit
Sub DemoGetCompanyName()

  ' Required reference to "Microsoft Excel nn.n Object Library" where "nn.n"
  ' depends on the version of Office installed.

  ' Change to reference your workbook and worksheet
  Const WbkName As String = "CompanyCodes.xlsx"
  Const WshtName As String = "Codes"

  ' I avoid names starting "xl" or "ol" so as to prevent a clash
  ' with a Microsoft name

  Dim CompCode As String
  Dim CompName As String
  Dim ExlApp As Excel.Application
  Dim Path As String
  Dim Rng As Excel.Range
  Dim WbkComp As Excel.Workbook
  Dim WshtCodes As Excel.Worksheet

  ' Replace with code to extract company code from email
  CompCode = "CEEJKL"

  Path = CreateObject("WScript.Shell").specialfolders("Desktop")

  Set ExlApp = Application.CreateObject("Excel.Application")

  With ExlApp
    .Visible = True         ' This slows your macro but helps during debugging
    .ScreenUpdating = False ' Reduces flash and increases speed if you write to workbook
    ' Open workbook read only
    Set WbkComp = .Workbooks.Open(Path & "\" & WbkName, , True)
  End With

  With WbkComp
    Set WshtCodes = .Worksheets(WshtName)
  End With

  With WshtCodes
    Set Rng = Cells.Find(What:=CompCode, After:=.Range("A1"), LookIn:=xlValues, _
                         LookAt:=xlWhole, SearchOrder:=xlByRows, _
                         SearchDirection:=xlNext, MatchCase:=False, _
                         SearchFormat:=False)
    If Rng Is Nothing Then
      ' Code not found
      Debug.Assert False
      CompName = ""
    Else
      CompName = .Cells(Rng.Row, 2).Value
    End If
  End With

  ' Close workbook and tidy up
  Set WshtCodes = Nothing
  WbkComp.Close
  Set WbkComp = Nothing
  ExlApp.Quit
  Set ExlApp = Nothing

  Debug.Print CompCode & " -> " & CompName

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM