VBA User-defined type not defined

Anonymous
2024-01-05T21:15:57+00:00

Hello,

I would like to use the following code to send an email notification when the spreadsheet is changed, but I am getting an error 'User-defined type not defined'

Can you help me?

' Creates email

' Requires Outlook library (References > Microsoft Outlook 16.0 Object Library)

Sub SendMail()

On Error GoTo ErrorHandler 

' Outlook variables 

Dim **olApp As Outlook.Application**, olMail As Outlook.MailItem 

Set olApp = New Outlook.Application 

Set olMail = olApp.CreateItem(olMailItem) 

' Message 

Dim strMessage As String 

strMessage = "Placeholder" 

' Email body 

Dim strBody As String 

strBody = "<body font-size: 11pt><p>" & \_ 

strMessage & "</p></body>" 

' Displays email 

olMail.Display 

' Stores signature 

Dim strSig As String 

strSig = Replace(olMail.HTMLBody, "<p class=MsoNormal><o:p> </o:p></p>", "") 

' Creates email 

With olMail 

    .To = "******@customheli.com" 

    .Recipients.ResolveAll 

    .Subject = "Placeholder" 

    .BodyFormat = olFormatHTML 

    .HTMLBody = strBody & strSig 

End With 

Exit Sub

ErrorHandler:

MsgBox "Error when creating email. Please try again or send the email manually.", vbOKOnly, "Error"

End Sub

Sub Test()

End Sub

Microsoft 365 and Office | Excel | For business | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. HansV 462.4K Reputation points MVP Volunteer Moderator
    2024-01-05T21:35:27+00:00

    In the Visual Basic Editor, select Tools > References...

    Scroll down the list until you see Microsoft Outlook 16.0 Object Library.

    Tick its check box, then click OK.

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2024-01-05T21:42:37+00:00

    It was already ticked.

    0 comments No comments
  3. HansV 462.4K Reputation points MVP Volunteer Moderator
    2024-01-05T22:30:17+00:00

    That is the Microsoft Office 16.0 Object library, not the Microsoft Outlook 16.0 Object Library.

    1 person found this answer helpful.
    0 comments No comments
  4. Anonymous
    2024-01-17T15:42:43+00:00

    My bad! Thank you :)

    0 comments No comments