Setting Library Reference by code / Using CDOSYS in a .vbs file

Hello Experts,

I have the following code which is pretty simple and works in VB6, but when I copy it into a VBS file and comment out the 2 Form_Load lines I get the following error at line 22:

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

Also, in VB6 I can get the same error if I remove the reference to the Microsoft CDO Message library. I guess, my question should be how can I set a Reference to C:\windows\system32\cdosys.dll by code within my .vbs file?

Thank you,
'Option Explicit

Private Sub Form_Load() '**************COMMENT OUT FOR VBS******************
    Call testMail
End Sub '**************COMMENT OUT FOR VBS******************

Sub testMail()
    smtpServer = ""
    smtpUser = ""
    smtpPass = "testing"
    fromAddress = ""
    toAddress = ""
    body = "testing"
    subject = "cdo test"
     Set cdoConfig = CreateObject("CDO.Configuration")
     With cdoConfig.Fields
        .Item(cdoSendUsingMethod) = cdoSendUsingPort
        .Item(cdoSMTPServer) = smtpServer
        .Item(cdoSMTPAuthenticate) = 1
        .Item(cdoSendUsername) = smtpUser
        .Item(cdoSendPassword) = smtpPass
        '.Item("cdoSMTPServerPort") = 45
    End With

    Set cdoMessage = CreateObject("CDO.Message")
    With cdoMessage
        Set .Configuration = cdoConfig
        .From = fromAddress
        .To = toAddress
        .subject = subject
        .HTMLBody = body
    End With
    Set cdoMessage = Nothing
    Set cdoConfig = Nothing
    MsgBox "done."

End Sub

Open in new window

APD TorontoAsked:
Who is Participating?
Try adding these constants to your code:
Const cdoSendUsingMethod = ""
Const cdoSendUsingPort = 2
Const cdoSMTPServer = ""
Const cdoSMTPServerPort = ""
Const cdoSMTPConnectionTimeout = ""
Const cdoSMTPAuthenticate = ""
Const cdoBasic = 1
Const cdoAnonymous = 0
Const cdoSendUserName = ""
Const cdoSendPassword = ""


APD TorontoAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.