Macro to open email (like double click) in Outlook

J

Jonas Herby

Hi

I'm trying to create a macro which delete the attachments in sent
items in MS Outlook. I have found a code that works fine - the only
problem is that I have to open the mail first. And I can't find any
command which does this.

I simply want to be able to click a button in Outlook , and then run a
macro which opens the message, delete the attachments, and closes the
messaeg again.

Can anyone help me?

The code which delete the attachments is here:
*******************************
Sub DelAtt()


Set itm = Application.ActiveInspector.CurrentItem
If Not itm Is Nothing Then
itm.Save
AttachmentCount = itm.Attachments.count
For lngIndex = AttachmentCount To 1 Step -1
Set att = itm.Attachments(lngIndex)
att.Delete
Next

End If
itm.Close
End Sub
*******************************
 
S

Sue Mosher [MVP]

Replace this statement

Set itm = Application.ActiveInspector.CurrentItem

which operates on an open item, with one that operates on the selected item:

Set itm = Application.ActiveExplorere.Selection(1)
 
S

Sue Mosher [MVP]

Also, you can omit the itm.Close statement if you're not opening the item.
And the itm.Save statement needs to go after the deletions. The way you
currently have it, it does nothing at all.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
J

Jonas Herby

Thanks for the reply.

I have tried your solution, but I'm having troubles.

I have created a shortcut in Outlook I click to run the macro, but
nothing happens when I click it. It worked fine before, so I think
it's the macro which doesn't work properly.

If I run the macro in Visual Basic, I get the following error: "Run
time error '438': Object doesn't support this property or method"

Do you have a clue about what I'm doing wrong?

/Jonas

The code now looks like this
****************Macro start***********************
Sub DelAtt()

Set itm = Application.ActiveExplorere.Selection(1)
If Not itm Is Nothing Then
AttachmentCount = itm.Attachments.count
For lngIndex = AttachmentCount To 1 Step -1
Set att = itm.Attachments(lngIndex)
att.Delete
Next
End If
itm.Save
End Sub

****************Macro start***********************
 
S

Sue Mosher [MVP]

It's ActiveExplorer, not ActiveExplorere. My typo, sorry (but you could have
looked it up in the object browser).

After you fix that, what happens if you step through the code in the VBA
debugger?

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Thanks for the reply.

I have tried your solution, but I'm having troubles.

I have created a shortcut in Outlook I click to run the macro, but
nothing happens when I click it. It worked fine before, so I think
it's the macro which doesn't work properly.

If I run the macro in Visual Basic, I get the following error: "Run
time error '438': Object doesn't support this property or method"

Do you have a clue about what I'm doing wrong?

/Jonas

The code now looks like this
****************Macro start***********************
Sub DelAtt()

Set itm = Application.ActiveExplorere.Selection(1)
If Not itm Is Nothing Then
AttachmentCount = itm.Attachments.count
For lngIndex = AttachmentCount To 1 Step -1
Set att = itm.Attachments(lngIndex)
att.Delete
Next
End If
itm.Save
End Sub

****************Macro start***********************
 

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