How to write a macro?

G

Guest

OK guys, I'm sorry for the seeming basic question, but I've never written an
Outlook macro before and have no basis to start from.

I'm good at VBA, in the Excel and Access environments, but not sure how to
apply that to Outlook.

So here's the thing. I have a report I send out on a daily basis via
Outlook, with attachments which are updated daily. The location and names
remane the same for the attachments, of which I have 3 (in case that makes a
difference).

I have an Outlook template set up for the mail itself, but I need to attach
the files each day as they are constantly changing.

My question is how would I write a macro which allows me to send this
template with the latest files attached.

Again sorry for the simple question, but I have no idea how the Outlook
model works and any help would be gratefully receieved.

TIA.
 
M

Michael Bauer

Am Tue, 27 Sep 2005 01:05:03 -0700 schrieb DaveO:

Dave, there´re a lot of samples available in the VBA help where you
could start. For using the template you can use the
CreateItemFromTemplate function. That returns a MailItem, which has a
Attachments collection. See Attachments.Add for a sample to add
attachments.
 
G

Guest

Thanks for the help Michael, have been able to write something that works now.

My only other question is about attachment.add argurments, specifically the
position of the file to be added. The help text says that if you put a number
'n' greater than the number of characters in the body of the mail, it'll add
it to the end. I have tried this but it always places the attachment at the
beginning of the mail no matter what I do. Any suggestions?

Attached is what I wrote for reference.

-----------------------

Dim outApp As Outlook.Application
Dim malEmail As Outlook.MailItem

Set outApp = CreateObject("Outlook.Application")
Set malEmail = outApp.CreateItemFromTemplate("C:\Documents and
Settings\dogden\Application Data\Microsoft\Templates\Daily report..oft")

malEmail.Display

With malEmail

.Attachments.Add "\\ias_database\centenium\Dave Ogden\Contact flash and
Data counts.xls", , 167

End With

malEmail.Send
 
M

Michael Bauer

Am Wed, 28 Sep 2005 01:15:02 -0700 schrieb DaveO:

Dave, the position works for RTF mails only. Did you try to use the
correct Body length? You can get it by Len(MailItem.Body).
 
G

Guest

In the settings of Outlook I have it set to use Word as the E-mail editor and
send the mail as Rich Text. Is something hampering this?

Also, have altered the code to now look like this...

-----------------

Dim outApp As Outlook.Application
Dim malEmail As Outlook.MailItem
Dim intMailLength As Integer

Set outApp = CreateObject("Outlook.Application")
Set malEmail = outApp.CreateItemFromTemplate("C:\Documents and
Settings\dogden\Application Data\Microsoft\Templates\Daily report..oft")

malEmail.Display

With malEmail

intMailLength = Len(.Body)
.Attachments.Add "\\ias_database\centenium\Dave Ogden\Contact flash and
Data counts.xls", , intMailLength + 1

End With

-------------------

Still it puts the attachment at the beginning of the mail. Do I need to
disable or alter the settings in anyway? Also if I do need to make a change,
can this be done via an Application. command line?

I'm using Outlook 2003 in case that matters.

TIA
 
G

Guest

Have tried other things and now am getting a security warning asking if I
want another program to use E-mail addresses saved in my Address book. How do
i get rid of this, as I'm using an E-mail template and don;t want to have to
allow the VBA to access the address book each time I run this.

Is this alos possible please?

Thanks for the help!
 
M

Michael Bauer

Am Thu, 29 Sep 2005 01:40:13 -0700 schrieb DaveO:

In OL 2003, just use the intrinsic Application object. I.e. remove the
CreateObject line and outApp variable, and replace outApp in the
CreateItemFromTemplate line by Application.
 
G

Guest

OK Michael, I've removed as you said an no longer do I get the box asking me
if I wish a program to use my address book.

However, the attachment is still being placed at the beginning of the mail
which I would like moved to the bottom. Can you think of anyway for me to be
able to do this at all, or check a setting to make sure I am using RTF?

Thanks again.
 
M

Michael Bauer

Am Fri, 30 Sep 2005 00:21:03 -0700 schrieb DaveO:

Dave, you´re right. I know two possible ways:

please don´t

a) use Word as the editor, or
b) display the e-mail before the attachment is added.
 
G

Guest

Michael, it would appear that using word as the editor is the problem here.
Is there anyway to turn this off in code at all?

TIA
 
M

Michael Bauer

Am Fri, 30 Sep 2005 07:37:02 -0700 schrieb DaveO:

Yes, you could find the setting somewhere in the Registry. But OL reads
the change not before its next start. A better approach would be to add
the attachment(s) first and then display the item.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top