Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook VBA Code Produces Run Time Error #429 ActiveX component cannot create object 1

Status
Not open for further replies.

Keyboy

Technical User
Aug 22, 2003
193
US
Hello all,

Some background: I am running Win 7 Home Premium SP1 with 64-bit OS on a Dell Studio laptop.

For email I use Outlook 2007 since Windows Live Mail died and Outlook came with my Microsoft Office 2007.

With help I came up with VB macro code to duplicate the permanent delete done by using shift+delete then return. Basically I wanted a permanent delete button on the ribbon to make the operation one click. I learned that VB can't directly delete so you have to move the selected message(s) to a folder (in my case Perm Delete under the inbox) and then delete from there.

Here is my code but when I run it, I get Run Time Error #429 ActiveX component cannot create object on the line Set myOlApp = CreateObject("Outlook.Application").

Sub PermDelete()


Dim muOlApp, myNameSpace, Sel, objRecip As Object
Dim MyItem As Object
Dim MyItem1 As Outlook.MailItem
Dim PermDeleteFolder As Object
Dim objProperty As Object
Dim SavedEntryId, i

Set myOlApp = CreateObject("Outlook.Application")

Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set Sel = Application.ActiveExplorer.Selection
Set PermDeleteFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Folder("Perm Delete")

For i = 1 To Sel.Count
If Sel.Item(i).Class = olMail Then
Set MyItem = Sel.Item(i)
MyItem.Move PermDeleteFolder
End If
Next

Dim obj As Object
For Each obj In PermDeleteFolder.Items

If DateDiff("N", obj.LastModificationTime, Now) < 1 / 1440 Then
obj.Delete
End If
Next
End Sub

I have searched here but only found referenced to Access which I do not believe is applicable to Outlook. Also web searches have suggested a lot of code fixes but after trying them I got the same result.

I am starting to believe something is missing in the Outlook install. I have run the Office repair from the original CD and that did not help.

Can someone point me in the right direction?

Thank you in advance fro your help!
 
Could it be that it is because you did not declare [tt]myOlApp As Object[/tt] anywhere [ponder]
You do have [tt]Dim muOlApp[/tt], but that's a different animal...


---- Andy

There is a great need for a sarcasm font.
 
Hi Andy,

Sorry the code I cut and pasted was out of date. The first line is:

Dim myOlApp, myNameSpace, Sel, objRecip As Object

An excellent catch but the error persists.
 
>Dim myOlApp, myNameSpace, Sel, objRecip As Object

Sadly this line of code is not doing what you think it is. Without an explicit type, variables default to Variant. So myOlApp, myNameSpace, Sel are all being declared as Variant here. Only objRecip is being declared as an Object. As it happens this won't matter too much in tis particular case, since a Variant can hold an Object.


This code looks as if Option Explicit may not be set. So could you try that to see if it catches any errors.

And finally: "out of date",. SO, just that first line is different in later version, or any of the other code. Hate to think we might waste our time trying to help you based on being shown the incorrect code ...
 
Is your code embedded in Outlook or other application? In any case it looks strange:
1. If Outlook:
Outlook objects can be declared according to object types. No need to create Outlook instance. For me the code works (after some syntax correction: Set [tt]PermDeleteFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("Perm Delete")[/tt]
2. If other application:
You use early binding ([tt]Dim MyItem1 As Outlook.MailItem[/tt], Outlook constant [tt]olFolderInbox[/tt] if not declared in other place), again, why not strongly type all Outlook objects? All office applications use host application as Application object in object model, so after [tt]Set Sel = Application.ActiveExplorer.Selection[/tt] the code fails.

Additionally "N" (or "n") in Datediff calculates minutes, is it what you intended in [tt]If DateDiff("N", obj.LastModificationTime, Now) < 1 / 1440 Then[/tt] line?

combo
 
Hello,

I have added the 's' to folders but still get the error. I am not sure what you mean by embedded so let me tell you this. I open Outlook and then click Tools>Macro>Visual Basic Editor and then have this code in Module1. If that is embedded, then I agree.

Any other ideas?

Thanks in advance!
 
VBA is VB for Applications, you can't create standalone application, it has to be attached to host application (as VBA project in Outlook) or be embedded in a document (as in Excel, Word, Access or PowerPoint).
Try with deleted [tt]Set myOlApp = CreateObject("Outlook.Application")[/tt] and replace [tt]myOlApp[/tt] by [tt]Application[/tt] later on.
And please show your current code. As strongm stated, "we might waste our time trying to help you based on being shown the incorrect code".

combo
 
Hello,

OK, I understand, this code is a Project macro within the Outlook Application.

Here is the final code that works:

Sub PermDelete()


Dim myNameSpace, Sel, objRecip As Object
Dim MyItem As Object
Dim MyItem1 As Outlook.MailItem
Dim PermDeleteFolder As Object
Dim objProperty As Object
Dim SavedEntryId, i

Set myNameSpace = Application.GetNamespace("MAPI")
Set Sel = Application.ActiveExplorer.Selection
Set PermDeleteFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("Perm Delete")

For i = 1 To Sel.Count
If Sel.Item(i).Class = olMail Then
Set MyItem = Sel.Item(i)
MyItem.Move PermDeleteFolder
End If
Next

Dim obj As Object
For Each obj In PermDeleteFolder.Items
obj.Delete
Next
End Sub

Thanks to everyone who helped me getting this code to work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top