Reputation: 23
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
Upvotes: 2
Views: 3631
Reputation: 4367
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, OLK
in 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 Normal
template, 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 Normal
template and link the Outlook library as a reference.
This will work, even with WORD as the email editing application, when pasted into an OUTLOOK project:
Option Explicit
Sub Names()
Dim Atmt As Attachment
Dim Mensaje As Outlook.MailItem
Dim Adjuntos As String
Dim Body As String
Dim i As Integer
Set Mensaje = Application.ActiveInspector.CurrentItem
Mensaje.BodyFormat = olFormatHTML
Body = Mensaje.HTMLBody
i = 0
Adjuntos = ""
For Each Atmt In Mensaje.Attachments
'If Atmt.Size > 5 Then
Adjuntos = Adjuntos & "** Attached file: <u> " & Atmt.FileName & " </u> <br>"
i = i + 1
'End If
Next Atmt
Adjuntos = "<u> <b> Total number of attached files: " & i & "</u></b> <br>" & Adjuntos
Mensaje.HTMLBody = Left(Body, InStr(Body, "</body>") - 1) & Adjuntos & Right(Body, Len(Body) - InStr(Body, "</body>") + 4)
Set Mensaje = Nothing
End Sub
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 Normal
template, but that means the macro will always be available, which may or may not be a problem for you.
Sub Names()
Dim OLK As Object 'Oulook.Application
Dim Atmt As Object 'Attachment
Dim Mensaje As Object 'Outlook.MailItem
Dim Adjuntos As String
Dim Body As String
Dim i As Integer
Set OLK = CreateObject("Outlook.Application")
Set Mensaje = OLK.ActiveInspector.CurrentItem
Mensaje.BodyFormat = 2 'olFormatHTML
Body = Mensaje.HTMLBody
i = 0
Adjuntos = ""
For Each Atmt In Mensaje.Attachments
'If Atmt.Size > 5 Then
Adjuntos = Adjuntos & "** Attached file: <u> " & Atmt.FileName & " </u> <br>"
i = i + 1
'End If
Next Atmt
Adjuntos = "<u> <b> Total number of attached files: " & i & "</u></b> <br>" & Adjuntos
Mensaje.HTMLBody = Left(Body, InStr(Body, "</body>") - 1) & Adjuntos & Right(Body, Len(Body) - InStr(Body, "</body>") + 4)
Set OLK = Nothing
Set Mensaje = Nothing
End Sub
Upvotes: 1