Word Document Attachment to an Outlook Email


This article describes how to create an ACUCOBOL-GT program that attaches a Word document to an Outlook email.


You want to learn how to send Outlook messages with ACUCOBOL-GT and/or you want to learn how to attach Word messages to an email.


To work with Outlook and Word, we are going to use our ActiveX interface. To do this we use the utility “axdfgen” to generate copy files describing the interface we can use to communicate with the ActiveX. We add them as copies in the Special-names section of the program.

For example:

           COPY "word.def".
           COPY "outlook.def".

If you need more information about creating these copy files, refer to ACUCOBOL-GT User Interface Programming Chapter 5, section 5.3 ActiveX.

To use Word and Outlook you need to declare certain variables:

       01 wordApp      handle of Application of Word.
       01 wordDoc      handle of Document.
       01 outlApp      handle of Application of Outlook.
       01 mailHandle   handle of Mailitem.
       01 attachs      handle of Attachments.
       01 attach       handle of Attachment.
       01 tNom         pic x(20) value "c:\test.doc".

These variables allow you to store the handle of the different elements we are using in this demo. Now, let’s look at the COBOL code used to invoke Word and Outlook:

           CREATE @Application Of @Word HANDLE IS wordApp

           CREATE @Application Of @Outlook HANDLE IS outlApp

Once invoked, you can open a Word document and create a new Mail Item:

           MODIFY wordApp @Documents::Open ( tNom )
           GIVING wordDoc.

           MODIFY outlApp CreateItem(OlMailItem)
           GIVING mailHandle.

Next, update the mail so it has a Subject, Body and Receiver Address:

           MODIFY  mailHandle     Subject = "Outlook test".

           MODIFY  mailHandle     Body = "Good Job this one".

           MODIFY  mailHandle     @To = "your.email@wherever.com".

Now, quit Word, create an attachment and add it:

           MODIFY wordAPP @Quit(@wdPromptToSaveChanges)

           INQUIRE mailHandle @Attachments attachs.
           MODIFY attachs @Add(tNom)RETURNING attach.

Then send the mail:

           MODIFY  mailHandle @Send().

After this is a little bit of cleaning (shown in the attached full program) and that’s all.

The full program is included in the attached Wordemail.zip file. It contains an AcuBench sample project including comments. In this sample you use Word to open and work on the document, and Outlook to send an email with an attachment. These are two distinct tasks that do not need each other to work, but does shows how easy it is to work with several ActiveX’s at the same time.

Old KB# 14278
Comment List