Hello everyone,
I am having a problem completing my outlook email setup through VB.
I am gettin Run-time error 424 - Object required:
Actually someone got me to this point, but I'm bumming out on this line:
"lvw.ListItems.Add , , OutlookAddressEntry.Name" - basically all the lines that have the prefix "lvw." because I don't know where that prefix is coming from.
Also, the "OutlookAddressEntry" is not set.
Please help.

Dim OutlookApp As Object
Dim OutlookMailItem As Outlook.MailItem
Dim OutlookAddressList As Outlook.AddressList
Dim OutlookAddressEntry As Outlook.AddressEntry
Dim MailAttach As String
Dim OutlookNSpace As Outlook.NameSpace
Dim OutlookAttach As Outlook.Attachment

MailAttach = "C:\ProjectStatus.xls"
     
 'Send Email using outlook application
 'Note: outlook must be installed on your PC for this module to work:
 '-------------------------------------------------------------------
 Set OutlookApp = New Outlook.Application
 Set OutlookMailItem = OutlookApp.CreateItem(0)
 Set OutlookNSpace = OutlookApp.GetNamespace("MAPI")
   
  
'Get email address from global email address folder
'--------------------------------------------------
For Each OutlookAddressList In OutlookNSpace.AddressLists
    For Each OutlookAddressEntry In OutlookAddressList.AddressEntries
        lvw.ListItems.Add , , OutlookAddressEntry.Name
        lvw.ListItems(lvw.ListItems.Count).SubItems(1) = OutlookAddressEntry.Address
        lvw.ListItems(lvw.ListItems.Count).SubItems(2) = OutlookAddressEntry.ID
        lvw.ListItems(lvw.ListItems.Count).Tag = OutlookAddressEntry.ID
    Next
Next

 EmailID = lvw.ListItems(lvw.ListItems.Count).SubItems(1)

 If Trim(EmailID) <> "" And Trim(EmailID) Like "*@*.*" Or Trim(EmailID) Like "*@*.com" Then
    GoTo SENDEMAIL
 Else
    MsgBox ("You have entered an invalid email address"), vbCritical, "Email Address Error!"
    Exit Sub
 End If


SENDEMAIL:
 OutlookMailItem.To = EmailID
 OutlookMailItem.Subject = "Project Status"
 OutlookMailItem.Body = "This is VB email test"
 
 If Not IsMissing(MailAttach) Then
  Set OutlookAttach = OutlookMailItem.Attachments.Add(MailAttach)
 End If
 
 'If Len(MailAttach) > 0 Then
 '   OutlookMailItem.Attachments.Add "C:\ProjectStatus.xls", olByValue, 1, "ProjectStatus"
 'End If
 
 'OutlookMailItem.Display      'To display the email
 OutlookMailItem.Send         'To send the email
    
 Set OutlookApp = Nothing
 Set OutlookMailItem = Nothing

Thanks,
tgifgemini

Dani AI

Generated

As guessed, "Run-time error 424 — Object required" means the name lvw is not resolving to a live object at runtime. In VB6 that prefix is normally a ListView control on a form; if the routine lives in a standard module the unqualified lvw will fail unless the form instance or a reference to the control is supplied. The Outlook AddressEntry issue is a separate symptom: some address entries (distribution lists, Exchange objects) do not expose a usable SMTP string via the simple Address property.

Quick checks and fixes:

  • Confirm a ListView control exists on the form and its Name property is exactly lvw. The toolbox control should be "Microsoft Windows Common Controls 6.0 (SP6)".
  • If code runs from a standard module, qualify the control or assign it first. Example of getting the control reference from a form instance:
Dim lst As MSComctlLib.ListView
Set lst = frmMain.Controls("lvw")   ' frmMain = the form's instance name
If lst Is Nothing Then
  MsgBox "ListView 'lvw' not found on frmMain"
  Exit Sub
End If
  • Guard Outlook entries before using them. Many GAL/Exchange entries won’t return a plain SMTP address; try the Address property first and fall back to Exchange methods:
Dim smtpAddr As String
If OutlookAddressEntry.Address <> "" Then
  smtpAddr = OutlookAddressEntry.Address
Else
  On Error Resume Next
  smtpAddr = OutlookAddressEntry.GetExchangeUser.PrimarySmtpAddress
  On Error GoTo 0
End If

Additional notes: use Option Explicit and declare the ListView type (or use late binding consistently). If using early binding for Outlook objects, set the Microsoft Outlook Object Library reference; if using the ListView control, ensure mscomctl.ocx is installed/registered on the target machine. These steps address the scope/symbol problem that reported and prevent attempts to call methods on a non-existent object.

do you have a listview named lvw?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.