I hope someone can help me with this. A few months ago I was able to write a macro for Outlook 2003 to add the filenames of all the attachment in an email message, which I really need for my line of work.
However, if I switch the default editor to Word, the macro doesn’t even appear; I guess it has to be incorporated into Word’s normal.dot or something. If I add it to VB from Word, I can see the macro, but I get all sorts of errors.
Hopefully someone can point me in the right direction for this. My current macro, which works in “normal” Outlook messages (not those created with Word Editor) is this:
Sub Names()
Dim Atmt As Attachment
Dim Mensaje As Outlook.MailItem
Dim Adjuntos As String
Set Mensaje = Application.ActiveInspector.CurrentItem
Mensaje.BodyFormat = olFormatHTML
i = 0
Adjuntos = ""
For Each Atmt In Mensaje.Attachments
'If Atmt.Size > 5 Then
Adjuntos = "<HMTL> ** Attached file: <u> " & Atmt.FileName & " </u> </html> <br>" & vbNewLine & Adjuntos
i = i + 1
'End If
Next Atmt
Adjuntos = "<HMTL> <u> <b> Total number of attached files: " & i & "</u></b> </html> <br>" & Adjuntos & vbNewLine
Mensaje.HTMLBody = Adjuntos & Mensaje.HTMLBody
Set Mensaje = Nothing
End Sub
The difference between the two code blocks below is simply the linking of the Outlook object library. In Outlook, this is not necessary, but from Word, you need to either include the library as a reference to your Word project, or else use late binding (the method I demonstrate below).
Late binding links the Outlook library to an object in your code/project,
OLKin this case, and allows you to use the associated functions without needing to perform any additional steps/save any additional files.Linking the library should also work, but since this is not a normal Word project that you can reference later/a new Word project is created for each new email, I’m thinking (though I did not test) that you would need to include the code in your
Normaltemplate, which means that code will be available on any Word document you create, unless you specify a different template.This may or may not be what you want to do, but if it is, then just past the Outlook code into your
Normaltemplate and link the Outlook library as a reference.From MS Outlook (preferred method)
This will work, even with WORD as the email editing application, when pasted into an OUTLOOK project:
From MS Word/Within the new mail item
I was able to get the following to work, but you should note that I got security warnings (normal, unavoidable AFAIK) that must be pushed through with user intervention.
Paste the below into your WORD project (open mail item) and run it. You should also be able to put this in the
Normaltemplate, but that means the macro will always be available, which may or may not be a problem for you.