none
Odd VBA behavior RRS feed

  • Question

  • Hello,

    I am using the code displayed here:

    http://www.outlookcode.com/article.aspx?id=48

    As the article states, it sets the storage folder for outgoing messages.

    About half of the time when I open Outlook, the code operates as it should.

    The other half of the time, it is like the code isn't triggered at all when a message is sent.

    BUT - when it doesn't work, all I have to do is hit Alt-F11 to open the code and immediately click the X in the upper right corner to close it (I don't save, compile, sign, change permissions or the code; just open the VBA window and close it) and thereafter, the code runs as expected....until the next time I open Outlook and get the same 50/50 chance.

    What could be causing this?

    Thanks,

    David

    Thursday, March 10, 2016 4:05 PM

Answers

  • The security setup I described above was the one under which I have been operating the entire time. I never see the Microsoft Outlook Security Notice.

    Oddly, this doesn't pop up even when I change the macro security setting to "Notifications for all macros"

    The only way I see that Security Notice pop-up is after I remove my certificate from the trusted publisher list.

    As soon as I click once on "Trust all documents from this publisher" the pop-up never reappears, even with the macro security setting set to "Notifications for all macros". Maybe that is the expected behavior. I don't know. But that would seem to be an odd use of the word "all".

    So far, after removing the trusted certificate and putting it back, the code has executed as it should. Maybe there was just a registry setting that needed to be reset or something. I will report back with whether the problem happens again or not.

    Thanks again,

    David
    • Marked as answer by David_JunFeng Monday, March 21, 2016 2:17 PM
    Friday, March 11, 2016 3:04 PM

All replies

  • Hello David,

    Most probably VBA macros are disabled in Outlook. You need to check security settings and allow VBA macros there.


    Take a look at the How to use Outlook's VBA Editor article for more information. Also you may find the Plan security settings for VBA macros for Office 2013 page helpful.
    Thursday, March 10, 2016 4:18 PM
  • Thank you for the reply.

    Wouldn't such a setting problem keep it from ever running?

    I have signed the code.

    I have my signing certificate as a Trusted Publisher and I have the setting you mention set to "Notify on all digitally signed macros, others disable."

    Never receive any notification.

    Thanks,

    David

    Thursday, March 10, 2016 4:56 PM
  • What code exactly do you have?
    Thursday, March 10, 2016 5:27 PM
  • I am using the code displayed here:

    http://www.outlookcode.com/article.aspx?id=48

    Private Sub Application_ItemSend(ByVal Item As Object, _
                                     Cancel As Boolean)
        Dim objNS As NameSpace
        Dim objFolder As MAPIFolder
        On Error Resume Next
        Set objNS = Application.Session
        If Item.Class = olMail Then
            Set objFolder = objNS.PickFolder
            If Not objFolder Is Nothing And _
              IsInDefaultStore(objFolder) And _
              objFolder.DefaultItemType = olmailitem Then
                Set Item.SaveSentMessageFolder = objFolder
            Else
                Set objFolder = _
                  objNS.GetDefaultFolder(olFolderSentMail)
                Set Item.SaveSentMessageFolder = objFolder
            End If
        End If
        Set objFolder = Nothing
        Set objNS = Nothing
    End Sub
    
    Public Function IsInDefaultStore(objOL As Object) As Boolean
        Dim objApp As Outlook.Application
        Dim objNS As Outlook.NameSpace
        Dim objInbox As Outlook.MAPIFolder
        Dim blnBadObject As Boolean
        On Error Resume Next
        Set objApp = objOL.Application
        If Err = 0 Then
            Set objNS = objApp.Session
            Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
            Select Case objOL.Class
                Case olFolder
                    If objOL.StoreID = objInbox.StoreID Then
                        IsInDefaultStore = True
                    Else
                        IsInDefaultStore = False
                    End If
                Case olAppointment, olContact, olDistributionList, _
                     olJournal, olMail, olNote, olPost, olTask
                    If objOL.Parent.StoreID = objInbox.StoreID Then
                        IsInDefaultStore = True
                    Else
                        IsInDefaultStore = False
                    End If
                Case Else
                    blnBadObject = True
            End Select
        Else
            blnBadObject = True
        End If
        If blnBadObject Then
            MsgBox "This function isn't designed to work " & _
                    "with " & TypeName(objOL) & _
                    " objects and will return False.", _
                    , "IsInDefaultStore"
            IsInDefaultStore = False
        End If
        Set objApp = Nothing
        Set objNS = Nothing
        Set objInbox = Nothing
    End Function

    Thursday, March 10, 2016 6:46 PM
  • David,

    I tried your code and restarted Outlook for 10 times, was unable to reproduce your issue.

    Now that you have your security settings to "Notify on all digitally signed macros, others disable.", on your Outlook startup, did you see the warn dialog as below?

    Friday, March 11, 2016 5:32 AM
  • Try to choose Enable All Macros. Does it work in that case?

    Most probably the security dialog is overlapped with any window and not visible to users. In that case you can miss the confirmation dialog and VBA code will be left disabled.

    Friday, March 11, 2016 12:18 PM
  • The security setup I described above was the one under which I have been operating the entire time. I never see the Microsoft Outlook Security Notice.

    Oddly, this doesn't pop up even when I change the macro security setting to "Notifications for all macros"

    The only way I see that Security Notice pop-up is after I remove my certificate from the trusted publisher list.

    As soon as I click once on "Trust all documents from this publisher" the pop-up never reappears, even with the macro security setting set to "Notifications for all macros". Maybe that is the expected behavior. I don't know. But that would seem to be an odd use of the word "all".

    So far, after removing the trusted certificate and putting it back, the code has executed as it should. Maybe there was just a registry setting that needed to be reset or something. I will report back with whether the problem happens again or not.

    Thanks again,

    David
    • Marked as answer by David_JunFeng Monday, March 21, 2016 2:17 PM
    Friday, March 11, 2016 3:04 PM
  • Good news, David! Thank you for sharing a possible solution for other forum readers.
    Friday, March 11, 2016 7:48 PM