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 = "smtpout.secureserver.net"
    smtpUser = "testing@m-p-c.org"
    smtpPass = "testing"
    fromAddress = "testing@m-p-c.org"
    
    toAddress = "aleks@aces-project.com"
    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
        .Update
    End With

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

End Sub

Open in new window

APD TorontoAsked:
Who is Participating?
 
RobSampsonCommented:
Try adding these constants to your code:
Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic = 1
Const cdoAnonymous = 0
Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"

Regards,

Rob.
0
 
APD TorontoAuthor Commented:
THANK YOU
0
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.