VB: Putting filenames in a message body.
October 13, 2008 11:25 PM   Subscribe

VB Script Help: I would like to put the filenames of my attachments into the message body of my emails. How do I do that?

I'm not a programmer - I'm just using a script that I ran across somewhere on the Internet. I'm using Outlook 2003 on XP. So far this script renames the subject, adds a quick line to the body and then sends the message. I would like it to add the file names of the attachments (plural) to the body after my "inspirational message." Bonus: Can I point my current olItem.Body at a text file?
Sub ChangeSubject()
Set oActiveExplorer = Application.ActiveExplorer
MsgBox TypeName(oActiveExplorer.Selection)
Set oSelection = oActiveExplorer.Selection
    For I = 1 To oSelection.Count
        Set olItem = oSelection.Item(I)
        olItem.Subject = "Your weekly inspirational message"
        olItem.Body = "Inspirational Message"
        olItem.Send
    Next
End Sub
posted by bigmusic to Computers & Internet (5 answers total)
 
Not sure about VBScript, but in VBA to access the attachments

Dim myAttachments As Attachments

Set myAttachments = myEmailObject.Attachments

' Skip shortcuts and embedded messages
Do While myAttachments(AttachmentsCounter).Type = olOLE _
Or Left$(myAttachments(AttachmentsCounter).DisplayName, 8) = "Shortcut" _
Or InStr(myAttachments(AttachmentsCounter).FileName, ".msg") > 0
AttachmentsCounter = AttachmentsCounter - 1

DO SOMETHING WITH THE ATTACHMENT NAME perhaps

myEmailObject.Body = myEmailObject.Body & vbCrLf & myAttachments(AttachmentsCounter).DisplayName
Loop




Now, if you want to load the entire body in from a file you could just read it in from the file thus:

Dim f as long
Dim myText as string

f = FreeFile
Open WHATEVERYOURPATHIS for input as f
myText = Input$(LOF(f), #f)
Close #f

myEmail.body = mytext

(Aircode so may not run first time but you get the idea..)
posted by NailsTheCat at 3:45 AM on October 14, 2008


Response by poster: Shows what I know, this is a VBA script. I'm getting a 424 error when I try to insert your code into the for loop above olItem.Send - I'm not sure if I am doing this correctly.
posted by bigmusic at 2:35 PM on October 14, 2008


Best answer: I just tested the below code and it seemed to work for me. Note that I have declared all of the variables -- olItem is explicitly declared as MailItem which might be why you got that error.

Sub ChangeSubject()

Dim oActiveExplorer As Object
Dim oSelection As Object
Dim olItem As MailItem
Dim myAttachments As Attachments
Dim myAttach As Attachment
Dim i As Integer
Dim f As Long
Dim myText As String
Const MYPATH As String = "c:\temp\temp.txt"

Set oActiveExplorer = Application.ActiveExplorer

MsgBox TypeName(oActiveExplorer.Selection)

Set oSelection = oActiveExplorer.Selection

For i = 1 To oSelection.Count

Set olItem = oSelection.Item(i)

olItem.Subject = "Your weekly inspirational message"
olItem.Body = "Inspirational Message"

' (1) get text from a file and replace the body with this
f = FreeFile
Open MYPATH For Input As f
myText = Input$(LOF(f), #f)
olItem.Body = myText
Close #f

' (2) add the filename of each attachments
Set myAttachments = olItem.Attachments
' Skip shortcuts and embedded messages
For Each myAttach In myAttachments
With myAttach
If myAttach.Type <> olOLE _
And Left$(myAttach.DisplayName, 8) <> "Shortcut" _
And InStr(myAttach.FileName, ".msg") = 0 Then
olItem.Body = olItem.Body & vbCrLf & myAttach.DisplayName
End If
End With

Next myAttach

olItem.Save
olItem.Send

Next i

End Sub
posted by NailsTheCat at 3:30 PM on October 14, 2008


Response by poster: Works perfectly - thank you so much. I really need to learn how to program one of these days - I seem to want to customize so much of my software. It's just boggling to me that there is no general purpose language that I can use to script everything - greasemonkey for browswer customization, VBA for office stuff, AutoIT for GUI automation --- I'm not sure I have the time or the inclination to learn them all. Python is attractive to me - but I can't use it in any of my applications that I use everyday.
posted by bigmusic at 4:17 PM on October 14, 2008


Glad it worked! VBA is easy once you get the hang of it and there's a lot of resources online including the wonderful newsgroups (e.g. microsoft.public.outlook.*) -- I share your ambivalence about what to learn though.
posted by NailsTheCat at 5:09 PM on October 14, 2008


« Older Don't Bore Just Dance   |   I want my sweet sweet salsa! Newer »
This thread is closed to new comments.