Access Outlook from Word with VBA code

S

Simon Minder

Hi all,

I have a problem with my template in 2007. I worte a template in 2003 with
VBA code behind. Everything works fine there, however, if I run this in 2007
the program stops at 'Set olApp = CreateObject("Outlook.Application")' (see
the whole code below). Are there any changes in VBA between 2003 and 2007? Or
why can I not create such an object?

Private Sub cmdSearchContact_Click()

Dim olApp As Outlook.Application
Dim objContact As Outlook.ContactItem
Dim objContacts As Outlook.MAPIFolder
Dim objNameSpace As Outlook.NameSpace
Dim objAllContacts As Object

' Reset List
lstContacts.Clear


Set olApp = CreateObject("Outlook.Application")
Set objNameSpace = olApp.GetNamespace("MAPI")

If optPersonalContacts = True Then
Set objContacts = objNameSpace.GetDefaultFolder(olFolderContacts)

ElseIf optMfoContacts = True Then
Set objContacts = objNameSpace.Folders("Public Folders"). _
Folders("All Public Folders"). _
Folders("MFO Contacts")
End If

Set objAllContacts = objContacts.Items

For Each objContact In objAllContacts
If objContact.FullName Like "*" & txtSearchContact & "*" Then
lstContacts.AddItem objContact.FullName
Else
' Do nothing
End If
Next
End Sub

Kind regards,

Simon Minder
 
K

Ken Slovak - [MVP - Outlook]

There were no changes to VBA that would cause CreateObject not to work.
Possibilities are a security setting in Word not allowing code to run or
some script stopper in an A-V or software firewall preventing CreateObject
from working.
 

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