1
votes

I've code in the "ThisOutlookSession" module.

Application_ItemSend works, events are triggered when sending mail.

Application_Startup runs when I initiate it manually after Outlook has been opened - not upon startup.

Making the sub private makes no difference - neither does making the variables public.

I have macro settings on "Enable all macros" in the Trust Center.

I'm on Outlook 2016 on a PC running Windows 10 Enterprise.

I have researched the issue intensively.

Option Explicit

Dim add_str As String

Public Sub Application_Startup()

    Dim olNs As Outlook.NameSpace
    Dim Folder As Outlook.MAPIFolder
    Dim SubFolder As Outlook.MAPIFolder
    Dim Item As Object

    Set olNs = Application.GetNamespace("MAPI")
    Set Folder = olNs.Folders("[email protected]").Folders("WORKFLOW").Folders("Reporting")

    For Each SubFolder In Folder.Folders
        If SubFolder.items.Restrict("[UnRead] = True").Count > 0 Then
            For Each Item In SubFolder.items
                Item.UnRead = False
            Next
        End If
    Next

End Sub


Public Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    If TypeName(Item) <> "MailItem" Then
        Exit Sub
    End If

    If Item.Subject Like "RE: *" _
        Or Item.Subject Like "AW: *" _
        Or Item.Subject Like "FW: *" Then
        Exit Sub
    End If

    UserForm1.Show

    If add_str = "[URGENT] " Then
        Item.Importance = olImportanceHigh
    End If

    Item.Subject = add_str & Item.Subject

    add_str = vbNullString

End Sub


Public Sub routine(str_ As String)
    add_str = Replace(str_, vbCrLf, " ")
    add_str = "[" & add_str & "] "
End Sub


Sub show_form1()
    UserForm1.Show
End Sub
4
split your startup and for each into 2 subs0m3r

4 Answers

3
votes

I tested your code and I’ve ran into the same problem. I have solved this problem by restarting my PC and adding the Public Sub Application_Quit() method.

2
votes

There seems to be a flag in outlook which checks for VBA code. If it can't find any it sets the registry value (HKEY_CURRENT_USER\Software\Microsoft\Office\XX.0\Outlook\LoadMacroProviderOnBoot) to 0 every time it closes. It doesn't seem to be setup so that it detects when code is added by copying the OTM file. I have discovered 2 scenarios (there may be more) which cause the flag to be set causing outlook to change the registry value to 1 on close.

  1. If any macro is run
  2. The visual basic editor is opened.

The issue arose in my situation when I try to roll out the VBA code to a new PC with no existing VBA code.

I use a batch script to roll out my VBA code to other machines and to fix it I simply added a REG ADD command to the bat file after the code is copied to the machine which sets the key to 1.

REG ADD HKCU\Software\Microsoft\Office\XX.0\Outlook /v LoadMacroProviderOnBoot /t REG_DWORD /d 1 /f

You need to change “XX.0” to the version of office you are dealing with. Check the registry to find out.

This seems to force Outlook to check for VBA code when it starts.

(Thanks to Sergik718 on Microsoft TechNet forums for pointing out that changing this registry entry helps.)

0
votes

Was experiencing same problem with Application_Startup() procedure not firing after a system reload and restore of my VBAProject. Checked macro settings, digitally signed the project - no luck. Received no button to Enable VBA during startup, like in Office 2010. VBA would function, but required me to manually fire the Application_Startup() procedure.

Tried creating a new, temporary "Private Application_Startup()" procedure, but no joy.

I deleted "Public" statement that preceded the Application_Startup() declaration. Saved, closed VBA and Outlook. Re-opened and suddenly it started working. Have restored the "Public", saved, re-opened, and it now seems to work properly. No explanation.

0
votes

Had a similar problem. Application_Startup didn't trigger on restarting Outlook and hence my changes in the function didn't load. The reason Application_Startup didn't trigger was because I had another program holding a reference open to Outlook