why this code opens two emails and reference isn't valid

hash993

New Member
Joined
Dec 18, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

this code is working properly"ish" the only issue when it opens outlook it opens two new emails not one.
then pop up " Reference isn't valid " then a second pop up "click row number #"

VBA Code:
Sub addlinks()
    Dim lastrow As String
    
    lastrow = Cells(Rows.Count, 1).End(xlDown).Row

    For i = 3 To lastrow
        If Range("e" & i).Value <> "" Then

        
              ActiveSheet.Hyperlinks.Add Range("Z" & i), Address:="", SubAddress:="CreateEmailWithHTMLBody()", TextToDisplay:="Run Macro"
              
        End If
    Next
End Sub
Function CreateEmailWithHTMLBody()

    Dim objOutlook As Object
    Dim objMail As Object

    Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(0)

    With objMail
        .To = "recipient@example.com"
        .Subject = "This is the subject" 

        'Set the HTML body of the email.
        .HTMLBody = "<html><body>This is the <b>HTML</b> body of the email.</body></html>"

        .Display 'Display the email before sending it.
    End With

    Set objMail = Nothing
    Set objOutlook = Nothing
    Exit Function
End Function
 
One problem is that name and position are local variables in addlinks and are out of scope in CreateEmailWithHTMLBody, so their value will be undefined. That is, when the email is actually sent they are blank. You would have discovered this if you were requiring declarations with Option Explicit.

Also, why are you adding name and position after the </html> tag?

I think this whole scheme is unworkable. See my alternative. This uses SelectionChange event instead of a hyperlink.

 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,214,918
Messages
6,122,241
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top