how to get email address from messages in outlook 2003 - vb6

Sub GetALLEmailAddresses()
Dim objFolder As Folders
Set objFolder = Application.ActiveExplorer.Selection
Dim dic As New Dictionary
Dim strEmail As String
Dim strEmails As String
Dim objItem As MailItem
For Each objItem In objFolder.Items
strEmail = objItem.SenderEmailAddress
If Not dic.Exists(strEmail) Then
strEmails = strEmails + strEmail + ";"
dic.Add strEmail, ""
End If
Next
Debug.Print strEmails
End Sub
I use this code to get email address from message body. I'm not prefect in vb. is there any to help how to get email address from messages in outlook 2003?

In that case, I don't think there's anything built in, so I'd suggest that you don't bother with the SenderEmailAddress but instead just get out the Body and then search the text for email addresses. This will get somewhat complicated though since it might be difficult to be able to tell what's part of an email address and what isn't.
The easiest place to start with is to just look for any # in the text, and then search for the next whitespace on either side of the # and get everything between those whitespaces. But there are a lot of issues to think about. What if the user typed # for some other reason, or if the email contains something like The first email is xxx#test.com.The second email is xxx2#test.com (note the missing space between the . and the The), where your app might think that the email should be xxx#test.com.The.
Edited since my answer was based on a complete misunderstanding of the question.

Related

VBA - Export individual PowerPoint slides into PDF files, named by content field

I have about 15 different slide decks, that contain 3-4 slides each. Each deck is comprised of "certificates" to be broken out and given to each individual recipient, who is named on the certificate. This process is repeated every month.
ideally, I would like to enable a VBA script to extract each sheet to save as (or print as) a PDF, and save the file with the name of the recipient named on the slide... is this possible?
The best I have found is this code, (which doesn't work on my mac, but works fine on a VM) which DOES separate the slides into PDF's, but I would like the naming to be changed.
Any help would be greatly appreciated!!
Sub ExportSlidesToIndividualPDF()
Dim oPPT As Presentation, oSlide As Slide
Dim sPath As String, sExt As String
Set oPPT = ActivePresentation
sPath = oPPT.FullName & "_Slide_"
sExt = ".pdf"
For Each oSlide In oPPT.Slides
i = oSlide.SlideNumber
oSlide.Select
oPPT.ExportAsFixedFormat _
Path:=sPath & i & sExt, _
FixedFormatType:=ppFixedFormatTypePDF, _
RangeType:=ppPrintSelection
Next
Set oPPT = Nothing
End Sub
Edit for further clarification:
I'm forced to run the script in my Windows Virtual Machine, and when I do that, I'm losing all of my formatting that I set up in my slides, created in office '11 (which is one of my big reasons to export to PDF in the first place).
A little more about my project:
1. I am compiling a couple thousand lines of "actions" (data), to determine a winner for a recognition award, based on criteria for 4 different award categories, for 12-15 different executives.
2. Results are tallied, then I retrieve the winners information (name, ID, and the same for their manager and executive) from an LDAP directory. Then put into a csv for mail-merge at later step.
3. A Template slide deck is used for all slides, (12-15 executives have 3-4 winners each).
4. Individual PDF certificates are created from those slides.
5. Congratulatory message is drafted, fields pulled from prepared csv for mail-merge, and then exported to outlook 2011 outbox (client offline).
6. I attach each individual certificate to each message manually in the outbox.
7. Send.
I have compiled sample set of the files linked here: Recognition Files (Sample)
On each slide there are one or several shapes. For the sake of illustration, assume that the recipient's name is in the first shape.
Sub ExportSlidesToIndividualPDF()
Dim oPPT As Presentation, oSlide As Slide
Dim sPath As String, sExt As String
Set oPPT = ActivePresentation
sExt = ".pdf"
For Each oSlide In oPPT.Slides
'## Retrieve the recipient's name from the shape and append it to the exported path:
sPath = oPPT.FullName & oSlide.Shapes(1).TextFrame.TextRange.Text
i = oSlide.SlideNumber
oSlide.Select
oPPT.ExportAsFixedFormat _
Path:=sPath & sExt, _
FixedFormatType:=ppFixedFormatTypePDF, _
RangeType:=ppPrintSelection
Next
Set oPPT = Nothing
End Sub
It may be more complicated, depending on how the slides are configured (e.g., if there is a shape that contains the name and other text, you will need a function to parse out the proper name and omit the rest, etc.
Provide more detail, about the problem if you need further assistance.

Automating email to make address specific attachments

Besides programming, I am also a professional photographer. I recently set up an extremely successful photobooth... so successful I have about 500 photos, and a list of 60 emails (some pictures have multiple recipients)to send them to.
Email is the only way I can get the pictures to their respective owner, and I do not have time to manually send each photo to its owner in an email app, nor do I want to risk making mistakes in the process. All of the email addresses are in a text file, with the file locations of each image following the intended recipient. In addition to the pictures, I have one universal body text to send.
I have already looked into ASP, Macros, CDOSys, and some slightly confusing options with SMTP, but answers using these are more than welcome.
Any ideas on how to use the information in the text file to email each recipient with their photos?
tl;dr: I need to get 500 individual photos to 60 email addresses, without sending each picture manually, or giving someone someone else's photos. All of the information is organized in a text file.
Update: I have a few different versions of the text file. I have a text file straight out of a notepad editor, I have a csv file, and I have a version which delimits file paths with # and email addresses with tabs.
Using classic asp and web email server and assuming you can have created a csv file.
step 1. upload textFile.csv into server
with say emailAddress, pictureFileAddress etc.
step 2. import and emailer script (classic asp)
downloadFile = Server.MapPath("textFile.csv")
Set objFSO = CreateObject("Scripting.FileSystemObject")
if objFSO.FileExists(downloadFile) then
firstLine = true '... assume first line is header
Do Until objFile.AtEndOfLine OR finished
StrTemp = objFile.ReadLine
dataArray = Split(StrTemp, ",") '... assume just two fields for this e.g.
dataCount = UBound(dataArray)
email = dataArray(0)
picturefile = dataArray(1)
'... perform any data validation or Regexp
If email <> "" AND firstLine = false then
Set myMail=CreateObject("CDO.Message")
myMail.From = "yourfromemail"
myMail.ReplyTo = "yourreplyfromemail"
myMail.Subject = "Your picture"
'...create target file for attachment
fn1 = picturefile
fn2 = Server.MapPath(fn1)
myMail.AddAttachment fn2
myMail.HTMLBody = strMessage
myMail.Send
Set myMail = Nothing
end if
if firstLine then firstline = false
loop
End if
I hope this helps

Save images in Outlook 2007

Programmatically, of course.
Having already asked this question on superuser, I'm looking at writing a simple macro to pull down the displayed image in an HTML message (email or feed) in outlook 2007, and allow me to save it to disk.
Unfortunately, I havent been able to find where in the OL object model I can reference either linked images, or the html content itself. Finding attached files is easy, its the linked/displayed images that is my issue.
Any help? Of course, if you have a better non-programmatic answer, I'll be glad to see that - over on superuser, of course...
This is based on the MSDN docs. I don't have Outlook to test it.
Assuming you have an email message open, you can call GetInspector method on the MailItem instance that you have & use its HTMLEditor property to get handle to the DOM.
From here on, you can call regular methods such as document.Images to get handle to all the image elements. I don't know, how one can save it locally to a disk but I am sure, there must be some method to do it.
I had a second look at shahkalpeshs answer and came up with the following solution:
(Written in Outlook 2003)
Option Explicit
Private Sub getImages()
Dim xmlhttp_ As xmlhttp
Dim htmldoc As Object
Dim currentImage As Object
Dim currentResponse() As Byte
Dim startTime As Date
Dim maxTime As Long
Dim pathFolder As String
Dim pathFull As String
Dim nrFile As Integer
pathFolder = "C:\YourFolder\Images\" '"(small fix for stackoverflow syntaxhighlighter)
maxTime = 30 ' max time to load 1 File in seconds '
If Me.ActiveWindow.CurrentItem.GetInspector.EditorType = olEditorHTML Then
Set htmldoc = Me.ActiveWindow.CurrentItem.GetInspector.HTMLEditor
Set xmlhttp_ = New xmlhttp
For Each currentImage In htmldoc.images
xmlhttp_.Open "GET", currentImage.src
If Left(currentImage.src, 8) <> "BLOCKED:" Then
xmlhttp_.Send
startTime = Now
pathFull = pathFolder & currentImage.nameProp
pathFull = Replace(pathFull, "?", vbNullString)
pathFull = Replace(pathFull, "&", vbNullString)
Do While xmlhttp_.readyState <> 4
If DateTime.DateDiff("s", startTime, Now) > maxTime Then Exit Do
DoEvents
Loop
If xmlhttp_.readyState = 4 Then
If Dir(pathFull) <> "" Then Kill pathFull
nrFile = freeFile
Open pathFull For Binary As #nrFile
currentResponse = xmlhttp_.responseBody
Put #nrFile, , currentResponse
Close #nrFile
End If
End If
Next currentImage
End If
Set xmlhttp_ = Nothing
Set currentImage = Nothing
Set htmldoc = Nothing
End Sub
This code will download all the images that are displayed in your ActiveWindow and save them in a folder.
You will need to add a Reference to Microsoft XML (any version >= 2.6 should work) through Tools->References in the VBA Editor
If you want to, you can also set a Reference to the Microsoft HTML Object Library and change:
Dim htmldoc As Object
Dim currentImage As Object
to:
Dim htmldoc As HTMLDocument
Dim currentImage As HTMLImg
Regarding your comment:
#marg, Thanks for the detailed response. I still cant believe the solution has to be so convoluted - the image is already displaying, why should I have to download it again? And what if I want to save only a single image? (In Outlook 2003, you were able to right click on the image and select save as... now no more.) Since this is the closes to an actual workable solution, and since there doesnt seem to be any better solution in current Outlook - i'm giving you the bounty...
I don't have 2007 to look for a non-programming solution.
I do not believe that the MailItem Object (CurrentItemin my solution is a MailItem) differs much between the versions (but I base that assumption on 0 % research :D) and I was not able to locate the direct local path where the displayed images are stored even though I am pretty sure they should be in your browsers cache-folder. Crawling your folder for a file with the name currentImage.nameProp and copying it to your destination folder would be an alternative solution. Simply redownloading the image should not be that bad :D

How do I close Word (or other app) if an error occurs in a VBScript?

I wrote a VBScript app to open Word and Excel documents and search and replace blocks of text and various sections, pulling the new text from a plain text file. I purposely avoided any error checking, primarily because I couldn't figure it out at the time (and the script ran reliably anyway). Now months later on my local machine, I am inexplicably getting error messages about Normal.dot being changed and a message box asking what I want to do about it (which requires three more dialogs to finally answer). Of course this kills my ability to run the script and simply walk away, as it causes the script to fail. Currently when this happens, I have to open the Task Manager, find Winword.exe (of which the GUI isn't running) and kill it then re-run my script.
What's a reasonable way of catching the error and successfully shutting down Word (or Excel). Based on this question I'm trying this:
Set objDoc = objWord.Documents.Open(curDir1 + "\docs\template_spec.dot")
If Err.Number <> 0 Then
WScript.Echo "Error in Word Open:" & Err.Description
objWord.Quit
Else
Set objSelection = objWord.Selection
'Do replacement activities'
ReplaceText(objSelection)
objDoc.SaveAs(curDir1 + "\docs\mynewdocument.doc")
objWord.Quit
End If
Set objShell = Nothing
Set objWord = Nothing
Set objExcel = Nothing
Of course, as fate would have it, I cannot replicate the problem, so it works like normal. Does this solution seem reasonable? And a side question: How the heck do I get Word to stop complaining about Normal.dot (or get the script to handle it)? It's as if Word leaves itself open in the background after I have closed the GUI in some cases.
have you considered wrapping everything into an 'On Error Resume Next' statement so that your script ignores all the errors and continues to run as much as possible before calling the objWord.quit regardless of success or fail.
if you want more information on the correct use of 'On Error Resume Next' then go over to the msdn article on it!
Hope this helps!
Paul
I'm afraid that
WScript.Echo "..."
if it ever fires, is going to stall your script. Other than that, everything looks right. I'll play with it when I get home.
Edit: Word does hang out in the background, quite frequently. For one thing, if you use Outlook, and use Word as your Outlook editor, Word won't go away until Outlook is gone.
I'd agree with the use of "on error resume next".
If you really need to forcefully terminate Word, you can use WMI and the Win32_Process class to find and kill the process. This should be a last resort if everything else fails.
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'winword.exe'")
For Each objProcess in colProcess
objProcess.Terminate()
Next
This was a modified example from:
http://www.computerperformance.co.uk/vbscript/wmi_process_stop.htm
Also, make sure all your references to the Word automation object are closed and/or set to nothing before you terminate the process.
The most reliable way to terminate all ActiveX instances, clean up garbage, and release resources is to put the code for that purpose into Sub Class_Terminate() of a dummy class, created instance of the class allows to handle script quit event.
Option Explicit
Dim objBeforeQuitHandler, objWord
' create a dummy class instance
Set objBeforeQuitHandler = New clsBeforeQuitHandler
' create word app instance
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Add.ActiveWindow.Selection.TypeText "80040000 error was raised. About to terminate the script." & vbCrLf & "Word will be quitted without saving before script termination just you close popped up error message."
' your code here...
' raise an error
Err.Raise vbObjectError
Class clsBeforeQuitHandler
' dummy class for wrapping script quit event handler
Private Sub Class_Terminate()
Dim objDoc
On Error Resume Next ' to prevent errors in case of unexpected word app termination
If TypeName(objWord) <> "Object" Then ' word app has not been closed yet
objWord.DisplayAlerts = False
For Each objDoc In objWord.Documents
objDoc.Saved = True ' to prevent save as dialog popping up
objDoc.Close
Next
objWord.Quit
End If
End Sub
End Class

Insert data from registry into predefined bookmarks

I've got data in the registry under Current User which I want to add into predefined bookmarks in Word documents. Now, the thing is, I want the exact same macro in every single Word document and have the macro skip the step if it can't find the predefined bookmark.
I've got this code already where I need to predefine an array of the total data I've got in the registry. The macro stores the values of each subkey from the registry into a string. But doesn't insert the value into the bookmark unless the bookmark exists. Well, that's the idea anyway.
Public Sub TemplateData()
Dim objShell
Dim strShell
Dim strDataArea
Dim Values() As String
Dim Fields
' Input the exact same key as in registry
Fields = Array("DEPARTMENT", "LETTER", "LNAME", "FNAME")
Set objShell = CreateObject("Wscript.Shell")
strDataArea = "HKCU\Templates\"
On Error Resume Next
For iTeller = 0 To UBound(Fields)
Dim sBookMarkName, sValue
sBookMarkName = "Bookmark" & Fields(iTeller)
sValue = objShell.RegRead(strDataArea & Fields(iTeller))
Selection.GoTo What:=wdGoToBookmark, Name:=sBookMarkName
Selection.Delete Unit:=wdCharacter, Count:=0
Selection.InsertAfter sValue
Next
On Error GoTo 0
End Sub
If I put a breakpoint on the bottom Next, I can see that it indeed finds the bookmarks, but somehow it inserts the values of every registry subkey until it finds the correct one. And the initial idea is also to have the macro delete whatever words are in the bookmkarks if I run the macro again since if the data in the registry get updates, the macro should pick that up and insert it into the document.
Can anyone find what might be wrong here?
Error in a Word VBA macro, trying to insert values into bookmarks
has answers. Kenny learned himself (re-set the Bookmark):
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=sBookMarkName
and Tom helped to check before use...
If ActiveDocument.Bookmarks.Exists(sBookmarkName) Then
... insert using your code
End If

Resources