excelvbaoutlook

Reference Outlook in Excel VBA: User-defined type not defined


Sub Alerta_email()
    Dim meuoutlook As Outlook.Application
    Dim criaremail As Outlook.MailItem
    
    Range(“E14”).Activate
    
    Do Until activateCell.Value = “”
    
        If ActiveCell.Offset(0, 1).Value < Range(“F7”, ”G7”) Then
        
            Set meuoutlook = New Application
            Set criaremail = Outlook.CriateItem(olMailItem)
            
            With criaremail
                .BodyFormat = olFormatHTML
                .Display
                .HTMLBody = “Alerta automático” & “<br>” & “O documento: “ _
                    & ActiveCell.Offset (0,2).Value & “ vai expirar em “ _
                    & ActiveCell.Offset (0,4).Value & “ Dias. “
                .To = Range(“n14”).Value
                .cc = Range(“o14).Value
                .subject = “Alerta de vencimento do documento: “ & ActiveCell.Offset (0,2).Value
                .send
            End With
            End If
        
        ActiveCell.offse(1, 0).Select
    
    Loop

End Sub

Videos on YouTube sent me to References to select some boxes.

I left the following selected:


Solution

  • I've corrected some of the more obvious errors in your code, see below. I'm not quite sure what was your intended meaning of If ActiveCell.Offset(0, 1).Value < Range(“F7”, ”G7”) Then. Anyway, what you need to do is to select the Microsoft Outlook Object Library:

    1. Open the VBA editor by pressing Alt + F11.
    2. Go to Tools > References.
    3. In the References dialog box, scroll down and check the box for Microsoft Outlook XX.X Object Library (where XX.X is the version number, e.g., 16.0 for Office 2016).
    4. Click OK.

    enter image description here

    Corrected code:

    Sub Alerta_email()
        Dim meuoutlook As Outlook.Application
        Dim criaremail As Outlook.MailItem
        
        Range("E14").Activate
        
        Do Until ActiveCell.Value = ""
        
            If ActiveCell.Offset(0, 1).Value < Range("F7").Value Or ActiveCell.Offset(0, 1).Value < Range("G7").Value Then
            
                Set meuoutlook = New Outlook.Application
                Set criaremail = meuoutlook.CreateItem(olMailItem)
                
                With criaremail
                    .BodyFormat = olFormatHTML
                    .Display
                    .HTMLBody = "Alerta automático" & "<br>" & "O documento: " _
                        & ActiveCell.Offset(0, 2).Value & " vai expirar em " _
                        & ActiveCell.Offset(0, 4).Value & " Dias. "
                    .To = Range("N14").Value
                    .CC = Range("O14").Value
                    .Subject = "Alerta de vencimento do documento: " & ActiveCell.Offset(0, 2).Value
                    .Send
                End With
            End If
            
            ActiveCell.Offset(1, 0).Select
        
        Loop
    
    End Sub