Outlook 2003 Macro Causes VBA to be Disabled

C

Clif Haugen

The following code works fine in Outlook 2000 and 2002. However, in
2003 the code runs fine but the next time Outlook is started it will
disable the VBA module.

I've tried to replace the .OTM file for the session and that hasn't
fixed the problem. I've also replaced all of my Outlook.Application
syntax with just Application. That hasn't worked. I've also looked
for any missing Set statements, but can't locate any.

The offending code is below. I can't claim to have created it - it
was posted in this newsgroup some time ago. I've made a few
modifications and find it very helpful for dealing with Junk Senders.

I would _really_ appreciate any thoughts!!

Clifford

Private Const JUNK_SENDERS_FILE As String = _
"C:\Documents and Settings\jim smartt\Application
Data\Microsoft\Outlook\Junk Senders.txt"


Public Sub MultipleJunkEMailSenders()
Dim objNS As NameSpace
Dim objApp As Application
Dim objExp As Explorer
Dim objMessage As Object
Dim objMailItem As MailItem
Dim objReply As MailItem
Dim objItems As Items
Dim objExplorer As Explorer
Dim intItem As Integer
Dim objFSO As Scripting.FileSystemObject
Dim objTextStream As Scripting.TextStream
Dim CPH As String

Set objApp = New Application
Set objNS = objApp.GetNamespace("MAPI")
Set objExp = objApp.ActiveExplorer
Set objFSO = New Scripting.FileSystemObject

If objFSO.FileExists(FileSpec:=JUNK_SENDERS_FILE) = False Then
Set objTextStream = objFSO.CreateTextFile _
(FileName:=JUNK_SENDERS_FILE)
Else
Set objTextStream = objFSO.OpenTextFile _
(FileName:=JUNK_SENDERS_FILE, IOMode:=ForAppending)
End If

Set objExplorer = Application.ActiveExplorer

For Each objMessage In objExplorer.Selection

Set objMailItem = objMessage
Set objReply = objMailItem.Reply
CPH = objReply.Recipients.Item(1).Address
objReply.Close olDiscard
objTextStream.WriteLine Text:=CPH
objMailItem.UnRead = True
objMailItem.Delete

Next objMessage

End Sub
 
K

Ken Slovak - [MVP - Outlook]

I can see problems from this line:
Set objApp = New Application

Why do that at all? Just use Application. That's an intrinsic object in
Outlook VBA and Outlook 2003 VBA code is very sensitive to things like that.
New Application will produce untrusted VBA code and inherently any Outlook
VBA code that uses the built-in Application object and derives all other
Outlook objects from that is considered as trusted.

I'd also fully qualify all my declarations, not As NameSpace but As
Outlook.NameSpace and so on.
 
D

David C. Jenner

This solves a long standing problem occasionally posted to this newsgroup.

If you search back for postings titled such as
"VBA add-in disabled on startup of Outlook"
"Microsoft VBA for Outlook becoming Disabled"
"Outlook 2003 keeps disabling vba"
etc.
you'll find that many of these problems were never satisfactorily solved.
It was never possible to re-enable VBA, run macros, and not get VBA disabled.

The culprit in these cases is likely to be the creating of a new Application
object. For example, I downloaded this code from Sue Mosher's site and was
trying to use it:

' usage examples setting category A or B
Sub MarkA()
Call MarkWithCategory("A")
End Sub

Sub MarkB()
Call MarkWithCategory("B")
End Sub

' This is the main procedure
Sub MarkWithCategory(strCat As String)
Dim objItem As Object
Set objItem = GetCurrentItem()
If Not objItem Is Nothing Then
objItem.Categories = objItem.Categories & "," & strCat
objItem.Save
End If
Set objItem = Nothing
End Sub

' Returns the currently selected or open item
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = CreateObject("Outlook.Application")
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
Case Else
' anything else will result in an error, which is
' why we have the error handler above
End Select
Set objApp = Nothing
End Function

I posted this at least twice with no resolution.

When I remove
CreateObject(" ")
and leave just the setting of the object to Outlook.Application,
this works for me. Apparently, as stated below, this may have
worked OK in Outlook 2000 and 2003, but does not work in Outlook 2003.

Thanks to Ken for providing a solution (after more than 6 months).

Dave
 
K

Ken Slovak - [MVP - Outlook]

Actually, we've posted this a number of times since Outlook 2003 was
released.

Outlook 2003 is both more and less restrictive than earlier secure versions
of Outlook. It's more restrictive because it adds new restricted properties
such as reading (but not writing) Item.Body and Item.HTMLBody and
Inspector.WordEditor. It's less restrictive because by default all properly
written VBA code running inside Outlook is trusted.

The trust only applies to usage of the Outlook object model, not CDO 1.21
code, and only if all Outlook objects are derived from the intrinsic
Application object in Outlook VBA. If you use that and derive everything
from that object your Outlook VBA code will be trusted and won't trigger the
security prompts.

The same principle applies to Outlook COM addins. If you use the Application
object passed in the On_Connection event handler to derive all your Outlook
objects then they are inherently trusted in Outlook 2003. In an Outlook
property page (OCX control) you use PropertyPageSite.Application for the
same purposes.

Using New, CreateObject or even GetObject will not be trusted and if the
user is running a script stopper such as Norton's it will even crash
Outlook.

I'm not familiar with where that code appears on Sue's Web site offhand. Is
it supposed to be used in a COM addin or Outlook VBA code or just from
anywhere? Please provide a page reference for it and I'll ping Sue to add a
comment or redo the code when she gets back from vacation.
 
D

David C. Jenner

I'm just a "casual" VBA Macro user in Outlook, so I've looked at this
newsgroup only for the specific problem I've had. If the posting title
didn't have "VBA" and "macro" in it, I probably didn't read it and thus
missed the relevant information. I'll probably unsubscribe now that the
problem is solved, so that I have more time to read the other million
newsgroups I subscribe to.

I originally asked in this group how to set a Category using VBA. I was
referred to Sue's site, outlookcode.com, and the example I quoted. I'm
not sure how to get to the example now; I even had to Google to find the
proper Web site name. I'm sure Sue will recognize the example.

Thanks,
Dave
 

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