Akhil raj
Akhil raj

Reputation: 1

Error calling function: Wrong number of arguments or invalid property assignment

This code was an attempt to make an improvement to a mailmerge attachment program. My attempt was to include multiple recipients to .To and .CC. I found some code, but unable to get SendMessage to work.

Sub sendmail()
Dim Source As Document, Maillist As Document, TempDoc As Document
Dim mysubject As String, message As String, title As String
Dim datarange As Range 
Dim body As String
Dim recips As Variant
Dim ccs As Variant
Dim bccs As Variant
Dim j As Integer
Dim attachs As Variant
Set Source = ActiveDocument
With Dialogs(wdDialogFileOpen)  
    .Show
End With
Set Maillist = ActiveDocument
' Show an input box asking the user for the subject to be inserted into the email messages
message = "Enter the subject to be used for each email message."    ' Set prompt.
title = " Email Subject Input"    ' Set title.
' Display message, title
mysubject = InputBox(message, title)
' Iterate through the Sections of the Source document and the rows of the catalog mailmerge document,
' extracting the information to be included in each email.

'IMPORTANT: This assumes your email addresses in the table are separated with commas!
For j = 0 To Source.Sections.Count - 1
    body = Source.Sections(j).Range.Text
    'get to recipients from tables col 1 (I'd prefer this in excel, it's tables are much better!)
    Set datarange = Maillist.Tables(1).Cell(j, 1).Range
    datarange.End = datarange.End - 1
    recips = Split(datarange.Text)
    'CC's
    Set datarange = Maillist.Tables(1).Cell(j, 2).Range
    datarange.End = datarange.End - 1
    ccs = Split(datarange.Text)
    'BCC's
    Set datarange = Maillist.Tables(1).Cell(j, 3).Range
    datarange.End = datarange.End - 1
    bccs = Split(datarange.Text)

    'Attachments array, should be paths, handled by the mail app, in an array
    ReDim attachs(Maillist.Tables(1).Columns.Count - 3) 'minus 2 because you start i at 2 and minus one more for option base 0
    For i = 2 To Maillist.Tables(1).Columns.Count
        Set datarange = Maillist.Tables(1).Cell(j, i).Range
        datarange.End = datarange.End - 1
        attachs(i) = Trim(datarange.Text)
    Next i

   'call the mail sender
   SendMessage recips, Subject, body, ccs, bccs, False, attachs
   Next j
Maillist.Close wdDoNotSaveChanges
MsgBox Source.Sections.Count - 1 & " messages have been sent."
End Sub

The error is

Wrong number of arguments or invalid property assignment

It is showing with SendMessage function. 'SendMessage recips, Subject, body, ccs, bccs, False, attachs

Upvotes: 0

Views: 860

Answers (1)

niton
niton

Reputation: 9179

The code you have not shown for the "SendMessage function" must accept the same number of arguments (parameters) as in this line.

'call the mail sender
SendMessage recips, Subject, body, ccs, bccs, False, attachs

That would be seven arguments matching: recips, Subject, body, ccs, bccs, False, attachs

It could look like:

Function SendMessage (fnrecips, fnSubject, fnbody, fnccs, fnbccs, fnFalse, fnattachs)

The names could be the same if you wish.

Upvotes: 1

Related Questions