2016-09-26 137 views
0

此代碼嘗試對mailmerge附件程序進行改進。我的嘗試是包括多個收件人.To.CC。我發現了一些代碼,但無法獲得SendMessage的工作。調用函數時出錯:參數數量錯誤或屬性賦值無效

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 

該錯誤是

Wrong number of arguments or invalid property assignment 

它與SendMessage函數示出。 'SendMessage recips,Subject,body,ccs,bccs,False,附加

+0

添加到您的問題文章,錯誤和生成錯誤的行。 SendMessage的代碼也可能會有所幫助。 – niton

+0

@niton使用SendMessage函數顯示「錯誤的參數數量或無效的屬性分配」錯誤。 'SendMessage recips,主題,正文,ccs,bccs,假,附加' –

回答

1

您沒有爲「SendMessage函數」顯示的代碼必須接受與此行相同數量的參數(參數)。

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

這將是七個參數匹配: recips,主題,正文,CCS,基底細胞癌,假,attachs

它可能看起來像:

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

的名稱可能是相同的如果你希望。

+0

謝謝@niton虐待嘗試代碼 –

相關問題