rcv msg, parse, execute program with parsed text?

  • Thread starter Thread starter Mickey Ferguson
  • Start date Start date
M

Mickey Ferguson

I would like to create a rule (using Outlook 2003 SP1) where, if I receive a
message from a specific user, with a specific subject line, I would then
parse the text of the message (or just insert the entire text of the
message, if need be) and execute a program with that parsed text as command
line parameter to the executed program.

I'm familiar enough with creating rules that I can handle creating the rule
from the specific user and with a specific subject line. I imagine the rest
of it must be done by creating some kind of VB script that does the rest of
the work, and the rule would invoke this VB script. (If it's done another
way, I'm certainly open to that...) I'm plenty familiar with VB script, but
I don't know how to link up the two (the rule and executing the VB script).

Any help on how to do this would be greatly appreciated.

Mickey
 
This is the basic format for an Outlook VBA procedure that you an invoke with
a Rules Wizard "run a script" rule action:

Sub ConvertHTMLToPlain(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)

' your code to work with olMail goes here

Set olMail = Nothing
Set olNS = Nothing
End Sub

olMail is the message received.
 
It's almost good. How do I prevent that annoying "A program is trying to
access e-mail addresses you have stored in Outlook" warning that occurs
because your macro is running?

Here is my script:

Private Declare Function WritePrivateProfileString Lib "kernel32" _
Alias
"WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) _
As Long

Sub UpdateHomeIPAddress(Item As Outlook.MailItem)
Dim sIPAddress As String, sPath As String, sFile As String

sIPAddress = Mid(Item.Body, InStr(Item.Body, " is ") + 4)
sPath = "C:\Program Files\RealVNC\"
sFile = Dir(sPath & "*.vnc")
While (sFile <> "")
WritePrivateProfileString "Connection", "Host", sIPAddress, sPath &
sFile
sFile = Dir
Wend

End Sub

And incidentally, if anyone knows how to prevent WritePrivateProfileString
from putting an extra blank line after it writes the 'Host' entry to the
[Connection] section, that would be appreciated. Here's what the file looks
like before and after.

*** Before ***
[Connection]
Host=1.2.3.4
[Options]
UseLocalCursor=1

*** End Before ***

*** After ***
[Connection]
Host=1.2.3.4

[Options]
UseLocalCursor=1

*** End After ***
 
Outlook version? If it's Outlook 2003, take another look at the procedure I
posted and note how it uses the GetItemFromID method to get an item that is
trusted with regard to the security prompts. Otherwise, see
http://www.outlookcode.com/d/sec.htm for your ooptions
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
I re-coded it, and now I get nothing at all. I should at least hit my
MsgBox calls, shouldn't I? Maybe there is something obvious I've messed up?

Sub UpdateHomeIPAddress(MyMail As Outlook.MailItem)
Dim strID As String, sIPAddress As String, sPath As String, sFile As
String
Dim olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem

MsgBox "Starting macro"
strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)

sIPAddress = Mid(olMail.Body, InStr(olMail.Body, " is ") + 4)
MsgBox "IP Address " & sIPAddress
sPath = "C:\Program Files\RealVNC\"
sFile = Dir(sPath & "*.vnc")
While (sFile <> "")
MsgBox "Updating " & sFile & " with IP Address " & sIPAddress
WritePrivateProfileString "Connection", "Host", sIPAddress, _
sPath & sFile
sFile = Dir
Wend

Set olMail = Nothing
Set olNS = Nothing

End Sub
 
How did you test? With another VBA procedure? With a rule?
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
I created a rule that runs the script. That's how I did my previous
iteration, where I was getting the warning, and prompted me to ask, and you
recommended I re-code it as I coded it here.
 
Does other VBA code still work? What happens if you add a breakpoint?
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
I have no other VBA code, and breakpoints aren't going to do much because
it's not even hitting the very first line in the macro (a simple MsgBox
call).

Does other VBA code still work? What happens if you add a breakpoint?
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
If you haven't tested other VBA code, you can't know that VBA is working at all. Create and run at least a little Hello World sub.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
My VBA code should have been fine, since I tested it within a normal VB app.

But putting that aside, for some reason my macros aren't running at all. I
took everything out of my VbaProject.OTM file and left only the text below.

Sub MyTest(MyMail As Outlook.MailItem)

MsgBox "Hello world!"

End Sub

And then I created a rule that looked for a particular message (based on
subject matter). If it found it, it played a sound, ran MyMail macro and
stopped processing more rules. When I run the rule, it finds the message
(if it's there) and plays the sound, but I get no message box.

Any idea how to troubleshoot not even running the macro?

If you haven't tested other VBA code, you can't know that VBA is working at
all. Create and run at least a little Hello World sub.
 
This is exactly why I asked you to conduct a simple test to see whether any VBA code runs at all. Check Help | About | Disabled Items and Tools | Macro | Security. You may also need to exit Outlook and shut down any external programs, such as PDA sync utilities, that use Outlook. Use the Task Manager to make sure Outlook.exe has shut down completely before you restart it and try your code again.
 
Thanks for the extra info. That pointed me to the problem - making the
security changes required shutting down Outlook. Macros are now running
again, and I'm not getting the warnings any longer.

This is exactly why I asked you to conduct a simple test to see whether any
VBA code runs at all. Check Help | About | Disabled Items and Tools | Macro
| Security. You may also need to exit Outlook and shut down any external
programs, such as PDA sync utilities, that use Outlook. Use the Task Manager
to make sure Outlook.exe has shut down completely before you restart it and
try your code again.
 
Back
Top