Q: Attachment: How to insert filename of attachment in message body 
Author Message
 Q: Attachment: How to insert filename of attachment in message body

Hi,

many mails I send with Outlook2000 have one or more attachments.
In the body of the message I type the filename to refer to
the attachment(s).
How can I insert the filename of the attachment(s) automatically in
the body (at the end of the body) of the message during attaching the
file or before sending?

e.g.
Filenames are "testpic.jpg" + "testdoc.pdf"
Text which should inserted in message body is
"Attachment: testpic.jpg, testdoc.pdf"

Does anybody know a solution with vba?

All "solutions" I found so far: Getting the filenames of all
attachments in a outlook-folder.
That do not really help me.

Outlook 2000
Win2000
Postoffice Workgroup WinNT4 (no Exchange)

Greatings
Uwe



Sun, 17 Jul 2005 21:55:48 GMT  
 Q: Attachment: How to insert filename of attachment in message body
This untested code should be close to what you want; just paste it into your
ThisOutlookSession module, and restart Outlook:

Option Explicit
Dim WithEvents myOlInspectors As Outlook.Inspectors
Dim WithEvents myMsg As Outlook.MailItem

Private Sub myOlInspectors_NewInspector(ByVal Inspector As Inspector)
    If Inspector.CurrentItem.Class <> olMail Then Exit Sub
    Set myMsg = Inspector.CurrentItem
End Sub

Private Sub Application_Startup()
    Set myOlInspectors = Application.Inspectors
End Sub

Private Sub Application_Quit()
    Set myCurrentInspector = Nothing
    Set myOlInspectors = Nothing
End Sub

Private Sub myMsg_AttachmentAdd(ByVal Attachment As Attachment)
    myMsg.Body = myMsg.Body & Attachment.FileName
End Sub

--
Eric Legault, MCSD
ADAPSYS - http://www.adapsys.ca


Quote:
> Hi,

> many mails I send with Outlook2000 have one or more attachments.
> In the body of the message I type the filename to refer to
> the attachment(s).
> How can I insert the filename of the attachment(s) automatically in
> the body (at the end of the body) of the message during attaching the
> file or before sending?

> e.g.
> Filenames are "testpic.jpg" + "testdoc.pdf"
> Text which should inserted in message body is
> "Attachment: testpic.jpg, testdoc.pdf"

> Does anybody know a solution with vba?

> All "solutions" I found so far: Getting the filenames of all
> attachments in a outlook-folder.
> That do not really help me.

> Outlook 2000
> Win2000
> Postoffice Workgroup WinNT4 (no Exchange)

> Greatings
> Uwe



Sun, 17 Jul 2005 23:09:23 GMT  
 Q: Attachment: How to insert filename of attachment in message body

Quote:

>This untested code should be close to what you want; just paste it into your
>ThisOutlookSession module, and restart Outlook:

>Option Explicit
>Dim WithEvents myOlInspectors As Outlook.Inspectors
>Dim WithEvents myMsg As Outlook.MailItem

>Private Sub myOlInspectors_NewInspector(ByVal Inspector As Inspector)
>    If Inspector.CurrentItem.Class <> olMail Then Exit Sub
>    Set myMsg = Inspector.CurrentItem
>End Sub

>Private Sub Application_Startup()
>    Set myOlInspectors = Application.Inspectors
>End Sub

>Private Sub Application_Quit()
>    Set myCurrentInspector = Nothing
>    Set myOlInspectors = Nothing
>End Sub

>Private Sub myMsg_AttachmentAdd(ByVal Attachment As Attachment)
>    myMsg.Body = myMsg.Body & Attachment.FileName
>End Sub

Thank You for this,
but the first test wasn't completly successfull.
It works fine till the Sub myMsd_AttachmentAdd finished, then
Outlook ends with a failure.

Testing:
Private Sub myMsg_AttachmentAdd(ByVal Attachment As Attachment)
    myMsg.Body = myMsg.Body & Attachment.FileName
BREAKPOINT
End Sub

At BREAKPOINT the attachments filename is inserted in the body :)
When I continue the code Outlook shuts down with a program failure :(

Furthermore: What's about declaration of myCurrentInspector?
  I added "Dim WithEvents myCurrentInspector As Outlook.Inspectors"

Note: I work with german version of Outlook2000

Greatings
Uwe



Mon, 18 Jul 2005 16:55:26 GMT  
 Q: Attachment: How to insert filename of attachment in message body
My apologies - the "Set myCurrentInspector = Nothing" line is some extra
code; remove it.  I tried testing the code (Outlook XP), and while I didn't
have any crashes, I noticed that the attachment's file name was not getting
saved properly in the Body in the Attachment_Add event.  However, this
seemed to work:

Private Sub myMsg_Send(Cancel As Boolean)
    myMsg.Body = myMsg.Attachments.Item(1).FileName & vbCrLf & myMsg.Body
End Sub

Forget the Attachment_Add event,  although you can use it to flag a variable
that an attachment has been added, and append the Body property in the Send
event with the file name.

--
Eric Legault, MCSD
ADAPSYS - http://www.adapsys.ca


Quote:

> >This untested code should be close to what you want; just paste it into
your
> >ThisOutlookSession module, and restart Outlook:

> >Option Explicit
> >Dim WithEvents myOlInspectors As Outlook.Inspectors
> >Dim WithEvents myMsg As Outlook.MailItem

> >Private Sub myOlInspectors_NewInspector(ByVal Inspector As Inspector)
> >    If Inspector.CurrentItem.Class <> olMail Then Exit Sub
> >    Set myMsg = Inspector.CurrentItem
> >End Sub

> >Private Sub Application_Startup()
> >    Set myOlInspectors = Application.Inspectors
> >End Sub

> >Private Sub Application_Quit()
> >    Set myCurrentInspector = Nothing
> >    Set myOlInspectors = Nothing
> >End Sub

> >Private Sub myMsg_AttachmentAdd(ByVal Attachment As Attachment)
> >    myMsg.Body = myMsg.Body & Attachment.FileName
> >End Sub

> Thank You for this,
> but the first test wasn't completly successfull.
> It works fine till the Sub myMsd_AttachmentAdd finished, then
> Outlook ends with a failure.

> Testing:
> Private Sub myMsg_AttachmentAdd(ByVal Attachment As Attachment)
>     myMsg.Body = myMsg.Body & Attachment.FileName
> BREAKPOINT
> End Sub

> At BREAKPOINT the attachments filename is inserted in the body :)
> When I continue the code Outlook shuts down with a program failure :(

> Furthermore: What's about declaration of myCurrentInspector?
>   I added "Dim WithEvents myCurrentInspector As Outlook.Inspectors"

> Note: I work with german version of Outlook2000

> Greatings
> Uwe



Mon, 18 Jul 2005 23:59:59 GMT  
 Q: Attachment: How to insert filename of attachment in message body
Hi,
now it works.

Thank You very much.

I extended the code handling none, one and several attachments.

The only (little) problem is now, that the format of the body is
changed from "only-text" (before send) to "rich-text" (sent).

Greatings
Uwe



Tue, 19 Jul 2005 16:35:21 GMT  
 Q: Attachment: How to insert filename of attachment in message body
To prevent the format change, you can use CDO to change the Message.Text
property; else if you have Outlook 2002, you can specify the BodyFormat
property to olFormatRichText.

--
Eric Legault, MCSD
ADAPSYS - http://www.adapsys.ca


Quote:
> Hi,
> now it works.

> Thank You very much.

> I extended the code handling none, one and several attachments.

> The only (little) problem is now, that the format of the body is
> changed from "only-text" (before send) to "rich-text" (sent).

> Greatings
> Uwe



Wed, 20 Jul 2005 00:02:55 GMT  
 Q: Attachment: How to insert filename of attachment in message body

Quote:
>To prevent the format change, you can use CDO to change the Message.Text
>property; else if you have Outlook 2002, you can specify the BodyFormat
>property to olFormatRichText.

I don't use CDO.
And it's not necessarily in this case because the messages in folder
"sent objects" only seemed to be send as rich-text, but they are
really send as only-text (because of an option "send as only-text" set
in extras/options/E-Mail-format).

Thank You very much again for Your usefull help.

Greetings
Uwe



Fri, 22 Jul 2005 16:30:03 GMT  
 Q: Attachment: How to insert filename of attachment in message body
Hello Uwe

I was searching to insert the filename for an attachment in a mail (in the
german outlook newsgroup).
There was a posting to look in this VBA newsgroup.

Is it possible to post the code for Outlook 2002 with OnlyText format here
once with a little explanation how to put in Outlook.

Greetings
Ursula from Switzerland



Wed, 27 Jul 2005 23:59:51 GMT  
 Q: Attachment: How to insert filename of attachment in message body

Quote:
>Hello Uwe

>I was searching to insert the filename for an attachment in a mail (in the
>german outlook newsgroup).
>There was a posting to look in this vba newsgroup.

>Is it possible to post the code for Outlook 2002 with OnlyText format here
>once with a little explanation how to put in Outlook.

>Greetings
>Ursula from Switzerland

Hello Ursula,
here are the code.
The most and the creativly parts of it are from Eric Legault.
Explanation are for German Version of Outlook2000 (should similarly
works in Outlook2002).

Copy code in the _Visual Basic Editor_ in _DieseOutlookSitzung_.
Change procedure _myMsg_Send_ for the result that You want.
Close Outlook with saving the changes.
Restart Outlook.
The VBA-Code attaches the filename automatically to the messagebody at
send event (when pressing the _Senden_ - Button).

In two cases last week (approximately 5% of all mails with
attachment), this works not as expected: No text was attached at the
mail. The reason is unclearly for me.

Gr?e
Uwe

Option Explicit
Dim WithEvents myOlInspectors As Outlook.Inspectors
Dim WithEvents myMsg As Outlook.MailItem

Private Sub myOlInspectors_NewInspector(ByVal Inspector As Inspector)
    If Inspector.CurrentItem.Class <> olMail Then Exit Sub
    Set myMsg = Inspector.CurrentItem
End Sub

Private Sub Application_Startup()
   Set myOlInspectors = Application.Inspectors
End Sub

Private Sub Application_Quit()
    Set myOlInspectors = Nothing
End Sub

Private Sub myMsg_Send(Cancel As Boolean)
Dim anzahl_attachments As Integer
Dim zaehler As Integer
anzahl_attachments = myMsg.Attachments.Count
If anzahl_attachments > 0 Then
    myMsg.Body = myMsg.Body & vbCrLf
    For zaehler = 1 To anzahl_attachments
        myMsg.Body = myMsg.Body & _
                  myMsg.Attachments.Item(zaehler).FileName & _
                  vbCrLf
    Next zaehler
End If
End Sub



Fri, 29 Jul 2005 18:15:29 GMT  
 Q: Attachment: How to insert filename of attachment in message body

Quote:
>Hello Uwe

>I was searching to insert the filename for an attachment in a mail (in the
>german outlook newsgroup).
>There was a posting to look in this vba newsgroup.

>Is it possible to post the code for Outlook 2002 with OnlyText format here
>once with a little explanation how to put in Outlook.

>Greetings
>Ursula from Switzerland

Hello Ursula,
at the end of this posting You'll find the code.
The most and the creativly parts of it are from Eric Legault.
Explanation are for German Version of Outlook2000 (should similarly
works in Outlook2002).

Copy code in the _Visual Basic Editor_ in _DieseOutlookSitzung_.
Change procedure _myMsg_Send_ for the result that You want.
Close Outlook with saving the changes.
Restart Outlook.
The VBA-Code attaches the filename automatically to the messagebody at
send event (when pressing the _Senden_ - Button).

In two cases last week (approximately 5% of all mails with
attachment), this works not as expected: No text was attached at the
mail. The reason is unclearly for me.

Gr?e
Uwe

Option Explicit
Dim WithEvents myOlInspectors As Outlook.Inspectors
Dim WithEvents myMsg As Outlook.MailItem

Private Sub myOlInspectors_NewInspector(ByVal Inspector As Inspector)
    If Inspector.CurrentItem.Class <> olMail Then Exit Sub
    Set myMsg = Inspector.CurrentItem
End Sub

Private Sub Application_Startup()
   Set myOlInspectors = Application.Inspectors
End Sub

Private Sub Application_Quit()
    Set myOlInspectors = Nothing
End Sub

Private Sub myMsg_Send(Cancel As Boolean)
Dim anzahl_attachments As Integer
Dim zaehler As Integer
anzahl_attachments = myMsg.Attachments.Count
If anzahl_attachments > 0 Then
    myMsg.Body = myMsg.Body & vbCrLf
    For zaehler = 1 To anzahl_attachments
        myMsg.Body = myMsg.Body & _
                  myMsg.Attachments.Item(zaehler).FileName & _
                  vbCrLf
    Next zaehler
End If
End Sub



Fri, 29 Jul 2005 22:25:06 GMT  
 Q: Attachment: How to insert filename of attachment in message body
Hello Uwe

Quote:
> In two cases last week (approximately 5% of all mails with attachment),

this works not as expected: No text was attached at the mail. The reason is
unclearly for me.
-----

Quote:
> Option Explicit
> Dim WithEvents myOlInspectors As Outlook.Inspectors
> Dim WithEvents myMsg As Outlook.MailItem

> Private Sub myOlInspectors_NewInspector(ByVal Inspector As Inspector)
>     If Inspector.CurrentItem.Class <> olMail Then Exit Sub
>     Set myMsg = Inspector.CurrentItem
> End Sub

> Private Sub Application_Startup()
>    Set myOlInspectors = Application.Inspectors
> End Sub

> Private Sub Application_Quit()
>     Set myOlInspectors = Nothing
> End Sub

> Private Sub myMsg_Send(Cancel As Boolean)
> Dim anzahl_attachments As Integer
> Dim zaehler As Integer
> anzahl_attachments = myMsg.Attachments.Count
> If anzahl_attachments > 0 Then
>     myMsg.Body = myMsg.Body & vbCrLf
>     For zaehler = 1 To anzahl_attachments
>         myMsg.Body = myMsg.Body & _
>                   myMsg.Attachments.Item(zaehler).FileName & _
>                   vbCrLf
>     Next zaehler
> End If
> End Sub

It seems, that I belong to the 5%!!!
Thanks anyway.

Ursula



Thu, 04 Aug 2005 01:01:34 GMT  
 Q: Attachment: How to insert filename of attachment in message body

Hello Ursula,

[...]

Quote:
>It seems, that I belong to the 5%!!!
>Thanks anyway.

alternative:
Copy following code in a modul.
Call this macro after attaching all attachments.

This works in my cases at 100%.

Bye
Uwe

Sub Anhang()
Dim anzahl_attachments As Integer
Dim zaehler As Integer
Dim myMsg As Outlook.MailItem

Set myMsg = ActiveInspector.CurrentItem
If myMsg.Class <> olMail Then Exit Sub

anzahl_attachments = myMsg.Attachments.Count

If anzahl_attachments > 0 Then
    myMsg.Body = myMsg.Body & vbCrLf
    For zaehler = 1 To anzahl_attachments
      myMsg.Body = myMsg.Body & _
                   myMsg.Attachments.Item(zaehler).FileName _
                   & vbCrLf
    Next zaehler
End If

End Sub



Fri, 05 Aug 2005 19:38:13 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Inserting attachments in body for RTF format.

2. Attachment in body of message

3. HTML attachment displayed in message body in MS Outlook

4. vbCrLf after attachment - (not '.Body')

5. Body Layout with an Attachment

6. Retrieving attachments and body from .msg files using a standalone application

7. Html attachment visible in mailitem body

8. Attachments and the mail Body

9. Retrieve HTML body from attachment

10. Saving Body Text From Attachments

11. Saving Item.body and item.attachment

12. Nested Attachments / Forwarded Attachments

 

 
Powered by phpBB® Forum Software