topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 5:57 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: .Intercepting/rewriting Outlook HTML emails automagically  (Read 8566 times)

CodeTRUCKER

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,085
    • View Profile
    • Donate to Member
.Intercepting/rewriting Outlook HTML emails automagically
« on: February 03, 2008, 01:08 AM »
Below is some code I scavenged from the Internet for a macro that will take attached images in Outlook 2002 and display them in a window. 

Does anyone know how to intercept an email w/ pix attchments in a locale Outlook client and "rewrite" the HTML so the pix display in the body automagically?

I have tried some experiments with the code below, but I don't know how to intercept the email before it is displayed in my client.

Is this possible?


Thanks! :)



Code: Visual Basic [Select]
  1. Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  2.  
  3.  
  4. Sub view_attachments()
  5. '***************************************************************
  6. ' ver. 1/30/04
  7. '  - Select one or multiple emails.
  8. '  - Copies files to 'Temporary Internet Files\view_attachments'
  9. '    (previously copied files are deleted each time it's run).
  10. '  - Only image files are displayed (no others are executed).
  11. '  - Right-click images to 'Save As', 'Email', 'Print', etc.
  12. '  - Hover over image to see original size & scaled size.
  13. '  - Clicking each image will toggle between original size
  14. '    & browser width (unless original size is smaller).
  15. '  - To scale all images to browser width, resize the browser,
  16. '    right-click on background & choose 'Refresh'.
  17. '***************************************************************
  18. On Error Resume Next
  19.  
  20.  
  21.     Dim oOL As Outlook.Application
  22.     Dim oSelection As Outlook.Selection
  23.  
  24.  
  25.     Set oOL = New Outlook.Application
  26.     Set oSelection = oOL.ActiveExplorer.Selection
  27.     Set objShell = CreateObject("WScript.Shell")
  28.     Set fs = CreateObject("Scripting.FileSystemObject")
  29.  
  30.  
  31.     vTempInt = objShell.RegRead("HKCU\software\microsoft\" _
  32.       & "Windows\CurrentVersion\Explorer\Shell Folders\Cache")
  33.     vPath = vTempInt & "\view_attachments\"
  34.  
  35.  
  36.     If fs.FolderExists(vPath) Then
  37.         fs.DeleteFile (vPath & "*.*")
  38.     Else
  39.         fs.CreateFolder vPath
  40.     End If
  41.  
  42.  
  43.     vBkgrColor = "000000"
  44.     vFontColor = "FFFFFF"
  45.     vHTMLBody = "<HTML><title>View Email Attachments</title>" _
  46.       & "<body bgcolor=#" & vBkgrColor & " link=#" & vFontColor _
  47.       & " alink=#" & vFontColor & " vlink=#" & vFontColor _
  48.       & "><font face=Arial size=3 color=#" & vFontColor & ">"
  49.  
  50.  
  51.     vEmailNum = 0
  52.     For Each obj In oSelection
  53.         vEmailNum = vEmailNum + 10
  54.         vSubject = "Attachments from: <a href=""Outlook:" _
  55.           & obj.EntryID & """><b>" & obj.Subject & "</b></a><br>"
  56.         vHTMLBody = vHTMLBody & vSubject
  57.         vAttachNum = vEmailNum
  58.         For Each Attachment In obj.Attachments
  59.             vAttachNum = vAttachNum + 1
  60.             vImg = "document.img" & vAttachNum
  61.             vWidth = "document.body.clientWidth - 20"
  62.             Attachment.SaveAsFile (vPath & Attachment.FileName)
  63.             vHTMLBody = vHTMLBody _
  64.               & "<b>" & Attachment.FileName & "</b><br>" _
  65.               & "<a href=""javascript:fWidth(" & vImg & ");"">" _
  66.               & "<center><IMG name=""img" & vAttachNum & """ alt="""" hspace=0 " _
  67.               & "src=""" & vPath & Attachment.FileName & """ align=baseline " _
  68.               & "border=0 " & "onload=""vOrig=String(" & vImg & ".width)" _
  69.               & "+ ' x ' + String(" & vImg & ".height);vRatio=(" & vWidth _
  70.               & ")/" & vImg & ".width;" & vImg & ".alt='Original Size: ' + " _
  71.               & "vOrig + '\n  Scaled Size: ';if(" & vImg & ".width <=" _
  72.               & vWidth & "){" & vImg & ".alt=" & vImg & ".alt + vOrig;}" _
  73.               & "else{" & vImg & ".alt=" & vImg & ".alt + String(" & vWidth _
  74.               & ")+ ' x ' + String(Math.round(vRatio *" & vImg & ".height));}" _
  75.               & "if (" & vImg & ".width >" & vWidth & "){" & vImg & ".width = " _
  76.               & vWidth & ";}""></center></a><br><br><br>"
  77.         Next
  78.         vHTMLBody = vHTMLBody & "</a><br><br>"
  79.     Next
  80.  
  81.  
  82.     If Not vImg = "" Then
  83.         vHTMLBody = vHTMLBody & "<script>function fWidth (vImg){" _
  84.           & "vCRLF=vImg.alt.indexOf('\n');vOrgWidth=vImg.alt.substring" _
  85.           & "(vImg.alt.indexOf(':')+2, vImg.alt.indexOf('x')-1);" _
  86.           & "if(vImg.width == " & vWidth & "|| vOrgWidth <= " & vWidth _
  87.           & "){vImg.width=vOrgWidth;vImg.alt=vImg.alt.substring(0,vCRLF)" _
  88.           & "+ '\n  Scaled Size: '+ vImg.alt.substring(vImg.alt." _
  89.           & "indexOf(':')+2,vCRLF);}else{vImg.width=" & vWidth & ";" _
  90.           & "vImg.alt=vImg.alt.substring(0,vCRLF) + '\n  Scaled Size: '" _
  91.           & "+ String(" & vWidth & ")+ ' x ' + String(vImg.height);}}</script>"
  92.     End If
  93.  
  94.  
  95.     vHTMLBody = vHTMLBody & "</font></body></html>"
  96.  
  97.  
  98.     Set ie = CreateObject("internetexplorer.application")
  99.     With ie
  100.         .toolbar = 0
  101.         .menubar = 0
  102.         .statusbar = 0
  103.         .Left = 100
  104.         .Top = 50
  105.         .Height = 750
  106.         .Width = 1000
  107.         .navigate "about:blank"
  108.         .document.Open
  109.         .document.Write vHTMLBody
  110.         .document.Close
  111.         .Visible = True
  112.     End With
  113.  
  114.  
  115.     vTimer = 0
  116.     Do Until ie.readyState = 4 Or vTimer = 10000
  117.         Sleep 10
  118.         vTimer = vTimer + 10
  119.     Loop
  120.  
  121.  
  122.     Set ie = Nothing
  123.     Set fs = Nothing
  124.     Set objShell = Nothing
  125.     Set oSelection = Nothing
  126.     Set oOL = Nothing
  127. End Sub
« Last Edit: February 07, 2008, 06:43 PM by CodeTRUCKER »

tinjaw

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,927
    • View Profile
    • Donate to Member
Re: Intercepting/rewriting Outlook HTML emails automagically
« Reply #1 on: February 03, 2008, 06:18 AM »
codeTRUCKER,

Off the top of my head I can think of two ways to do that.

1) Put something in the middle of Outlook and the mail server. Just like a proxy. That proxy would introspect all incoming emails (which are just strings of ASCII text - binaries are MIME encoded) and rewrite the emails as desired. You could use the tool of you choice to create this proxy.

2) Since you are using Outlook, you can use VBA (or any scripting language compatible with the scripting host, like Jscript) and interact with Outlook. You basically make an add-on for Outlook.

BigJim

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 178
  • I have seen the light!
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Intercepting/rewriting Outlook HTML emails automagically
« Reply #2 on: February 05, 2008, 12:06 AM »
2) Since you are using Outlook, you can use VBA (or any scripting language compatible with the scripting host, like Jscript) and interact with Outlook.

I sure wish somebody would do that. It's so annoying that Outlook handles attached images so poorly. M/$ says it's because of security. Humbug! From time to time I go hunting for a solution and am again amazed that there seem to be few or no solutions available.

I had an application a while back (can't recall the name just now) which worked OK. Would have gladly coughed up $10 for it but all the links to buy it were dead. Then the trial expired and it gaged.

The app didn't display the images in the message but when you clicked on any of several attached images and your viewer launched you could then page through all of them instead of having to go back to the message and open each one individually.
TruckerJim says "You can go down a hill too slow a thousand times. But too fast only once."

CodeTRUCKER

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,085
    • View Profile
    • Donate to Member
Re: Intercepting/rewriting Outlook HTML emails automagically
« Reply #3 on: February 07, 2008, 01:56 AM »
2) Since you are using Outlook, you can use VBA (or any scripting language compatible with the scripting host, like Jscript) and interact with Outlook.

I sure wish somebody would do that. It's so annoying that Outlook handles attached images so poorly. M/$ says it's because of security. Humbug! From time to time I go hunting for a solution and am again amazed that there seem to be few or no solutions available.

Hi Jim,

Me too!  I am looking into this as I am not satisfied with the way OE handles it.  I know "what" to do to make it work, but I have to learn "how" to program it (read: learn syntax).   

I had an application a while back (can't recall the name just now) which worked OK. Would have gladly coughed up $10 for it but all the links to buy it were dead. Then the trial expired and it gaged.

The app didn't display the images in the message but when you clicked on any of several attached images and your viewer launched you could then page through all of them instead of having to go back to the message and open each one individually.

The macro I posted in the first post above will provide the function you have described.  :) 
After the macro has been saved, I reccomend creating a button to put on the toolbar in OE.  When you have picture attachments in a mail, just click run the macro and voila!  A simple browser will open with all the pictures and if you select multiple mails in the message list, it will display all the pictures in all the mails in one window.  Scroll down to see all of them.

Hope this helps.

Fair winds!


BigJim

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 178
  • I have seen the light!
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Intercepting/rewriting Outlook HTML emails automagically
« Reply #4 on: February 07, 2008, 11:12 AM »
Sounds interesting. And thanks!

This is embarrassingly lame but when I try to copy/paste your script into the VB editor it keeps throwing in CR/LF's and it won't run. Needless to say, an avid participant I am. But a coder I'm not!

Help

VB example.jpg
TruckerJim says "You can go down a hill too slow a thousand times. But too fast only once."

CodeTRUCKER

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,085
    • View Profile
    • Donate to Member
Re: Intercepting/rewriting Outlook HTML emails automagically
« Reply #5 on: February 07, 2008, 11:30 AM »
Sounds interesting. And thanks!

... when I try to copy/paste your script into the VB editor it keeps throwing in CR/LF's and it won't run. Needless to say, an avid participant I am. But a coder I'm not!


Jim, You may not be a coder, but you are no novice to computers! :)  I chose to make the directions pretty granular so even the least experienced should be able to make this work.  In time, I should be able to automate the following, but for now it as to be manual.  Let us know how it goes.

Ok, follow these directions and it should get you going... 

The first thing we need to do is open the macro editor...

  • In OL, click on the menubar... Tools >> Macro >> Macros.
  • When the window opens type "ThumbViewer" in the "Macro Name" box and click the "Create" button.  This will open a "Visual Basic for Outlook" (VB) IDE.  Leave this open.

Second, we need to open the attached text file which has the syntax we need and send it to the clipboard....

  • Now, come back to the attached text file on this post and open it.  It is just text and will open fairly instantaneous.  You can save it if you want to.  Make sure it only opens in Not Pad or other text-only editor.  It will be unusable if it opens in MS Word or other word processor because there are hidden formatting characters that will be copied which will corrupt the syntax.
  • Click anywhere on the text.
  • Press "CTRL + A" to select the entire text.
  • Press "CTRL + C" to copy the text to the clipboard.

Now we have the code in our hands we need to put it into the IDE and save the project

  • Switch back to the IDE window.
  • Click anywhere on the text.
  • Press "CTRL + A" to select the entire text.
  • Hit the "Delete" key to clear out all contents.
  • Press "CTRL + V" to paste the syntax text from the clipboard into the IDE.
  • Save and close the IDE.

Now that we are back in OL and if everything worked OK you should be able to select any email(s) with a picture attachment(s) and execute the "ThumbViewer" macro.
 
  • In the list of emails, select one or multiple emails.
  • In OL, click on the menubar... Tools >> Macro >> Macros.
  • When the window opens, you should see "ThumbViewer" in the "Macro Name" box.
  • Click the "Run" button.  This will open a simple browser with all of the picture attachments displayed in "single-file."  You will have to scroll down to see the rest of your pictures.



« Last Edit: February 07, 2008, 04:28 PM by CodeTRUCKER »

BigJim

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 178
  • I have seen the light!
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Intercepting/rewriting Outlook HTML emails automagically
« Reply #6 on: February 07, 2008, 11:49 AM »
It's choking on the quotes (") in the first column. If I remove the first one it chokes on the ampersand (&).

Clearly, I don't know what I'm doing.  :-[
TruckerJim says "You can go down a hill too slow a thousand times. But too fast only once."

CodeTRUCKER

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,085
    • View Profile
    • Donate to Member
Re: Intercepting/rewriting Outlook HTML emails automagically
« Reply #7 on: February 07, 2008, 12:25 PM »
It's choking on the quotes (") in the first column. If I remove the first one it chokes on the ampersand (&).

Clearly, I don't know what I'm doing.  :-[

Not our doing... the SMF software is wrapping the lines and busting the syntax.  I will make a text file (no formatting) and attach it to the above post in just a minute.

BigJim

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 178
  • I have seen the light!
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Intercepting/rewriting Outlook HTML emails automagically
« Reply #8 on: February 07, 2008, 12:59 PM »
You're a prince. I don't care what they say about truck drivers!   :lol:
TruckerJim says "You can go down a hill too slow a thousand times. But too fast only once."

CodeTRUCKER

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,085
    • View Profile
    • Donate to Member
Re: Intercepting/rewriting Outlook HTML emails automagically
« Reply #9 on: February 07, 2008, 04:32 PM »
You're a prince. I don't care what they say about truck drivers!   :lol:

Thanks for the flowers! :) 

Try these instructions above

BigJim

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 178
  • I have seen the light!
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: .Intercepting/rewriting Outlook HTML emails automagically
« Reply #10 on: February 07, 2008, 07:01 PM »
Absolutely first class! Works a treat. Thanks driver!! That little ditty is going to avert untold frustration. 

(Just for the record, I had a handle on all the step-by-step. Just couldn't stop to try to figure out the problem with the code. But your excellent guide will make it easy and a pleasure to share your script with other frustrated Outlook users.)
TruckerJim says "You can go down a hill too slow a thousand times. But too fast only once."

CodeTRUCKER

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,085
    • View Profile
    • Donate to Member
Re: .Intercepting/rewriting Outlook HTML emails automagically
« Reply #11 on: February 07, 2008, 07:07 PM »
Absolutely first class! Works a treat. Thanks driver!! That little ditty is going to avert untold frustration. 

(Just for the record, I had a handle on all the step-by-step. Just couldn't stop to try to figure out the problem with the code. But your excellent guide will make it easy and a pleasure to share your script with other frustrated Outlook users.)

You're welcome!  Like I said... you are no computer novice!