none
Compile error: Ambiguous name detected: Application_Startup RRS feed

  • Question

  • Hello,

    I have 2 pieces of VBA code in outlook that I am trying to combine into one code. each of them works separately but when I combine them I get the error shown above.

    The first code is

    Private Declare Function ShellExecute Lib "shell32.dll" Alias _
        "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
        ByVal lpFile As String, ByVal lpParameters As String, _
        ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private WithEvents Items As Outlook.Items
    
    
    Private Sub Application_Startup()
        Dim Ns As Outlook.NameSpace
        Dim Folder As Outlook.MAPIFolder
        Set Ns = Application.GetNamespace("MAPI")
        Set Folder = Ns.GetDefaultFolder(olFolderSentMail)
        Set Items = Folder.Items
    End Sub
    
    Private Sub Items_ItemAdd(ByVal Item As Object)
        If TypeOf Item Is Outlook.MailItem Then
            If MsgBox("Print email?", vbYesNo Or vbQuestion) = vbYes _
               Then
                Item.PrintOut
            End If
        End If
    
    End Sub
    
    

    The second code is

    Private WithEvents objinspectors As Outlook.Inspectors
    
    Private Sub Application_Startup()
      Set objinspectors = Application.Inspectors
    End Sub
    
    Private Sub objinspectors_NewInspector(ByVal Inspector As Inspector)
      If TypeName(Inspector.CurrentItem) = "MailItem" Then
        MsgBox "Your Reminder Goes Here"
      End If
    End Sub
    

    I tried to combine them as shown here, but was returned an error:

    Private Declare Function ShellExecute Lib "shell32.dll" Alias _
        "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
        ByVal lpFile As String, ByVal lpParameters As String, _
        ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private WithEvents Items As Outlook.Items
    
    
    Private WithEvents objinspectors As Outlook.Inspectors
    
    Private Sub Application_Startup()
      Set objinspectors = Application.Inspectors
    End Sub
    
    Private Sub objinspectors_NewInspector(ByVal Inspector As Inspector)
      If TypeName(Inspector.CurrentItem) = "MailItem" Then
        MsgBox "Your Reminder Goes Here"
      End If
    End Sub
    
    
    
    Private Sub Application_Startup()
        Dim Ns As Outlook.NameSpace
        Dim Folder As Outlook.MAPIFolder
        Set Ns = Application.GetNamespace("MAPI")
        Set Folder = Ns.GetDefaultFolder(olFolderSentMail)
        Set Items = Folder.Items
    End Sub
    
    Private Sub Items_ItemAdd(ByVal Item As Object)
        If TypeOf Item Is Outlook.MailItem Then
            If MsgBox("Print email?", vbYesNo Or vbQuestion) = vbYes _
               Then
                Item.PrintOut
            End If
        End If
    
    End Sub
    

    Does anyone have any tips for how I can get these 2 vba codes working together within outlook? Thank you, I really appreciate your time!

    Tuesday, October 25, 2016 4:25 PM

Answers

  • Hello Kelsey,

    I have noticed two event handlers in the code. Instead, you need to combine the code into the single one:

    Private Declare Function ShellExecute Lib "shell32.dll" Alias _
        "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
        ByVal lpFile As String, ByVal lpParameters As String, _
        ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private WithEvents Items As Outlook.Items
    
    
    Private WithEvents objinspectors As Outlook.Inspectors
    
    Private Sub objinspectors_NewInspector(ByVal Inspector As Inspector)
      If TypeName(Inspector.CurrentItem) = "MailItem" Then
        MsgBox "Your Reminder Goes Here"
      End If
    End Sub
    
    Private Sub Application_Startup()
        Dim Ns As Outlook.NameSpace
        Dim Folder As Outlook.MAPIFolder
        Set Ns = Application.GetNamespace("MAPI")
        Set Folder = Ns.GetDefaultFolder(olFolderSentMail)
        Set Items = Folder.Items
        
        Set objinspectors = Application.Inspectors
    End Sub
    
    Private Sub Items_ItemAdd(ByVal Item As Object)
        If TypeOf Item Is Outlook.MailItem Then
            If MsgBox("Print email?", vbYesNo Or vbQuestion) = vbYes _
               Then
                Item.PrintOut
            End If
        End If
    
    End Sub


    [custom.development]

    Tuesday, October 25, 2016 6:48 PM

All replies

  • Hello Kelsey,

    I have noticed two event handlers in the code. Instead, you need to combine the code into the single one:

    Private Declare Function ShellExecute Lib "shell32.dll" Alias _
        "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
        ByVal lpFile As String, ByVal lpParameters As String, _
        ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private WithEvents Items As Outlook.Items
    
    
    Private WithEvents objinspectors As Outlook.Inspectors
    
    Private Sub objinspectors_NewInspector(ByVal Inspector As Inspector)
      If TypeName(Inspector.CurrentItem) = "MailItem" Then
        MsgBox "Your Reminder Goes Here"
      End If
    End Sub
    
    Private Sub Application_Startup()
        Dim Ns As Outlook.NameSpace
        Dim Folder As Outlook.MAPIFolder
        Set Ns = Application.GetNamespace("MAPI")
        Set Folder = Ns.GetDefaultFolder(olFolderSentMail)
        Set Items = Folder.Items
        
        Set objinspectors = Application.Inspectors
    End Sub
    
    Private Sub Items_ItemAdd(ByVal Item As Object)
        If TypeOf Item Is Outlook.MailItem Then
            If MsgBox("Print email?", vbYesNo Or vbQuestion) = vbYes _
               Then
                Item.PrintOut
            End If
        End If
    
    End Sub


    [custom.development]

    Tuesday, October 25, 2016 6:48 PM
  • thanks so much!! I appreciate your help !!
    Wednesday, October 26, 2016 4:22 PM